Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Onyx.clear handle collections and regular keys with an underscore (attempt Nr.2) #578

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading