react-native-onyx 3.0.138 → 3.0.140
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Onyx.js +42 -3
- package/dist/PendingWrites.d.ts +5 -0
- package/dist/PendingWrites.js +14 -0
- package/package.json +1 -1
package/dist/Onyx.js
CHANGED
|
@@ -385,7 +385,15 @@ function mergeCollection(collectionKey, collection) {
|
|
|
385
385
|
* @param keysToPreserve is a list of ONYXKEYS that should not be cleared with the rest of the data
|
|
386
386
|
*/
|
|
387
387
|
function clear(keysToPreserve = []) {
|
|
388
|
-
|
|
388
|
+
// Writes can land while storage clears (e.g. a new sign-in's session), so record them to avoid clobbering them.
|
|
389
|
+
// Matching on values instead would confuse them with reads refilling the cache from storage.
|
|
390
|
+
const keysWrittenDuringClear = new Set();
|
|
391
|
+
const removeWriteListener = PendingWrites_1.default.addWriteListener((keys) => {
|
|
392
|
+
for (const key of keys) {
|
|
393
|
+
keysWrittenDuringClear.add(key);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
const clearPromise = PendingWrites_1.default.trackPendingGlobalWrite(OnyxUtils_1.default.afterInit(() => {
|
|
389
397
|
const pendingMergeEntries = OnyxUtils_1.default.getPendingMergeEntries(keysToPreserve);
|
|
390
398
|
const defaultKeyStates = OnyxUtils_1.default.getDefaultKeyStates();
|
|
391
399
|
const initialKeys = Object.keys(defaultKeyStates);
|
|
@@ -399,6 +407,16 @@ function clear(keysToPreserve = []) {
|
|
|
399
407
|
// because the notification process needs the old values in cache but at that point they will be already removed from it.
|
|
400
408
|
const keyValuesToResetAsCollection = {};
|
|
401
409
|
const allKeys = new Set([...cachedKeys, ...initialKeys]);
|
|
410
|
+
// A write issued before the reset below is overwritten by it, so only a cache value that differs from
|
|
411
|
+
// what clear set counts as a write that landed after the reset.
|
|
412
|
+
const valuesSetByClear = {};
|
|
413
|
+
const wasWrittenDuringClear = (key) => {
|
|
414
|
+
if (!keysWrittenDuringClear.has(key) || !(key in valuesSetByClear)) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
const currentValue = OnyxCache_1.default.get(key);
|
|
418
|
+
return currentValue !== undefined && currentValue !== valuesSetByClear[key];
|
|
419
|
+
};
|
|
402
420
|
OnyxUtils_1.default.cancelPendingMerges(pendingMergeEntries);
|
|
403
421
|
// The only keys that should not be cleared are:
|
|
404
422
|
// 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline
|
|
@@ -416,6 +434,7 @@ function clear(keysToPreserve = []) {
|
|
|
416
434
|
if (!isKeyToPreserve) {
|
|
417
435
|
const oldValue = OnyxCache_1.default.get(key);
|
|
418
436
|
const newValue = (_a = defaultKeyStates[key]) !== null && _a !== void 0 ? _a : null;
|
|
437
|
+
valuesSetByClear[key] = newValue;
|
|
419
438
|
if (newValue !== oldValue) {
|
|
420
439
|
OnyxCache_1.default.set(key, newValue);
|
|
421
440
|
const collectionKey = OnyxKeys_1.default.getCollectionKey(key);
|
|
@@ -449,15 +468,33 @@ function clear(keysToPreserve = []) {
|
|
|
449
468
|
for (const key of keysToBeClearedFromStorage)
|
|
450
469
|
OnyxCache_1.default.drop(key);
|
|
451
470
|
return storage_1.default.removeItems(keysToBeClearedFromStorage)
|
|
452
|
-
.then(() =>
|
|
471
|
+
.then(() => {
|
|
472
|
+
// Save written keys again in case `removeItems` deleted them after they were stored.
|
|
473
|
+
const writtenKeyValuePairs = Object.keys(valuesSetByClear)
|
|
474
|
+
.filter((key) => !OnyxKeys_1.default.isRamOnlyKey(key) && wasWrittenDuringClear(key))
|
|
475
|
+
.map((key) => [key, OnyxCache_1.default.get(key)]);
|
|
476
|
+
return storage_1.default.multiSet([...defaultKeyValuePairs.filter(([key]) => !wasWrittenDuringClear(key)), ...writtenKeyValuePairs]);
|
|
477
|
+
})
|
|
453
478
|
.then(() => {
|
|
454
479
|
DevTools_1.default.clearState(keysToPreserve);
|
|
455
480
|
OnyxSubscriptionManager_1.default.resetCacheMissReports();
|
|
456
|
-
// Notify the subscribers for each key/value group so they can receive the new values
|
|
481
|
+
// Notify the subscribers for each key/value group so they can receive the new values.
|
|
482
|
+
// Keys written during the clear are skipped, their subscribers already have the newer value.
|
|
457
483
|
for (const [key, value] of Object.entries(keyValuesToResetIndividually)) {
|
|
484
|
+
if (wasWrittenDuringClear(key)) {
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
458
487
|
OnyxUtils_1.default.notifyKey(key, value);
|
|
459
488
|
}
|
|
460
489
|
for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
|
|
490
|
+
const writtenMemberKeys = Object.keys(value.newValues).filter(wasWrittenDuringClear);
|
|
491
|
+
for (const memberKey of writtenMemberKeys) {
|
|
492
|
+
delete value.newValues[memberKey];
|
|
493
|
+
delete value.oldValues[memberKey];
|
|
494
|
+
}
|
|
495
|
+
if (Object.keys(value.newValues).length === 0) {
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
461
498
|
OnyxUtils_1.default.notifyCollection(key, value.newValues, value.oldValues);
|
|
462
499
|
}
|
|
463
500
|
});
|
|
@@ -465,6 +502,8 @@ function clear(keysToPreserve = []) {
|
|
|
465
502
|
.then(() => undefined);
|
|
466
503
|
return OnyxCache_1.default.captureTask(OnyxCache_1.TASK.CLEAR, promise);
|
|
467
504
|
}));
|
|
505
|
+
clearPromise.then(removeWriteListener, removeWriteListener);
|
|
506
|
+
return clearPromise;
|
|
468
507
|
}
|
|
469
508
|
/**
|
|
470
509
|
* Insert API responses and lifecycle data into Onyx
|
package/dist/PendingWrites.d.ts
CHANGED
|
@@ -9,6 +9,10 @@ declare function trackPendingWrite<T>(keys: OnyxKey | OnyxKey[], pendingWrite: P
|
|
|
9
9
|
* Registers an in-flight write that affects every key (Onyx.clear). Deregistered once it settles.
|
|
10
10
|
*/
|
|
11
11
|
declare function trackPendingGlobalWrite<T>(pendingGlobalWrite: Promise<T>): Promise<T>;
|
|
12
|
+
/**
|
|
13
|
+
* Calls `listener` with the keys of every write issued from now on, until the returned function is called.
|
|
14
|
+
*/
|
|
15
|
+
declare function addWriteListener(listener: (keys: OnyxKey[]) => void): () => void;
|
|
12
16
|
/**
|
|
13
17
|
* In-flight writes that can change the value delivered to a subscriber of `key`: writes to the key
|
|
14
18
|
* itself, writes to any member when `key` is a collection root, and global writes (clear).
|
|
@@ -28,6 +32,7 @@ declare function clearPendingWrites(): void;
|
|
|
28
32
|
declare const _default: {
|
|
29
33
|
trackPendingWrite: typeof trackPendingWrite;
|
|
30
34
|
trackPendingGlobalWrite: typeof trackPendingGlobalWrite;
|
|
35
|
+
addWriteListener: typeof addWriteListener;
|
|
31
36
|
getPendingWritesForKey: typeof getPendingWritesForKey;
|
|
32
37
|
scheduleInitialSubscriberNotification: typeof scheduleInitialSubscriberNotification;
|
|
33
38
|
clearPendingWrites: typeof clearPendingWrites;
|
package/dist/PendingWrites.js
CHANGED
|
@@ -9,6 +9,8 @@ const OnyxKeys_1 = __importDefault(require("./OnyxKeys"));
|
|
|
9
9
|
const pendingWritesByKey = new Map();
|
|
10
10
|
// In-flight writes that affect every key (Onyx.clear). Any initial notification waits for these.
|
|
11
11
|
const pendingGlobalWrites = new Set();
|
|
12
|
+
// Told about every write as it is issued, so Onyx.clear can tell writes apart from reads refilling the cache.
|
|
13
|
+
const writeListeners = new Set();
|
|
12
14
|
/**
|
|
13
15
|
* Registers an in-flight write under each key it can change, so `scheduleInitialSubscriberNotification`
|
|
14
16
|
* waits only for the writes relevant to a connecting key. Returns the same promise so callers can wrap a
|
|
@@ -17,6 +19,9 @@ const pendingGlobalWrites = new Set();
|
|
|
17
19
|
function trackPendingWrite(keys, pendingWrite) {
|
|
18
20
|
// Drop nullish keys (e.g. a keyless `clear` item) so they never reach `getPendingWritesForKey`'s scan.
|
|
19
21
|
const keyList = (Array.isArray(keys) ? keys : [keys]).filter((key) => typeof key === 'string');
|
|
22
|
+
for (const listener of writeListeners) {
|
|
23
|
+
listener(keyList);
|
|
24
|
+
}
|
|
20
25
|
for (const key of keyList) {
|
|
21
26
|
let pendingWritesForKey = pendingWritesByKey.get(key);
|
|
22
27
|
if (!pendingWritesForKey) {
|
|
@@ -49,6 +54,13 @@ function trackPendingGlobalWrite(pendingGlobalWrite) {
|
|
|
49
54
|
pendingGlobalWrite.then(deregister, deregister);
|
|
50
55
|
return pendingGlobalWrite;
|
|
51
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Calls `listener` with the keys of every write issued from now on, until the returned function is called.
|
|
59
|
+
*/
|
|
60
|
+
function addWriteListener(listener) {
|
|
61
|
+
writeListeners.add(listener);
|
|
62
|
+
return () => writeListeners.delete(listener);
|
|
63
|
+
}
|
|
52
64
|
/**
|
|
53
65
|
* In-flight writes that can change the value delivered to a subscriber of `key`: writes to the key
|
|
54
66
|
* itself, writes to any member when `key` is a collection root, and global writes (clear).
|
|
@@ -93,10 +105,12 @@ function scheduleInitialSubscriberNotification(key, notify) {
|
|
|
93
105
|
function clearPendingWrites() {
|
|
94
106
|
pendingWritesByKey.clear();
|
|
95
107
|
pendingGlobalWrites.clear();
|
|
108
|
+
writeListeners.clear();
|
|
96
109
|
}
|
|
97
110
|
exports.default = {
|
|
98
111
|
trackPendingWrite,
|
|
99
112
|
trackPendingGlobalWrite,
|
|
113
|
+
addWriteListener,
|
|
100
114
|
getPendingWritesForKey,
|
|
101
115
|
scheduleInitialSubscriberNotification,
|
|
102
116
|
clearPendingWrites,
|