@@ -24,6 +24,9 @@ let evictionAllowList = [];
24
24
// long as we have at least one subscriber that returns false for the canEvict property.
25
25
const evictionBlocklist = { } ;
26
26
27
+ // Optional user-provided key value states set when Onyx initializes or clears
28
+ let defaultKeyStates = { } ;
29
+
27
30
/**
28
31
* When a key change happens, search for any callbacks matching the regex pattern and trigger those callbacks
29
32
* Get some data from the store
@@ -432,23 +435,6 @@ function multiSet(data) {
432
435
. catch ( error => evictStorageAndRetry ( error , multiSet , data ) ) ;
433
436
}
434
437
435
- /**
436
- * Clear out all the data in the store
437
- *
438
- * @returns {Promise<void> }
439
- */
440
- function clear ( ) {
441
- let allKeys ;
442
- return AsyncStorage . getAllKeys ( )
443
- . then ( keys => allKeys = keys )
444
- . then ( ( ) => AsyncStorage . clear ( ) )
445
- . then ( ( ) => {
446
- _ . each ( allKeys , ( key ) => {
447
- keyChanged ( key , null ) ;
448
- } ) ;
449
- } ) ;
450
- }
451
-
452
438
// Key/value store of Onyx key and arrays of values to merge
453
439
const mergeQueue = { } ;
454
440
@@ -515,6 +501,32 @@ function merge(key, val) {
515
501
} ) ;
516
502
}
517
503
504
+ /**
505
+ * Merge user provided default key value pairs.
506
+ */
507
+ function initializeWithDefaultKeyStates ( ) {
508
+ _ . each ( defaultKeyStates , ( state , key ) => merge ( key , state ) ) ;
509
+ }
510
+
511
+ /**
512
+ * Clear out all the data in the store
513
+ *
514
+ * @returns {Promise<void> }
515
+ */
516
+ function clear ( ) {
517
+ let allKeys ;
518
+ return AsyncStorage . getAllKeys ( )
519
+ . then ( keys => allKeys = keys )
520
+ . then ( ( ) => AsyncStorage . clear ( ) )
521
+ . then ( ( ) => {
522
+ _ . each ( allKeys , ( key ) => {
523
+ keyChanged ( key , null ) ;
524
+ } ) ;
525
+
526
+ initializeWithDefaultKeyStates ( ) ;
527
+ } ) ;
528
+ }
529
+
518
530
/**
519
531
* Merges a collection based on their keys
520
532
*
@@ -584,12 +596,15 @@ function init({
584
596
// Let Onyx know about all of our keys
585
597
onyxKeys = keys ;
586
598
599
+ // Set our default key states to use when initializing and clearing Onyx data
600
+ defaultKeyStates = initialKeyStates ;
601
+
587
602
// Let Onyx know about which keys are safe to evict
588
603
evictionAllowList = safeEvictionKeys ;
589
604
addAllSafeEvictionKeysToRecentlyAccessedList ( ) ;
590
605
591
606
// Initialize all of our keys with data provided
592
- _ . each ( initialKeyStates , ( state , key ) => merge ( key , state ) ) ;
607
+ initializeWithDefaultKeyStates ( ) ;
593
608
594
609
// Update any key whose value changes in storage
595
610
registerStorageEventListener ( ( key , newValue ) => keyChanged ( key , newValue ) ) ;
0 commit comments