Skip to content

Commit d0a5d12

Browse files
authored
Merge pull request #75 from Expensify/marcaaron-reinitValuesOnClear
Make sure Onyx re-sets with default values when logging out and back in
2 parents 61dd5a1 + 22bf685 commit d0a5d12

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

lib/Onyx.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ let evictionAllowList = [];
2424
// long as we have at least one subscriber that returns false for the canEvict property.
2525
const evictionBlocklist = {};
2626

27+
// Optional user-provided key value states set when Onyx initializes or clears
28+
let defaultKeyStates = {};
29+
2730
/**
2831
* When a key change happens, search for any callbacks matching the regex pattern and trigger those callbacks
2932
* Get some data from the store
@@ -432,23 +435,6 @@ function multiSet(data) {
432435
.catch(error => evictStorageAndRetry(error, multiSet, data));
433436
}
434437

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-
452438
// Key/value store of Onyx key and arrays of values to merge
453439
const mergeQueue = {};
454440

@@ -515,6 +501,32 @@ function merge(key, val) {
515501
});
516502
}
517503

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+
518530
/**
519531
* Merges a collection based on their keys
520532
*
@@ -584,12 +596,15 @@ function init({
584596
// Let Onyx know about all of our keys
585597
onyxKeys = keys;
586598

599+
// Set our default key states to use when initializing and clearing Onyx data
600+
defaultKeyStates = initialKeyStates;
601+
587602
// Let Onyx know about which keys are safe to evict
588603
evictionAllowList = safeEvictionKeys;
589604
addAllSafeEvictionKeysToRecentlyAccessedList();
590605

591606
// Initialize all of our keys with data provided
592-
_.each(initialKeyStates, (state, key) => merge(key, state));
607+
initializeWithDefaultKeyStates();
593608

594609
// Update any key whose value changes in storage
595610
registerStorageEventListener((key, newValue) => keyChanged(key, newValue));

tests/unit/onyxTest.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
33

44
const ONYX_KEYS = {
55
TEST_KEY: 'test',
6+
ANOTHER_TEST: 'anotherTest',
67
COLLECTION: {
78
TEST_KEY: 'test_',
89
}
@@ -11,6 +12,9 @@ const ONYX_KEYS = {
1112
Onyx.init({
1213
keys: ONYX_KEYS,
1314
registerStorageEventListener: () => {},
15+
initialKeyStates: {
16+
[ONYX_KEYS.ANOTHER_TEST]: 42,
17+
},
1418
});
1519

1620
describe('Onyx', () => {
@@ -68,16 +72,28 @@ describe('Onyx', () => {
6872
initWithStoredValues: false,
6973
callback: (value) => {
7074
testKeyValue = value;
71-
}
75+
},
7276
});
7377

74-
return Onyx.set(ONYX_KEYS.TEST_KEY, 'test')
78+
let anotherTestValue;
79+
const anotherTestConnectionID = Onyx.connect({
80+
key: ONYX_KEYS.ANOTHER_TEST,
81+
callback: (value) => {
82+
anotherTestValue = value;
83+
},
84+
});
85+
86+
return waitForPromisesToResolve()
87+
.then(() => Onyx.set(ONYX_KEYS.TEST_KEY, 'test'))
7588
.then(() => {
7689
expect(testKeyValue).toBe('test');
77-
return Onyx.clear();
90+
expect(anotherTestValue).toBe(42);
91+
return Onyx.clear().then(waitForPromisesToResolve);
7892
})
7993
.then(() => {
8094
expect(testKeyValue).toBeNull();
95+
expect(anotherTestValue).toBe(42);
96+
return Onyx.disconnect(anotherTestConnectionID);
8197
});
8298
});
8399

0 commit comments

Comments
 (0)