Skip to content

Commit c009473

Browse files
authored
Merge pull request #642 from callstack-internal/refactor/recentlyAccessedKeys-array-to-set
OnyxCache: Convert recentlyAccessedKeys to set
2 parents 5796442 + c8ed471 commit c009473

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

lib/OnyxCache.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class OnyxCache {
4747
private evictionBlocklist: Record<OnyxKey, string[] | undefined> = {};
4848

4949
/** List of keys that have been directly subscribed to or recently modified from least to most recent */
50-
private recentlyAccessedKeys: OnyxKey[] = [];
50+
private recentlyAccessedKeys = new Set<OnyxKey>();
5151

5252
constructor() {
5353
this.storageKeys = new Set();
@@ -312,7 +312,7 @@ class OnyxCache {
312312
* Remove a key from the recently accessed key list.
313313
*/
314314
removeLastAccessedKey(key: OnyxKey): void {
315-
this.recentlyAccessedKeys = this.recentlyAccessedKeys.filter((recentlyAccessedKey) => recentlyAccessedKey !== key);
315+
this.recentlyAccessedKeys.delete(key);
316316
}
317317

318318
/**
@@ -327,7 +327,7 @@ class OnyxCache {
327327
}
328328

329329
this.removeLastAccessedKey(key);
330-
this.recentlyAccessedKeys.push(key);
330+
this.recentlyAccessedKeys.add(key);
331331
}
332332

333333
/**
@@ -356,7 +356,12 @@ class OnyxCache {
356356
* Finds a key that can be safely evicted
357357
*/
358358
getKeyForEviction(): OnyxKey | undefined {
359-
return this.recentlyAccessedKeys.find((key) => !this.evictionBlocklist[key]);
359+
for (const key of this.recentlyAccessedKeys) {
360+
if (!this.evictionBlocklist[key]) {
361+
return key;
362+
}
363+
}
364+
return undefined;
360365
}
361366
}
362367

0 commit comments

Comments
 (0)