Skip to content

Commit

Permalink
Merge pull request #578 from margelo/fix/set-callback-not-called-for-…
Browse files Browse the repository at this point in the history
…keys-with-underscore3

fix: `Onyx.clear` handle collections and regular keys with an underscore (attempt Nr.2)
  • Loading branch information
mountiny authored Aug 7, 2024
2 parents 0e596e1 + 5053379 commit e9377df
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,9 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
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] = {};
}
Expand Down
6 changes: 4 additions & 2 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,16 @@ function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(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<TKey extends CollectionKey>(key: TKey): [TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never, string] {
function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType = TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never>(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];
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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_',
Expand All @@ -25,6 +27,7 @@ Onyx.init({
keys: ONYX_KEYS,
initialKeyStates: {
[ONYX_KEYS.OTHER_TEST]: 42,
[ONYX_KEYS.KEY_WITH_UNDERSCORE]: 'default',
},
});

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit e9377df

Please sign in to comment.