Skip to content

Commit 1de9604

Browse files
committed
more optimized retrieval of data from cache
1 parent 22f0083 commit 1de9604

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

lib/OnyxUtils.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,12 @@ function tryGetCachedValue<TKey extends OnyxKey>(key: TKey, mapping?: Partial<Ma
525525
let val = cache.get(key);
526526

527527
if (isCollectionKey(key)) {
528-
// Try optimized collection data retrieval first
529528
const collectionData = cache.getCollectionData(key);
530-
if (collectionData && Object.keys(collectionData).length > 0) {
529+
const allCacheKeys = cache.getAllKeys();
530+
if (collectionData !== undefined && allCacheKeys.size > 0) {
531531
val = collectionData;
532532
} else {
533533
// Fallback to original logic
534-
const allCacheKeys = cache.getAllKeys();
535-
536534
// It is possible we haven't loaded all keys yet so we do not know if the
537535
// collection actually exists.
538536
if (allCacheKeys.size === 0) {
@@ -563,9 +561,10 @@ function tryGetCachedValue<TKey extends OnyxKey>(key: TKey, mapping?: Partial<Ma
563561
}
564562

565563
function getCachedCollection<TKey extends CollectionKeyBase>(collectionKey: TKey, collectionMemberKeys?: string[]): NonNullable<OnyxCollection<KeyValueMapping[TKey]>> {
566-
// Use optimized collection data retrieval
564+
// Use optimized collection data retrieval when cache is populated
567565
const collectionData = cache.getCollectionData(collectionKey);
568-
if (collectionData) {
566+
const allKeys = collectionMemberKeys || cache.getAllKeys();
567+
if (collectionData !== undefined && (Array.isArray(allKeys) ? allKeys.length > 0 : allKeys.size > 0)) {
569568
// If we have specific member keys, filter the collection
570569
if (collectionMemberKeys) {
571570
const filteredCollection: OnyxCollection<KeyValueMapping[TKey]> = {};
@@ -583,7 +582,6 @@ function getCachedCollection<TKey extends CollectionKeyBase>(collectionKey: TKey
583582
}
584583

585584
// Fallback to original implementation if collection data not available
586-
const allKeys = collectionMemberKeys || cache.getAllKeys();
587585
const collection: OnyxCollection<KeyValueMapping[TKey]> = {};
588586

589587
const collectionKeyLength = collectionKey.length;
@@ -1324,8 +1322,7 @@ function subscribeToKey<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKe
13241322
// subscribed to a "collection key" or a single key.
13251323
const matchingKeys: string[] = [];
13261324

1327-
// For now, use the original key matching logic to ensure compatibility
1328-
// TODO: Optimize this after ensuring the cache collection data is always properly populated
1325+
// Use original key matching logic for reliability
13291326
keys.forEach((key) => {
13301327
if (!isKeyMatch(mapping.key, key)) {
13311328
return;

0 commit comments

Comments
 (0)