From 7a9e438b0aba6ac2aad058a855a635e46aadc7ec Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 10 Feb 2026 11:08:02 -0800 Subject: [PATCH] addressing GLOBAL_SELECTOR atom fetch edge case --- package.json | 2 +- src/connection.test.ts | 77 ++++++++++++++++++- src/connection.ts | 1 + .../repository/atom-value/node/index.ts | 16 ++-- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 22c2be0..7895bf7 100644 --- a/package.json +++ b/package.json @@ -78,5 +78,5 @@ "test:watch": "jest --updateSnapshot --watchAll" }, "types": "dist/index.d.ts", - "version": "2.0.1" + "version": "2.0.2" } diff --git a/src/connection.test.ts b/src/connection.test.ts index 9fd2268..08f30b2 100644 --- a/src/connection.test.ts +++ b/src/connection.test.ts @@ -8,7 +8,7 @@ import { import isEqual from 'lodash.isequal'; -import { GLOBAL_SELECTOR, type Value } from './'; +import { DELETE_TAG, GLOBAL_SELECTOR, MOVE_TAG, type Value } from './'; import AccessorCache from './model/accessor-cache'; import { Immutable } from './main'; @@ -60,6 +60,8 @@ describe( 'Connection class', () => { const propertyPaths = [ 'valid', 'b.message' ]; let d = connection.get( ...propertyPaths ); + jest.runAllTimers(); + passedNoneFoundTest = Object.keys( d ).length === 2 && d[ 'b.message' ] === undefined @@ -94,6 +96,7 @@ describe( 'Connection class', () => { } const v = connection.get( ...propertyPaths ); + jest.runAllTimers(); passedFoundTest = Object.keys( v ).length === 2 @@ -128,15 +131,24 @@ describe( 'Connection class', () => { const gsData = connection.get( GLOBAL_SELECTOR, 'a', 'valid' )[ GLOBAL_SELECTOR ]; + jest.runAllTimers(); expect( gsData ).toStrictEqual( changes ); expect( gsData ).not.toBe( changes ); - jest.useRealTimers() + jest.useRealTimers(); } ); + test( 'fetches the GLOBAL_SELECTOR path by default', () => { + jest.useFakeTimers(); + expect( setup( clonedeep( protectedData ) ).connection.get() ).toEqual({[ GLOBAL_SELECTOR ]: protectedData }); + jest.runAllTimers(); + jest.useRealTimers(); + }); } ); describe( 'disconnection', () => { test( 'discards internally held refs and sets `disconnected` flag', () => { + jest.useFakeTimers(); + const { cache, connection, teardown } = setup(); const cacheGetSpy = jest @@ -147,6 +159,7 @@ describe( 'Connection class', () => { .mockReturnValue( undefined ); connection.get( expect.any( Array ) as unknown as string ); + jest.runAllTimers(); expect( cacheGetSpy ).toHaveBeenCalledTimes( 1 ); connection.set( {} ); expect( setSpy ).toHaveBeenCalledTimes( 1 ); @@ -162,6 +175,8 @@ describe( 'Connection class', () => { connection.set( {} ); expect( setSpy ).not.toHaveBeenCalled(); + jest.useRealTimers(); + jest.restoreAllMocks(); teardown(); @@ -192,6 +207,7 @@ describe( 'Connection class', () => { ); } ); test( 'runs full fetch on complex paths', () => { + jest.useFakeTimers(); const source = createSourceData(); type Data = typeof source; const { connection } = setup( source ); @@ -215,6 +231,7 @@ describe( 'Connection class', () => { 'tags[6]': source.tags[ 6 ], '@@GLOBAL': source }); + jest.runAllTimers(); connection.set({ isActive: true, friends: { 1: { name: { last: 'NEW LNAME' } } }, @@ -242,12 +259,54 @@ describe( 'Connection class', () => { 'tags[6]': source.tags[ 6 ], '@@GLOBAL': updatedDataEquiv }); + jest.runAllTimers(); + jest.useRealTimers(); connection.disconnect(); } ); + test( 'handles deep value computed update requests', () => { + jest.useFakeTimers(); + + const source = createSourceData(); + type Data = typeof source; + const { connection } = setup( source ); + + expect( connection.get() ).toEqual({[ GLOBAL_SELECTOR ]: source }); + jest.runAllTimers(); + + connection.set({ + friends: { [ MOVE_TAG ]: [ -1, 1 ] }, + isActive: true, + history: { + places: { + '2': { + city: 'Marakesh', + country: 'Morocco' + } as unknown as Data["history"]["places"][0] + } as unknown as Data["history"]["places"] + }, + tags: { [ DELETE_TAG ]: [ 3, 5 ] } + } as unknown as Data ); + const defaultState = createSourceData(); + const expectedValue = { ...defaultState }; + expectedValue.friends = [ 0, 2, 1 ].map( i => defaultState.friends[ i ] ); + expectedValue.history.places[ 2 ].city = 'Marakesh'; + expectedValue.history.places[ 2 ].country = 'Morocco'; + expectedValue.isActive = true; + expectedValue.tags = [ 0, 1, 2, 4, 6 ].map( i => defaultState.tags[ i ] ); + + expect( connection.get() ).toEqual({[ GLOBAL_SELECTOR ]: expectedValue }); + jest.runAllTimers(); + + connection.disconnect(); + + jest.useRealTimers(); + + } ); test( 'handles atoms sharing', () => { + jest.useFakeTimers(); const source = createSourceData(); const { connection } = setup( source ); - connection.get( + const data1 = connection.get( 'history.places[2].city', 'history.places[2].country', 'history.places[2].year', @@ -256,6 +315,7 @@ describe( 'Connection class', () => { 'tags[6]', '@@GLOBAL' ); + jest.runAllTimers(); const data2 = connection.get( 'friends[1].name.last', 'history.places[2].country', @@ -263,6 +323,16 @@ describe( 'Connection class', () => { 'company', 'tags[5]', ); + jest.runAllTimers(); + expect( data1 ).toEqual({ + 'history.places[2].city': source.history.places[ 2 ].city, + 'history.places[2].country': source.history.places[ 2 ].country, + 'history.places[2].year': source.history.places[ 2 ].year, + 'isActive': source.isActive, + 'tags[5]': source.tags[ 5 ], + 'tags[6]': source.tags[ 6 ], + '@@GLOBAL': source + }); expect( data2 ).toEqual({ 'friends[1].name.last': source.friends[ 1 ].name.last, 'history.places[2].country': source.history.places[ 2 ].country, @@ -271,5 +341,6 @@ describe( 'Connection class', () => { 'tags[5]': source.tags[ 5 ], }); connection.disconnect(); + jest.useRealTimers(); } ); } ); diff --git a/src/connection.ts b/src/connection.ts index 6f46e66..9e00870 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -67,6 +67,7 @@ export class Connection { this._source = undefined; } + /** @param - defaults to the `GLOBAL_SELECTOR` via the `accessorCache.get` method */ @invoke get( ...propertyPaths : Array ) { return this._source.map diff --git a/src/model/accessor-cache/repository/atom-value/node/index.ts b/src/model/accessor-cache/repository/atom-value/node/index.ts index 928c43f..5129948 100644 --- a/src/model/accessor-cache/repository/atom-value/node/index.ts +++ b/src/model/accessor-cache/repository/atom-value/node/index.ts @@ -219,18 +219,16 @@ class AtomNode{ } return; } + if( activeNode.isRoot ) { + activeNode.value = set( activeNode.value, fullPath, value ) as T; + return; + } const nodePathLen = activeNode.fullPath.length; - if( fullPathLen === nodePathLen ) { - activeNode.value = activeNode.isRoot - ? set( activeNode.value, fullPath, value ) as T - : value; + if( fullPathLen !== nodePathLen ) { + activeNode.value = set( activeNode.value, fullPath.slice( nodePathLen ), value ) as T; return; } - activeNode.value = set( - activeNode.value, - fullPath.slice( nodePathLen ), - value - ) as T; + activeNode.value = value; } /**