Skip to content

Commit

Permalink
Merge pull request #577 from callstack-internal/perf/keyChanged
Browse files Browse the repository at this point in the history
[PERF] OnyxUtils keyChanged cached collection retrieval optimisation
  • Loading branch information
MariaHCD authored Aug 7, 2024
2 parents 115542b + 2f237f9 commit 16349bb
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,9 @@ function keyChanged<TKey extends OnyxKey>(
return;
}
}

const cachedCollections: Record<string, ReturnType<typeof getCachedCollection>> = {};

for (let i = 0; i < stateMappingKeys.length; i++) {
const subscriber = callbackToStateMapping[stateMappingKeys[i]];
if (!subscriber || !isKeyMatch(subscriber.key, key) || !canUpdateSubscriber(subscriber)) {
Expand All @@ -822,7 +825,12 @@ function keyChanged<TKey extends OnyxKey>(
}

if (isCollectionKey(subscriber.key) && subscriber.waitForCollectionCallback) {
const cachedCollection = getCachedCollection(subscriber.key);
let cachedCollection = cachedCollections[subscriber.key];

if (!cachedCollection) {
cachedCollection = getCachedCollection(subscriber.key);
cachedCollections[subscriber.key] = cachedCollection;
}

cachedCollection[key] = value;
subscriber.callback(cachedCollection);
Expand Down

0 comments on commit 16349bb

Please sign in to comment.