@@ -3,7 +3,7 @@ import bindAll from 'lodash/bindAll';
33import type { ValueOf } from 'type-fest' ;
44import utils from './utils' ;
55import type { OnyxKey , OnyxValue } from './types' ;
6- import * as Str from './Str ' ;
6+ import OnyxKeys from './OnyxKeys ' ;
77
88// Task constants
99const TASK = {
@@ -52,12 +52,6 @@ class OnyxCache {
5252 /** List of keys that have been directly subscribed to or recently modified from least to most recent */
5353 private recentlyAccessedKeys = new Set < OnyxKey > ( ) ;
5454
55- /** Set of collection keys for fast lookup */
56- private collectionKeys = new Set < OnyxKey > ( ) ;
57-
58- /** Set of RAM-only keys for fast lookup */
59- private ramOnlyKeys = new Set < OnyxKey > ( ) ;
60-
6155 constructor ( ) {
6256 this . storageKeys = new Set ( ) ;
6357 this . nullishStorageKeys = new Set ( ) ;
@@ -94,11 +88,8 @@ class OnyxCache {
9488 'addEvictableKeysToRecentlyAccessedList' ,
9589 'getKeyForEviction' ,
9690 'setCollectionKeys' ,
97- 'isCollectionKey' ,
98- 'getCollectionKey' ,
9991 'getCollectionData' ,
100- 'setRamOnlyKeys' ,
101- 'isRamOnlyKey' ,
92+ 'hasValueChanged' ,
10293 ) ;
10394 }
10495
@@ -120,13 +111,17 @@ class OnyxCache {
120111 */
121112 setAllKeys ( keys : OnyxKey [ ] ) {
122113 this . storageKeys = new Set ( keys ) ;
114+ for ( const key of keys ) {
115+ OnyxKeys . registerMemberKey ( key ) ;
116+ }
123117 }
124118
125119 /** Saves a key in the storage keys list
126120 * Serves to keep the result of `getAllKeys` up to date
127121 */
128122 addKey ( key : OnyxKey ) : void {
129123 this . storageKeys . add ( key ) ;
124+ OnyxKeys . registerMemberKey ( key ) ;
130125 }
131126
132127 /** Used to set keys that are null/undefined in storage without adding null to the storage map */
@@ -172,7 +167,7 @@ class OnyxCache {
172167 // since it will either be set to a non nullish value or removed from the cache completely.
173168 this . nullishStorageKeys . delete ( key ) ;
174169
175- const collectionKey = this . getCollectionKey ( key ) ;
170+ const collectionKey = OnyxKeys . getCollectionKey ( key ) ;
176171 if ( value === null || value === undefined ) {
177172 delete this . storageMap [ key ] ;
178173
@@ -201,18 +196,19 @@ class OnyxCache {
201196 delete this . storageMap [ key ] ;
202197
203198 // Remove from collection data cache if this is a collection member
204- const collectionKey = this . getCollectionKey ( key ) ;
199+ const collectionKey = OnyxKeys . getCollectionKey ( key ) ;
205200 if ( collectionKey && this . collectionData [ collectionKey ] ) {
206201 delete this . collectionData [ collectionKey ] [ key ] ;
207202 }
208203
209204 // If this is a collection key, clear its data
210- if ( this . isCollectionKey ( key ) ) {
205+ if ( OnyxKeys . isCollectionKey ( key ) ) {
211206 delete this . collectionData [ key ] ;
212207 }
213208
214209 this . storageKeys . delete ( key ) ;
215210 this . recentKeys . delete ( key ) ;
211+ OnyxKeys . deregisterMemberKey ( key ) ;
216212 }
217213
218214 /**
@@ -235,7 +231,7 @@ class OnyxCache {
235231 this . addKey ( key ) ;
236232 this . addToAccessedKeys ( key ) ;
237233
238- const collectionKey = this . getCollectionKey ( key ) ;
234+ const collectionKey = OnyxKeys . getCollectionKey ( key ) ;
239235
240236 if ( value === null || value === undefined ) {
241237 this . addNullishStorageKey ( key ) ;
@@ -325,7 +321,7 @@ class OnyxCache {
325321 delete this . storageMap [ key ] ;
326322
327323 // Remove from collection data cache if this is a collection member
328- const collectionKey = this . getCollectionKey ( key ) ;
324+ const collectionKey = OnyxKeys . getCollectionKey ( key ) ;
329325 if ( collectionKey && this . collectionData [ collectionKey ] ) {
330326 delete this . collectionData [ collectionKey ] [ key ] ;
331327 }
@@ -364,17 +360,7 @@ class OnyxCache {
364360 * @param testKey - Key to check
365361 */
366362 isEvictableKey ( testKey : OnyxKey ) : boolean {
367- return this . evictionAllowList . some ( ( key ) => this . isKeyMatch ( key , testKey ) ) ;
368- }
369-
370- /**
371- * Check if a given key matches a pattern key
372- * @param configKey - Pattern that may contain a wildcard
373- * @param key - Key to test against the pattern
374- */
375- private isKeyMatch ( configKey : OnyxKey , key : OnyxKey ) : boolean {
376- const isCollectionKey = configKey . endsWith ( '_' ) ;
377- return isCollectionKey ? Str . startsWith ( key , configKey ) : configKey === key ;
363+ return this . evictionAllowList . some ( ( key ) => OnyxKeys . isKeyMatch ( key , testKey ) ) ;
378364 }
379365
380366 /**
@@ -411,7 +397,7 @@ class OnyxCache {
411397 return getAllKeysFn ( ) . then ( ( keys : Set < OnyxKey > ) => {
412398 for ( const evictableKey of this . evictionAllowList ) {
413399 for ( const key of keys ) {
414- if ( ! this . isKeyMatch ( evictableKey , key ) ) {
400+ if ( ! OnyxKeys . isKeyMatch ( evictableKey , key ) ) {
415401 continue ;
416402 }
417403
@@ -437,7 +423,7 @@ class OnyxCache {
437423 * Set the collection keys for optimized storage
438424 */
439425 setCollectionKeys ( collectionKeys : Set < OnyxKey > ) : void {
440- this . collectionKeys = collectionKeys ;
426+ OnyxKeys . setCollectionKeys ( collectionKeys ) ;
441427
442428 // Initialize collection data for existing collection keys
443429 for ( const collectionKey of collectionKeys ) {
@@ -446,25 +432,11 @@ class OnyxCache {
446432 }
447433 this . collectionData [ collectionKey ] = { } ;
448434 }
449- }
450-
451- /**
452- * Check if a key is a collection key
453- */
454- isCollectionKey ( key : OnyxKey ) : boolean {
455- return this . collectionKeys . has ( key ) ;
456- }
457435
458- /**
459- * Get the collection key for a given member key
460- */
461- getCollectionKey ( key : OnyxKey ) : OnyxKey | undefined {
462- for ( const collectionKey of this . collectionKeys ) {
463- if ( key . startsWith ( collectionKey ) && key . length > collectionKey . length ) {
464- return collectionKey ;
465- }
436+ // Register existing storageKeys with OnyxKeys
437+ for ( const key of this . storageKeys ) {
438+ OnyxKeys . registerMemberKey ( key ) ;
466439 }
467- return undefined ;
468440 }
469441
470442 /**
@@ -480,19 +452,6 @@ class OnyxCache {
480452 return { ...cachedCollection } ;
481453 }
482454
483- /**
484- * Set the RAM-only keys for optimized storage
485- */
486- setRamOnlyKeys ( ramOnlyKeys : Set < OnyxKey > ) : void {
487- this . ramOnlyKeys = ramOnlyKeys ;
488- }
489-
490- /**
491- * Check if a key is a RAM-only key
492- */
493- isRamOnlyKey ( key : OnyxKey ) : boolean {
494- return this . ramOnlyKeys . has ( key ) ;
495- }
496455}
497456
498457const instance = new OnyxCache ( ) ;
0 commit comments