diff --git a/lib/Onyx.ts b/lib/Onyx.ts index f927f353..aef24863 100644 --- a/lib/Onyx.ts +++ b/lib/Onyx.ts @@ -586,8 +586,9 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise { const newValue = defaultKeyStates[key] ?? null; if (newValue !== oldValue) { cache.set(key, newValue); - const collectionKey = key.substring(0, key.indexOf('_') + 1); - if (collectionKey) { + + const collectionKey = OnyxUtils.getCollectionKey(key); + if (OnyxUtils.isCollectionKey(collectionKey)) { if (!keyValuesToResetAsCollection[collectionKey]) { keyValuesToResetAsCollection[collectionKey] = {}; } diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index df4a63b2..27184527 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -405,14 +405,16 @@ function isCollectionMemberKey(collect * @param key - The collection member key to split. * @returns A tuple where the first element is the collection part and the second element is the ID part. */ -function splitCollectionMemberKey(key: TKey): [TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never, string] { +function splitCollectionMemberKey(key: TKey): [CollectionKeyType, string] { const underscoreIndex = key.lastIndexOf('_'); if (underscoreIndex === -1) { throw new Error(`Invalid ${key} key provided, only collection keys are allowed.`); } - return [key.substring(0, underscoreIndex + 1) as TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never, key.substring(underscoreIndex + 1)]; + const collectionKey = key.substring(0, underscoreIndex + 1) as CollectionKeyType; + const memberKey = key.substring(underscoreIndex + 1); + return [collectionKey, memberKey]; } /** diff --git a/tests/unit/onyxTest.ts b/tests/unit/onyxTest.ts index cc13beca..cd53d53f 100644 --- a/tests/unit/onyxTest.ts +++ b/tests/unit/onyxTest.ts @@ -9,6 +9,8 @@ import type GenericCollection from '../utils/GenericCollection'; const ONYX_KEYS = { TEST_KEY: 'test', OTHER_TEST: 'otherTest', + // Special case: this key is not a collection key, but it has an underscore in its name + KEY_WITH_UNDERSCORE: 'nvp_test', COLLECTION: { TEST_KEY: 'test_', TEST_CONNECT_COLLECTION: 'testConnectCollection_', @@ -25,6 +27,7 @@ Onyx.init({ keys: ONYX_KEYS, initialKeyStates: { [ONYX_KEYS.OTHER_TEST]: 42, + [ONYX_KEYS.KEY_WITH_UNDERSCORE]: 'default', }, }); @@ -202,6 +205,35 @@ describe('Onyx', () => { }); }); + it('should notify key subscribers that use a underscore in their name', () => { + const mockCallback = jest.fn(); + connectionID = Onyx.connect({ + key: ONYX_KEYS.KEY_WITH_UNDERSCORE, + callback: mockCallback, + }); + + return waitForPromisesToResolve() + .then(() => mockCallback.mockReset()) + .then(() => Onyx.set(ONYX_KEYS.KEY_WITH_UNDERSCORE, 'test')) + .then(() => { + expect(mockCallback).toHaveBeenCalledTimes(1); + expect(mockCallback).toHaveBeenLastCalledWith('test', ONYX_KEYS.KEY_WITH_UNDERSCORE); + mockCallback.mockReset(); + return Onyx.clear(); + }) + .then(() => { + expect(mockCallback).toHaveBeenCalledTimes(1); + expect(mockCallback).toHaveBeenCalledWith('default', ONYX_KEYS.KEY_WITH_UNDERSCORE); + }) + .then(() => Onyx.set(ONYX_KEYS.KEY_WITH_UNDERSCORE, 'default')) + .then(() => mockCallback.mockReset()) + .then(() => Onyx.set(ONYX_KEYS.KEY_WITH_UNDERSCORE, 'test')) + .then(() => { + expect(mockCallback).toHaveBeenCalledTimes(1); + expect(mockCallback).toHaveBeenCalledWith('test', ONYX_KEYS.KEY_WITH_UNDERSCORE); + }); + }); + it('should not notify subscribers after they have disconnected', () => { let testKeyValue: unknown; connectionID = Onyx.connect({