Skip to content

Commit

Permalink
Fix for #200 and release version patch
Browse files Browse the repository at this point in the history
  • Loading branch information
radubrehar committed Oct 11, 2023
1 parent f25144b commit fda5089
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 112 deletions.
186 changes: 93 additions & 93 deletions examples/src/pages/tests/table/props/data/mutations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,51 @@ export default test.describe.parallel('Mutations simple test', () => {

await page.waitForTimeout(50);

expect(
await page.evaluate(() => (window as any).mutations.get('2')),
).toEqual([
{
primaryKey: 2,
type: 'update',
data: { id: 2, age: 120 },
originalData: {
id: 2,
firstName: 'Marry',
lastName: 'Bob',
age: 25,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
expect(await page.evaluate(() => (window as any).mutations.get(2))).toEqual(
[
{
primaryKey: 2,
type: 'update',
data: { id: 2, age: 120 },
originalData: {
id: 2,
firstName: 'Marry',
lastName: 'Bob',
age: 25,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
},
},
},
]);
],
);

await editModel.startEdit({ ...cell, event: 'enter', value: '234af' });

await editModel.confirmEdit(cell);

await page.waitForTimeout(50);

expect(
await page.evaluate(() => (window as any).mutations.get('2')),
).toEqual([
{
primaryKey: 2,
type: 'update',
data: { id: 2, age: '234af' },
originalData: {
id: 2,
firstName: 'Marry',
lastName: 'Bob',
age: 120,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
expect(await page.evaluate(() => (window as any).mutations.get(2))).toEqual(
[
{
primaryKey: 2,
type: 'update',
data: { id: 2, age: '234af' },
originalData: {
id: 2,
firstName: 'Marry',
lastName: 'Bob',
age: 120,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
},
},
},
]);
],
);
});

test('api.addData and api.insertData work correctly', async ({
Expand All @@ -81,70 +81,70 @@ export default test.describe.parallel('Mutations simple test', () => {
).toBe('Mark');
expect(await rowModel.getRenderedRowCount()).toBe(7);

expect(
await page.evaluate(() => (window as any).mutations.get('5')),
).toEqual([
{
position: 'after',
primaryKey: 5,
originalData: null,
type: 'insert',
data: {
id: 6,
firstName: 'Mark',
lastName: 'Berg',
age: 39,
canDesign: 'no',
currency: 'USD',
preferredLanguage: 'Go',
stack: 'frontend',
expect(await page.evaluate(() => (window as any).mutations.get(5))).toEqual(
[
{
position: 'after',
primaryKey: 5,
originalData: null,
type: 'insert',
data: {
id: 6,
firstName: 'Mark',
lastName: 'Berg',
age: 39,
canDesign: 'no',
currency: 'USD',
preferredLanguage: 'Go',
stack: 'frontend',
},
},
},
]);

expect(
await page.evaluate(() => (window as any).mutations.get('6')),
).toEqual([
{
position: 'before',
primaryKey: 6,
type: 'insert',
originalData: null,
data: {
id: 7,
firstName: 'Before Mark',
lastName: 'Before',
age: 39,
canDesign: 'no',
currency: 'USD',
preferredLanguage: 'Go',
stack: 'frontend',
],
);

expect(await page.evaluate(() => (window as any).mutations.get(6))).toEqual(
[
{
position: 'before',
primaryKey: 6,
type: 'insert',
originalData: null,
data: {
id: 7,
firstName: 'Before Mark',
lastName: 'Before',
age: 39,
canDesign: 'no',
currency: 'USD',
preferredLanguage: 'Go',
stack: 'frontend',
},
},
},
]);
],
);

await page.getByText('Update id=2 to age=100').click();

await page.waitForTimeout(50);

expect(
await page.evaluate(() => (window as any).mutations.get('2')),
).toEqual([
{
primaryKey: 2,
type: 'update',
data: { id: 2, age: 100 },
originalData: {
id: 2,
firstName: 'Marry',
lastName: 'Bob',
age: 25,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
expect(await page.evaluate(() => (window as any).mutations.get(2))).toEqual(
[
{
primaryKey: 2,
type: 'update',
data: { id: 2, age: 100 },
originalData: {
id: 2,
firstName: 'Marry',
lastName: 'Bob',
age: 25,
canDesign: 'yes',
currency: 'USD',
preferredLanguage: 'JavaScript',
stack: 'frontend',
},
},
},
]);
],
);
});
});
22 changes: 13 additions & 9 deletions source/src/components/DataSource/DataSourceCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export class DataSourceCache<DataType, PrimaryKeyType = string> {
private affectedFields: Set<keyof DataType> = new Set();
private allFieldsAffected: boolean = false;

private primaryKeyToData: Map<string, DataSourceMutation<DataType>[]> =
new Map();
private primaryKeyToData: Map<
PrimaryKeyType,
DataSourceMutation<DataType>[]
> = new Map();

static clone<DataType, PrimaryKeyType = string>(
cache: DataSourceCache<DataType, PrimaryKeyType>,
Expand All @@ -38,7 +40,9 @@ export class DataSourceCache<DataType, PrimaryKeyType = string> {
clone.affectedFields = new Set(cache.affectedFields);
clone.primaryKeyToData = light
? cache.primaryKeyToData
: new Map<string, DataSourceMutation<DataType>[]>(cache.primaryKeyToData);
: new Map<PrimaryKeyType, DataSourceMutation<DataType>[]>(
cache.primaryKeyToData,
);

return clone;
}
Expand All @@ -57,7 +61,7 @@ export class DataSourceCache<DataType, PrimaryKeyType = string> {
metadata: any,
) => {
this.allFieldsAffected = true;
const pk = `${primaryKey}`;
const pk = primaryKey;
const value = this.primaryKeyToData.get(pk) || [];
value.push({
type: 'delete',
Expand All @@ -74,7 +78,7 @@ export class DataSourceCache<DataType, PrimaryKeyType = string> {
position: 'before' | 'after',
metadata: any,
) => {
const pk = `${primaryKey}`;
const pk = primaryKey;
const value = this.primaryKeyToData.get(pk) || [];

this.allFieldsAffected = true;
Expand All @@ -101,7 +105,7 @@ export class DataSourceCache<DataType, PrimaryKeyType = string> {
keys.forEach((key) => this.affectedFields.add(key));
}

const pk = `${primaryKey}`;
const pk = primaryKey;
const value = this.primaryKeyToData.get(pk) || [];

value.push({
Expand All @@ -111,7 +115,7 @@ export class DataSourceCache<DataType, PrimaryKeyType = string> {
originalData,
metadata,
});
this.primaryKeyToData.set(`${primaryKey}`, value);
this.primaryKeyToData.set(primaryKey, value);
};

clear = () => {
Expand All @@ -125,13 +129,13 @@ export class DataSourceCache<DataType, PrimaryKeyType = string> {
};

removeInfo = (primaryKey: PrimaryKeyType) => {
this.primaryKeyToData.delete(`${primaryKey}`);
this.primaryKeyToData.delete(primaryKey);
};

getMutationsForPrimaryKey = (
primaryKey: PrimaryKeyType,
): DataSourceMutation<DataType>[] | undefined => {
const data = this.primaryKeyToData.get(`${primaryKey}`);
const data = this.primaryKeyToData.get(primaryKey);

return data;
};
Expand Down
18 changes: 8 additions & 10 deletions source/src/components/DataSource/Indexer.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { DataSourceCache } from './DataSourceCache';

export class Indexer<DataType, PrimaryKeyType = string> {
primaryKeyToData: Map<string, DataType> = new Map();
primaryKeyToData: Map<PrimaryKeyType, DataType> = new Map();

private add = (primaryKey: PrimaryKeyType | string, data: DataType) => {
this.primaryKeyToData.set(`${primaryKey}`, data);
private add = (primaryKey: PrimaryKeyType, data: DataType) => {
this.primaryKeyToData.set(primaryKey, data);
};

clear = () => {
this.primaryKeyToData.clear();
};

getDataForPrimaryKey = (
primaryKey: PrimaryKeyType | string,
): DataType | undefined => {
return this.primaryKeyToData.get(`${primaryKey}`);
getDataForPrimaryKey = (primaryKey: PrimaryKeyType): DataType | undefined => {
return this.primaryKeyToData.get(primaryKey);
};

indexArray = (
arr: DataType[],
options: {
toPrimaryKey: (data: DataType) => PrimaryKeyType;
cache?: DataSourceCache<DataType>;
cache?: DataSourceCache<DataType, PrimaryKeyType>;
},
) => {
const { cache, toPrimaryKey } = options;
Expand All @@ -39,7 +37,7 @@ export class Indexer<DataType, PrimaryKeyType = string> {
if (item != null) {
// we need this check because for lazy loading we have rows which are not loaded yet

const pk = `${toPrimaryKey(item)}`;
const pk = toPrimaryKey(item);
const cacheInfo = cache?.getMutationsForPrimaryKey(pk);

if (cacheInfo && cacheInfo.length) {
Expand All @@ -63,7 +61,7 @@ export class Indexer<DataType, PrimaryKeyType = string> {
}

if (info.type === 'insert') {
const insertPK = `${toPrimaryKey(info.data)}`;
const insertPK = toPrimaryKey(info.data);
this.add(insertPK, info.data);

if (info.position === 'before') {
Expand Down
4 changes: 4 additions & 0 deletions www/content/docs/releases/index.page.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ title: Releases
description: All releases | Infinite Table DataGrid for React
---

## 3.0.3

@milestone id="98"

## 3.0.1

@milestone id="97"
Expand Down

0 comments on commit fda5089

Please sign in to comment.