From fda5089fe3847f83a5d9b0c9125fdbdbb3e35eb4 Mon Sep 17 00:00:00 2001 From: radubrehar Date: Wed, 11 Oct 2023 15:29:03 +0300 Subject: [PATCH] Fix for #200 and release version patch --- .../tests/table/props/data/mutations.spec.ts | 186 +++++++++--------- .../components/DataSource/DataSourceCache.ts | 22 ++- source/src/components/DataSource/Indexer.ts | 18 +- www/content/docs/releases/index.page.md | 4 + 4 files changed, 118 insertions(+), 112 deletions(-) diff --git a/examples/src/pages/tests/table/props/data/mutations.spec.ts b/examples/src/pages/tests/table/props/data/mutations.spec.ts index 36c19016..e9434e73 100644 --- a/examples/src/pages/tests/table/props/data/mutations.spec.ts +++ b/examples/src/pages/tests/table/props/data/mutations.spec.ts @@ -16,25 +16,25 @@ 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' }); @@ -42,25 +42,25 @@ 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: '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 ({ @@ -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', + }, }, - }, - ]); + ], + ); }); }); diff --git a/source/src/components/DataSource/DataSourceCache.ts b/source/src/components/DataSource/DataSourceCache.ts index 2afaa0b9..e93d7908 100644 --- a/source/src/components/DataSource/DataSourceCache.ts +++ b/source/src/components/DataSource/DataSourceCache.ts @@ -25,8 +25,10 @@ export class DataSourceCache { private affectedFields: Set = new Set(); private allFieldsAffected: boolean = false; - private primaryKeyToData: Map[]> = - new Map(); + private primaryKeyToData: Map< + PrimaryKeyType, + DataSourceMutation[] + > = new Map(); static clone( cache: DataSourceCache, @@ -38,7 +40,9 @@ export class DataSourceCache { clone.affectedFields = new Set(cache.affectedFields); clone.primaryKeyToData = light ? cache.primaryKeyToData - : new Map[]>(cache.primaryKeyToData); + : new Map[]>( + cache.primaryKeyToData, + ); return clone; } @@ -57,7 +61,7 @@ export class DataSourceCache { metadata: any, ) => { this.allFieldsAffected = true; - const pk = `${primaryKey}`; + const pk = primaryKey; const value = this.primaryKeyToData.get(pk) || []; value.push({ type: 'delete', @@ -74,7 +78,7 @@ export class DataSourceCache { position: 'before' | 'after', metadata: any, ) => { - const pk = `${primaryKey}`; + const pk = primaryKey; const value = this.primaryKeyToData.get(pk) || []; this.allFieldsAffected = true; @@ -101,7 +105,7 @@ export class DataSourceCache { keys.forEach((key) => this.affectedFields.add(key)); } - const pk = `${primaryKey}`; + const pk = primaryKey; const value = this.primaryKeyToData.get(pk) || []; value.push({ @@ -111,7 +115,7 @@ export class DataSourceCache { originalData, metadata, }); - this.primaryKeyToData.set(`${primaryKey}`, value); + this.primaryKeyToData.set(primaryKey, value); }; clear = () => { @@ -125,13 +129,13 @@ export class DataSourceCache { }; removeInfo = (primaryKey: PrimaryKeyType) => { - this.primaryKeyToData.delete(`${primaryKey}`); + this.primaryKeyToData.delete(primaryKey); }; getMutationsForPrimaryKey = ( primaryKey: PrimaryKeyType, ): DataSourceMutation[] | undefined => { - const data = this.primaryKeyToData.get(`${primaryKey}`); + const data = this.primaryKeyToData.get(primaryKey); return data; }; diff --git a/source/src/components/DataSource/Indexer.ts b/source/src/components/DataSource/Indexer.ts index 5d291d9e..183cacb0 100644 --- a/source/src/components/DataSource/Indexer.ts +++ b/source/src/components/DataSource/Indexer.ts @@ -1,27 +1,25 @@ import { DataSourceCache } from './DataSourceCache'; export class Indexer { - primaryKeyToData: Map = new Map(); + primaryKeyToData: Map = 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; + cache?: DataSourceCache; }, ) => { const { cache, toPrimaryKey } = options; @@ -39,7 +37,7 @@ export class Indexer { 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) { @@ -63,7 +61,7 @@ export class Indexer { } if (info.type === 'insert') { - const insertPK = `${toPrimaryKey(info.data)}`; + const insertPK = toPrimaryKey(info.data); this.add(insertPK, info.data); if (info.position === 'before') { diff --git a/www/content/docs/releases/index.page.md b/www/content/docs/releases/index.page.md index 9f44a54e..f76a115e 100644 --- a/www/content/docs/releases/index.page.md +++ b/www/content/docs/releases/index.page.md @@ -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"