Skip to content

Commit 20f4604

Browse files
changed safeEvictionKeys to evictableKeys
1 parent f21fe96 commit 20f4604

11 files changed

Lines changed: 24 additions & 25 deletions

File tree

API-INTERNAL.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<dt><a href="#setSkippableCollectionMemberIDs">setSkippableCollectionMemberIDs()</a></dt>
2727
<dd><p>Setter - sets the skippable collection member IDs.</p>
2828
</dd>
29-
<dt><a href="#initStoreValues">initStoreValues(keys, initialKeyStates, safeEvictionKeys)</a></dt>
29+
<dt><a href="#initStoreValues">initStoreValues(keys, initialKeyStates, evictableKeys)</a></dt>
3030
<dd><p>Sets the initial values for the Onyx store</p>
3131
</dd>
3232
<dt><a href="#maybeFlushBatchUpdates">maybeFlushBatchUpdates()</a></dt>
@@ -213,7 +213,7 @@ Setter - sets the skippable collection member IDs.
213213
**Kind**: global function
214214
<a name="initStoreValues"></a>
215215

216-
## initStoreValues(keys, initialKeyStates, safeEvictionKeys)
216+
## initStoreValues(keys, initialKeyStates, evictableKeys)
217217
Sets the initial values for the Onyx store
218218

219219
**Kind**: global function
@@ -222,7 +222,7 @@ Sets the initial values for the Onyx store
222222
| --- | --- |
223223
| keys | `ONYXKEYS` constants object from Onyx.init() |
224224
| initialKeyStates | initial data to set when `init()` and `clear()` are called |
225-
| safeEvictionKeys | This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal. |
225+
| evictableKeys | This is an array of keys (individual or collection patterns) that are eligible for automatic removal when storage limits are reached. |
226226

227227
<a name="maybeFlushBatchUpdates"></a>
228228

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,14 @@ Different platforms come with varying storage capacities and Onyx has a way to g
388388
By default, Onyx will not evict anything from storage and will presume all keys are "unsafe" to remove unless explicitly told otherwise.
389389

390390
**To flag a key as safe for removal:**
391-
- Add the key to the `safeEvictionKeys` option in `Onyx.init(options)`
391+
- Add the key to the `evictableKeys` option in `Onyx.init(options)`
392392
- Implement `canEvict` in the Onyx config for each component subscribing to a key
393393
- The key will only be deleted when all subscribers return `true` for `canEvict`
394394

395395
e.g.
396396
```js
397397
Onyx.init({
398-
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
398+
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
399399
});
400400
```
401401

@@ -423,7 +423,7 @@ Provide the `captureMetrics` boolean flag to `Onyx.init` to capture call statist
423423
```js
424424
Onyx.init({
425425
keys: ONYXKEYS,
426-
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
426+
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
427427
captureMetrics: Config.BENCHMARK_ONYX,
428428
});
429429
```

lib/Onyx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import decorateWithMetrics from './metrics';
3939
function init({
4040
keys = {},
4141
initialKeyStates = {},
42-
safeEvictionKeys = [],
42+
evictableKeys = [],
4343
maxCachedKeysCount = 1000,
4444
shouldSyncMultipleInstances = Boolean(global.localStorage),
4545
debugSetState = false,
@@ -71,7 +71,7 @@ function init({
7171
cache.setRecentKeysLimit(maxCachedKeysCount);
7272
}
7373

74-
OnyxUtils.initStoreValues(keys, initialKeyStates, safeEvictionKeys);
74+
OnyxUtils.initStoreValues(keys, initialKeyStates, evictableKeys);
7575

7676
// Initialize all of our keys with data provided then give green light to any pending connections
7777
Promise.all([cache.addAllSafeEvictionKeysToRecentlyAccessedList(OnyxUtils.isCollectionKey), OnyxUtils.initializeWithDefaultKeyStates()]).then(OnyxUtils.getDeferredInitTask().resolve);

lib/OnyxCache.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {ValueOf} from 'type-fest';
44
import utils from './utils';
55
import type {OnyxKey, OnyxValue} from './types';
66
import Storage from './storage';
7+
import * as Str from './Str';
78

89
// Task constants
910
const TASK = {
@@ -319,10 +320,8 @@ class OnyxCache {
319320
* @param key - Key to test against the pattern
320321
*/
321322
private isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean {
322-
// Use a consistent way to determine if a key is a collection key by checking
323-
// if it ends with an underscore (_)
324323
const isCollectionKey = configKey.endsWith('_');
325-
return isCollectionKey ? key.startsWith(configKey) : configKey === key;
324+
return isCollectionKey ? Str.startsWith(key, configKey) : configKey === key;
326325
}
327326

328327
/**

lib/OnyxUtils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ function setSkippableCollectionMemberIDs(ids: Set<string>): void {
138138
*
139139
* @param keys - `ONYXKEYS` constants object from Onyx.init()
140140
* @param initialKeyStates - initial data to set when `init()` and `clear()` are called
141-
* @param safeEvictionKeys - This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal.
141+
* @param evictableKeys - This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal.
142142
*/
143-
function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Partial<KeyValueMapping>, safeEvictionKeys: OnyxKey[]): void {
143+
function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Partial<KeyValueMapping>, evictableKeys: OnyxKey[]): void {
144144
// We need the value of the collection keys later for checking if a
145145
// key is a collection. We store it in a map for faster lookup.
146146
const collectionValues = Object.values(keys.COLLECTION ?? {}) as string[];
@@ -155,7 +155,7 @@ function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Pa
155155
DevTools.initState(initialKeyStates);
156156

157157
// Let Onyx know about which keys are safe to evict
158-
cache.setEvictionAllowList(safeEvictionKeys);
158+
cache.setEvictionAllowList(evictableKeys);
159159

160160
if (typeof keys.COLLECTION === 'object' && typeof keys.COLLECTION.SNAPSHOT === 'string') {
161161
snapshotKey = keys.COLLECTION.SNAPSHOT;

lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ type InitOptions = {
451451
* This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged
452452
* as "safe" for removal. Any components subscribing to these keys must also implement a canEvict option. See the README for more info.
453453
*/
454-
safeEvictionKeys?: OnyxKey[];
454+
evictableKeys?: OnyxKey[];
455455

456456
/**
457457
* Sets how many recent keys should we try to keep in cache

lib/useOnyx.ts

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

212212
if (!OnyxCache.isSafeEvictionKey(key)) {
213-
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({safeEvictionKeys: []}).`);
213+
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

216216
if (options.canEvict) {

lib/withOnyx/index.tsx

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

292292
if (!cache.isSafeEvictionKey(key)) {
293-
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({safeEvictionKeys: []}).`);
293+
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

296296
if (canEvict) {

tests/unit/cacheEvictionTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('Cache eviction', () => {
1919
// When we initialize Onyx and mark the set collection key as a safeEvictionKey
2020
Onyx.init({
2121
keys: ONYX_KEYS,
22-
safeEvictionKeys: [ONYX_KEYS.COLLECTION.TEST_KEY],
22+
evictableKeys: [ONYX_KEYS.COLLECTION.TEST_KEY],
2323
});
2424

2525
// And connect to this key

tests/unit/onyxCacheTest.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ describe('Onyx', () => {
444444
function initOnyx(overrides?: Partial<InitOptions>) {
445445
Onyx.init({
446446
keys: ONYX_KEYS,
447-
safeEvictionKeys: [ONYX_KEYS.COLLECTION.MOCK_COLLECTION],
447+
evictableKeys: [ONYX_KEYS.COLLECTION.MOCK_COLLECTION],
448448
maxCachedKeysCount: 10,
449449
...overrides,
450450
});
@@ -677,7 +677,7 @@ describe('Onyx', () => {
677677
});
678678
});
679679

680-
it('Should prioritize eviction of safeEvictionKeys over non-safe keys when cache limit is reached', () => {
680+
it('Should prioritize eviction of evictableKeys over non-evictable keys when cache limit is reached', () => {
681681
const testKeys = {
682682
...ONYX_KEYS,
683683
SAFE_FOR_EVICTION: 'evictable_',
@@ -710,7 +710,7 @@ describe('Onyx', () => {
710710
return initOnyx({
711711
keys: testKeys,
712712
maxCachedKeysCount: 3, // Only allow 3 keys in cache
713-
safeEvictionKeys: [testKeys.SAFE_FOR_EVICTION],
713+
evictableKeys: [testKeys.SAFE_FOR_EVICTION],
714714
})
715715
.then(() => {
716716
// Connect to non-evictable keys first (oldest in LRU queue)
@@ -746,7 +746,7 @@ describe('Onyx', () => {
746746
});
747747
});
748748

749-
it('Should fall back to LRU order for all keys once all safeEvictionKeys are evicted', () => {
749+
it('Should fall back to LRU order for all keys once all evictableKeys are evicted', () => {
750750
const testKeys = {
751751
...ONYX_KEYS,
752752
SAFE_FOR_EVICTION: 'evictable_',
@@ -775,7 +775,7 @@ describe('Onyx', () => {
775775
return initOnyx({
776776
keys: testKeys,
777777
maxCachedKeysCount: 2, // Only allow 2 keys in cache
778-
safeEvictionKeys: [testKeys.SAFE_FOR_EVICTION],
778+
evictableKeys: [testKeys.SAFE_FOR_EVICTION],
779779
})
780780
.then(() => {
781781
// Connect to keys in LRU order (oldest first)

0 commit comments

Comments
 (0)