diff --git a/lib/Onyx.ts b/lib/Onyx.ts index f927f353..3a591757 100644 --- a/lib/Onyx.ts +++ b/lib/Onyx.ts @@ -645,6 +645,7 @@ function updateSnapshots(data: OnyxUpdate[]) { const promises: Array<() => Promise> = []; const snapshotCollection = OnyxUtils.getCachedCollection(snapshotCollectionKey); + const snapshotCollectionKeyLength = snapshotCollectionKey.length; Object.entries(snapshotCollection).forEach(([snapshotKey, snapshotValue]) => { // Snapshots may not be present in cache. We don't know how to update them so we skip. @@ -656,7 +657,7 @@ function updateSnapshots(data: OnyxUpdate[]) { data.forEach(({key, value}) => { // snapshots are normal keys so we want to skip update if they are written to Onyx - if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key)) { + if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key, snapshotCollectionKeyLength)) { return; } diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index e37370d6..8430db0d 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -396,8 +396,8 @@ function isCollectionKey(key: OnyxKey): key is CollectionKeyBase { return onyxCollectionKeySet.has(key); } -function isCollectionMemberKey(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}` { - return Str.startsWith(key, collectionKey) && key.length > collectionKey.length; +function isCollectionMemberKey(collectionKey: TCollectionKey, key: string, collectionKeyLength: number): key is `${TCollectionKey}${string}` { + return key.startsWith(collectionKey) && key.length > collectionKeyLength; } /** @@ -556,12 +556,14 @@ function getCachedCollection(collectionKey: TKey const allKeys = collectionMemberKeys || cache.getAllKeys(); const collection: OnyxCollection = {}; + const collectionKeyLength = collectionKey.length; + // forEach exists on both Set and Array allKeys.forEach((key) => { // If we don't have collectionMemberKeys array then we have to check whether a key is a collection member key. // Because in that case the keys will be coming from `cache.getAllKeys()` and we need to filter out the keys that // are not part of the collection. - if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key)) { + if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key, collectionKeyLength)) { return; } @@ -597,6 +599,7 @@ function keysChanged( // individual collection key member for the collection that is being updated. It is important to note that the collection parameter cane be a PARTIAL collection // and does not represent all of the combined keys and values for a collection key. It is just the "new" data that was merged in via mergeCollection(). const stateMappingKeys = Object.keys(callbackToStateMapping); + const collectionKeyLength = collectionKey.length; for (let i = 0; i < stateMappingKeys.length; i++) { const subscriber = callbackToStateMapping[stateMappingKeys[i]]; if (!subscriber) { @@ -616,7 +619,7 @@ function keysChanged( /** * e.g. Onyx.connect({key: `${ONYXKEYS.COLLECTION.REPORT}{reportID}`, callback: ...}); */ - const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key); + const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key, collectionKeyLength); // Regular Onyx.connect() subscriber found. if (typeof subscriber.callback === 'function') { diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts index c80bb853..406968ba 100644 --- a/lib/useOnyx.ts +++ b/lib/useOnyx.ts @@ -109,7 +109,11 @@ function useOnyx>(key: TKey const previousCollectionKey = OnyxUtils.splitCollectionMemberKey(previousKey)[0]; const collectionKey = OnyxUtils.splitCollectionMemberKey(key)[0]; - if (OnyxUtils.isCollectionMemberKey(previousCollectionKey, previousKey) && OnyxUtils.isCollectionMemberKey(collectionKey, key) && previousCollectionKey === collectionKey) { + if ( + OnyxUtils.isCollectionMemberKey(previousCollectionKey, previousKey, previousCollectionKey.length) && + OnyxUtils.isCollectionMemberKey(collectionKey, key, collectionKey.length) && + previousCollectionKey === collectionKey + ) { return; } } catch (e) {