Skip to content

Commit

Permalink
Send collection key on connect callback
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioh8010 committed Aug 20, 2024
1 parent 5ac0fd1 commit 9b4759f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
// send the whole cached collection.
if (isSubscribedToCollectionKey) {
if (subscriber.waitForCollectionCallback) {
subscriber.callback(cachedCollection, undefined);
subscriber.callback(cachedCollection, subscriber.key);
continue;
}

Expand Down Expand Up @@ -829,7 +829,7 @@ function keyChanged<TKey extends OnyxKey>(
}

cachedCollection[key] = value;
subscriber.callback(cachedCollection, undefined);
subscriber.callback(cachedCollection, subscriber.key);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ type BaseConnectOptions = {
type DefaultConnectCallback<TKey extends OnyxKey> = (value: OnyxEntry<KeyValueMapping[TKey]>, key: TKey) => void;

/** Represents the callback function used in `Onyx.connect()` method with a collection key. */
type CollectionConnectCallback<TKey extends OnyxKey> = (value: NonUndefined<OnyxCollection<KeyValueMapping[TKey]>>, key: undefined) => void;
type CollectionConnectCallback<TKey extends OnyxKey> = (value: NonUndefined<OnyxCollection<KeyValueMapping[TKey]>>, key: TKey) => void;

/** Represents the options used in `Onyx.connect()` method with a regular key. */
// NOTE: Any changes to this type like adding or removing options must be accounted in OnyxConnectionManager's `generateConnectionID()` method!
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/onyxClearWebStorageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ describe('Set data while storage is clearing', () => {
test_3: 3,
test_4: 4,
},
undefined,
ONYX_KEYS.COLLECTION.TEST,
);
expect(collectionCallback).toHaveBeenLastCalledWith({}, undefined);
expect(collectionCallback).toHaveBeenLastCalledWith({}, ONYX_KEYS.COLLECTION.TEST);
})
);
});
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ describe('Onyx', () => {
expect(mockCallback).toHaveBeenNthCalledWith(1, undefined, undefined);

// AND the value for the second call should be collectionUpdate since the collection was updated
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate, undefined);
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate, ONYX_KEYS.COLLECTION.TEST_POLICY);
})
);
});
Expand Down Expand Up @@ -1072,7 +1072,7 @@ describe('Onyx', () => {
expect(mockCallback).toHaveBeenCalledTimes(2);

// AND the value for the second call should be collectionUpdate
expect(mockCallback).toHaveBeenLastCalledWith(collectionUpdate, undefined);
expect(mockCallback).toHaveBeenLastCalledWith(collectionUpdate, ONYX_KEYS.COLLECTION.TEST_POLICY);
})
);
});
Expand Down Expand Up @@ -1107,7 +1107,7 @@ describe('Onyx', () => {
expect(mockCallback).toHaveBeenCalledTimes(2);

// And the value for the second call should be collectionUpdate
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate, undefined);
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate, ONYX_KEYS.COLLECTION.TEST_POLICY);
})

// When merge is called again with the same collection not modified
Expand Down Expand Up @@ -1148,7 +1148,7 @@ describe('Onyx', () => {
expect(mockCallback).toHaveBeenCalledTimes(1);

// And the value for the second call should be collectionUpdate
expect(mockCallback).toHaveBeenNthCalledWith(1, collectionUpdate, undefined);
expect(mockCallback).toHaveBeenNthCalledWith(1, collectionUpdate, ONYX_KEYS.COLLECTION.TEST_POLICY);
})

// When merge is called again with the same collection not modified
Expand Down Expand Up @@ -1186,7 +1186,7 @@ describe('Onyx', () => {
]).then(() => {
expect(collectionCallback).toHaveBeenCalledTimes(2);
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, undefined);
expect(collectionCallback).toHaveBeenNthCalledWith(2, {[itemKey]: {a: 'a'}}, undefined);
expect(collectionCallback).toHaveBeenNthCalledWith(2, {[itemKey]: {a: 'a'}}, ONYX_KEYS.COLLECTION.TEST_UPDATE);

expect(testCallback).toHaveBeenCalledTimes(2);
expect(testCallback).toHaveBeenNthCalledWith(1, undefined, undefined);
Expand Down Expand Up @@ -1425,7 +1425,7 @@ describe('Onyx', () => {
})
.then(() => {
expect(collectionCallback).toHaveBeenCalledTimes(3);
expect(collectionCallback).toHaveBeenCalledWith(collectionDiff, undefined);
expect(collectionCallback).toHaveBeenCalledWith(collectionDiff, ONYX_KEYS.COLLECTION.ANIMALS);

// Cat hasn't changed from its original value, expect only the initial connect callback
expect(catCallback).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -1556,7 +1556,7 @@ describe('Onyx', () => {
},
},
},
undefined,
ONYX_KEYS.COLLECTION.ROUTES,
);

connections.map((id) => Onyx.disconnect(id));
Expand Down Expand Up @@ -1626,15 +1626,15 @@ describe('Onyx', () => {
{
[cat]: {age: 3, sound: 'meow'},
},
undefined,
ONYX_KEYS.COLLECTION.ANIMALS,
);
expect(animalsCollectionCallback).toHaveBeenNthCalledWith(
2,
{
[cat]: {age: 3, sound: 'meow'},
[dog]: {size: 'M', sound: 'woof'},
},
undefined,
ONYX_KEYS.COLLECTION.ANIMALS,
);

expect(catCallback).toHaveBeenNthCalledWith(1, {age: 3, sound: 'meow'}, cat);
Expand All @@ -1645,7 +1645,7 @@ describe('Onyx', () => {
[bob]: {age: 25, car: 'sedan'},
[lisa]: {age: 21, car: 'SUV'},
},
undefined,
ONYX_KEYS.COLLECTION.PEOPLE,
);

connections.map((id) => Onyx.disconnect(id));
Expand Down

0 comments on commit 9b4759f

Please sign in to comment.