diff --git a/lib/OnyxSnapshotCache.ts b/lib/OnyxSnapshotCache.ts index 92c7bba1e..70af8021e 100644 --- a/lib/OnyxSnapshotCache.ts +++ b/lib/OnyxSnapshotCache.ts @@ -43,11 +43,13 @@ class OnyxSnapshotCache { */ getSelectorID(selector: UseOnyxSelector): number { const typedSelector = selector as unknown as UseOnyxSelector; - if (!this.selectorIDMap.has(typedSelector)) { - const id = this.selectorIDCounter++; - this.selectorIDMap.set(typedSelector, id); + const existingID = this.selectorIDMap.get(typedSelector); + if (existingID !== undefined) { + return existingID; } - return this.selectorIDMap.get(typedSelector)!; + const id = this.selectorIDCounter++; + this.selectorIDMap.set(typedSelector, id); + return id; } /** @@ -67,7 +69,7 @@ class OnyxSnapshotCache { const cacheKey = `${key}_${selectorID}`; // Increment reference count for this cache key - const currentCount = this.cacheKeyRefCounts.get(cacheKey) || 0; + const currentCount = this.cacheKeyRefCounts.get(cacheKey) ?? 0; this.cacheKeyRefCounts.set(cacheKey, currentCount + 1); return cacheKey; @@ -78,7 +80,7 @@ class OnyxSnapshotCache { * Decrements reference counter and removes cache entry if no consumers remain. */ deregisterConsumer(key: OnyxKey, cacheKey: string): void { - const currentCount = this.cacheKeyRefCounts.get(cacheKey) || 0; + const currentCount = this.cacheKeyRefCounts.get(cacheKey) ?? 0; if (currentCount <= 1) { // Last consumer - remove from reference counter and cache @@ -111,10 +113,12 @@ class OnyxSnapshotCache { * Set cached snapshot result for a key and cache key combination */ setCachedResult>>(key: OnyxKey, cacheKey: string, result: TResult): void { - if (!this.snapshotCache.has(key)) { - this.snapshotCache.set(key, new Map()); + let keyCache = this.snapshotCache.get(key); + if (!keyCache) { + keyCache = new Map(); + this.snapshotCache.set(key, keyCache); } - this.snapshotCache.get(key)!.set(cacheKey, result); + keyCache.set(cacheKey, result); } /** diff --git a/tests/unit/OnyxSnapshotCacheTest.ts b/tests/unit/OnyxSnapshotCacheTest.ts index aa1126462..316873681 100644 --- a/tests/unit/OnyxSnapshotCacheTest.ts +++ b/tests/unit/OnyxSnapshotCacheTest.ts @@ -123,6 +123,25 @@ describe('OnyxSnapshotCache', () => { expect(secondCall).toBe(thirdCall); }); + it('should return a stable number for the same selector and a different number for a different selector', () => { + const selectorA: TestSelector = (data) => { + const testData = data as TestData | undefined; + return testData?.name ?? ''; + }; + const selectorB: TestSelector = (data) => { + const testData = data as TestData | undefined; + return testData?.id ?? ''; + }; + + const firstA = cache.getSelectorID(selectorA); + const firstB = cache.getSelectorID(selectorB); + const secondA = cache.getSelectorID(selectorA); + + expect(typeof firstA).toBe('number'); + expect(firstA).toBe(secondA); + expect(firstB).not.toBe(firstA); + }); + it('should clear selector IDs and reset counter', () => { const selector1: TestSelector = (data) => { const testData = data as TestData | undefined;