Skip to content

Commit 8a9326c

Browse files
resolve comments
1 parent 22c33c4 commit 8a9326c

7 files changed

Lines changed: 22 additions & 34 deletions

File tree

API-INTERNAL.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ is associated with a collection of keys.</p>
7171
<dd><p>Checks to see if a provided key is the exact configured key of our connected subscriber
7272
or if the provided key is a collection member key (in case our configured key is a &quot;collection key&quot;)</p>
7373
</dd>
74-
<dt><a href="#isSafeEvictionKey">isSafeEvictionKey()</a></dt>
74+
<dt><a href="#isEvictableKey">isEvictableKey()</a></dt>
7575
<dd><p>Checks to see if this key has been flagged as safe for removal.</p>
7676
</dd>
7777
<dt><a href="#getCollectionKey">getCollectionKey(key)</a> ⇒</dt>
@@ -319,9 +319,9 @@ Checks to see if a provided key is the exact configured key of our connected sub
319319
or if the provided key is a collection member key (in case our configured key is a "collection key")
320320

321321
**Kind**: global function
322-
<a name="isSafeEvictionKey"></a>
322+
<a name="isEvictableKey"></a>
323323

324-
## isSafeEvictionKey()
324+
## isEvictableKey()
325325
Checks to see if this key has been flagged as safe for removal.
326326

327327
**Kind**: global function

lib/OnyxCache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class OnyxCache {
7979
'setAllKeys',
8080
'setEvictionAllowList',
8181
'getEvictionBlocklist',
82-
'isSafeEvictionKey',
82+
'isEvictableKey',
8383
'removeLastAccessedKey',
8484
'addLastAccessedKey',
8585
'addAllSafeEvictionKeysToRecentlyAccessedList',
@@ -254,12 +254,12 @@ class OnyxCache {
254254
const key = iterResult.value;
255255
// Don't consider the most recently accessed key for eviction
256256
// This ensures we don't immediately evict a key we just added
257-
if (key !== undefined && key !== mostRecentKey && this.isSafeEvictionKey(key)) {
257+
if (key !== undefined && key !== mostRecentKey && this.isEvictableKey(key)) {
258258
safeKeysToRemove.push(key);
259259
}
260260
iterResult = iterator.next();
261261
}
262-
262+
console.log('safeKeysToRemove', safeKeysToRemove);
263263
safeKeysToRemove.forEach((key) => {
264264
delete this.storageMap[key];
265265
this.recentKeys.delete(key);
@@ -295,7 +295,7 @@ class OnyxCache {
295295
* Checks to see if this key has been flagged as safe for removal.
296296
* @param testKey - Key to check
297297
*/
298-
isSafeEvictionKey(testKey: OnyxKey): boolean {
298+
isEvictableKey(testKey: OnyxKey): boolean {
299299
return this.evictionAllowList.some((key) => this.isKeyMatch(key, testKey));
300300
}
301301

@@ -323,7 +323,7 @@ class OnyxCache {
323323
*/
324324
addLastAccessedKey(key: OnyxKey, isCollectionKey: boolean): void {
325325
// Only specific keys belong in this list since we cannot remove an entire collection.
326-
if (isCollectionKey || !this.isSafeEvictionKey(key)) {
326+
if (isCollectionKey || !this.isEvictableKey(key)) {
327327
return;
328328
}
329329

lib/OnyxUtils.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ function getDeferredInitTask(): DeferredTask {
112112
return deferredInitTask;
113113
}
114114

115-
/**
116-
* Getter - returns the eviction block list.
117-
*/
118-
function getEvictionBlocklist(): Record<OnyxKey, string[] | undefined> {
119-
return cache.getEvictionBlocklist();
120-
}
121-
122115
/**
123116
* Getter - returns the skippable collection member IDs.
124117
*/
@@ -1011,7 +1004,7 @@ function sendDataToConnection<TKey extends OnyxKey>(mapping: Mapping<TKey>, valu
10111004
* run out of storage the least recently accessed key can be removed.
10121005
*/
10131006
function addKeyToRecentlyAccessedIfNeeded<TKey extends OnyxKey>(mapping: Mapping<TKey>): void {
1014-
if (!cache.isSafeEvictionKey(mapping.key)) {
1007+
if (!cache.isEvictableKey(mapping.key)) {
10151008
return;
10161009
}
10171010

@@ -1424,7 +1417,6 @@ const OnyxUtils = {
14241417
doAllCollectionItemsBelongToSameParent,
14251418
subscribeToKey,
14261419
unsubscribeFromKey,
1427-
getEvictionBlocklist,
14281420
getSkippableCollectionMemberIDs,
14291421
setSkippableCollectionMemberIDs,
14301422
};

lib/useOnyx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
209209
return;
210210
}
211211

212-
if (!OnyxCache.isSafeEvictionKey(key)) {
212+
if (!OnyxCache.isEvictableKey(key)) {
213213
throw new Error(`canEvict can't be used on key '${key}'. This key must explicitly be flagged as safe for removal by adding it to Onyx.init({evictableKeys: []}).`);
214214
}
215215

lib/withOnyx/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export default function <TComponentProps, TOnyxProps>(
289289
const canEvict = !!Str.result(mapping.canEvict as GenericFunction, this.props);
290290
const key = Str.result(mapping.key as GenericFunction, this.props);
291291

292-
if (!cache.isSafeEvictionKey(key)) {
292+
if (!cache.isEvictableKey(key)) {
293293
throw new Error(`canEvict can't be used on key '${key}'. This key must explicitly be flagged as safe for removal by adding it to Onyx.init({evictableKeys: []}).`);
294294
}
295295

tests/unit/cacheEvictionTest.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@ test('Cache eviction', () => {
5050
);
5151
StorageMock.setItem = setItemMock;
5252

53-
return Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`, {test: 'add'})
54-
.then(() => waitForPromisesToResolve())
55-
.then(() => {
56-
// Then our collection should no longer contain the evictable key
57-
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`]).toBe(undefined);
58-
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`]).toStrictEqual({test: 'add'});
59-
});
53+
return Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`, {test: 'add'}).then(() => {
54+
// Then our collection should no longer contain the evictable key
55+
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`]).toBe(undefined);
56+
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`]).toStrictEqual({test: 'add'});
57+
});
6058
});
6159
});

tests/unit/onyxCacheTest.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,7 @@ describe('Onyx', () => {
535535
let connections: Array<{key: string; connection: Connection}> = [];
536536

537537
// Given Onyx is configured with max 5 keys in cache
538-
return initOnyx({
539-
maxCachedKeysCount: 5,
540-
})
538+
return initOnyx({maxCachedKeysCount: 5})
541539
.then(() => {
542540
// Given 10 connections for different keys
543541
connections = range.map((number) => {
@@ -709,11 +707,11 @@ describe('Onyx', () => {
709707
})
710708
.then(() => {
711709
// Verify keys are correctly identified as evictable or not
712-
expect(cache.isSafeEvictionKey?.(evictableKey1)).toBe(true);
713-
expect(cache.isSafeEvictionKey?.(evictableKey2)).toBe(true);
714-
expect(cache.isSafeEvictionKey?.(evictableKey3)).toBe(true);
715-
expect(cache.isSafeEvictionKey?.(triggerKey)).toBe(true);
716-
expect(cache.isSafeEvictionKey?.(criticalKey1)).toBe(false);
710+
expect(cache.isEvictableKey?.(evictableKey1)).toBe(true);
711+
expect(cache.isEvictableKey?.(evictableKey2)).toBe(true);
712+
expect(cache.isEvictableKey?.(evictableKey3)).toBe(true);
713+
expect(cache.isEvictableKey?.(triggerKey)).toBe(true);
714+
expect(cache.isEvictableKey?.(criticalKey1)).toBe(false);
717715

718716
// Connect to non-evictable keys first
719717
Onyx.connect({key: criticalKey1, callback: jest.fn()});

0 commit comments

Comments
 (0)