Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/OnyxSnapshotCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ class OnyxSnapshotCache {
*/
getSelectorID<TKey extends OnyxKey, TReturnValue>(selector: UseOnyxSelector<TKey, TReturnValue>): number {
const typedSelector = selector as unknown as UseOnyxSelector<OnyxKey, unknown>;
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;
}

/**
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -111,10 +113,12 @@ class OnyxSnapshotCache {
* Set cached snapshot result for a key and cache key combination
*/
setCachedResult<TResult extends UseOnyxResult<OnyxValue<OnyxKey>>>(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);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/OnyxSnapshotCacheTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading