react-native-onyx 2.0.47 → 2.0.48
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 +3 -3
- package/dist/useOnyx.js +15 -11
- package/package.json +1 -1
package/dist/Onyx.js
CHANGED
|
@@ -501,7 +501,7 @@ function clear(keysToPreserve = []) {
|
|
|
501
501
|
// since collection key subscribers need to be updated differently
|
|
502
502
|
if (!isKeyToPreserve) {
|
|
503
503
|
const oldValue = OnyxCache_1.default.get(key);
|
|
504
|
-
const newValue = (_a = defaultKeyStates[key]) !== null && _a !== void 0 ? _a :
|
|
504
|
+
const newValue = (_a = defaultKeyStates[key]) !== null && _a !== void 0 ? _a : null;
|
|
505
505
|
if (newValue !== oldValue) {
|
|
506
506
|
OnyxCache_1.default.set(key, newValue);
|
|
507
507
|
const collectionKey = key.substring(0, key.indexOf('_') + 1);
|
|
@@ -509,10 +509,10 @@ function clear(keysToPreserve = []) {
|
|
|
509
509
|
if (!keyValuesToResetAsCollection[collectionKey]) {
|
|
510
510
|
keyValuesToResetAsCollection[collectionKey] = {};
|
|
511
511
|
}
|
|
512
|
-
keyValuesToResetAsCollection[collectionKey][key] = newValue;
|
|
512
|
+
keyValuesToResetAsCollection[collectionKey][key] = newValue !== null && newValue !== void 0 ? newValue : undefined;
|
|
513
513
|
}
|
|
514
514
|
else {
|
|
515
|
-
keyValuesToResetIndividually[key] = newValue;
|
|
515
|
+
keyValuesToResetIndividually[key] = newValue !== null && newValue !== void 0 ? newValue : undefined;
|
|
516
516
|
}
|
|
517
517
|
}
|
|
518
518
|
}
|
package/dist/useOnyx.js
CHANGED
|
@@ -9,8 +9,10 @@ const OnyxUtils_1 = __importDefault(require("./OnyxUtils"));
|
|
|
9
9
|
const useLiveRef_1 = __importDefault(require("./useLiveRef"));
|
|
10
10
|
const usePrevious_1 = __importDefault(require("./usePrevious"));
|
|
11
11
|
const Onyx_1 = __importDefault(require("./Onyx"));
|
|
12
|
+
const OnyxCache_1 = __importDefault(require("./OnyxCache"));
|
|
12
13
|
function getCachedValue(key, selector) {
|
|
13
|
-
|
|
14
|
+
var _a;
|
|
15
|
+
return ((_a = OnyxUtils_1.default.tryGetCachedValue(key, { selector })) !== null && _a !== void 0 ? _a : undefined);
|
|
14
16
|
}
|
|
15
17
|
function useOnyx(key, options) {
|
|
16
18
|
const connectionIDRef = (0, react_1.useRef)(null);
|
|
@@ -18,8 +20,8 @@ function useOnyx(key, options) {
|
|
|
18
20
|
// Used to stabilize the selector reference and avoid unnecessary calls to `getSnapshot()`.
|
|
19
21
|
const selectorRef = (0, useLiveRef_1.default)(options === null || options === void 0 ? void 0 : options.selector);
|
|
20
22
|
// Stores the previous cached value as it's necessary to compare with the new value in `getSnapshot()`.
|
|
21
|
-
// We initialize it to `
|
|
22
|
-
const cachedValueRef = (0, react_1.useRef)(
|
|
23
|
+
// We initialize it to `null` to simulate that we don't have any value from cache yet.
|
|
24
|
+
const cachedValueRef = (0, react_1.useRef)(null);
|
|
23
25
|
// Stores the previously result returned by the hook, containing the data from cache and the fetch status.
|
|
24
26
|
// We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache.
|
|
25
27
|
// However, if `initWithStoredValues` is `true` we set the fetch status to `loaded` since we want to signal that data is ready.
|
|
@@ -50,13 +52,14 @@ function useOnyx(key, options) {
|
|
|
50
52
|
throw new Error(`'${previousKey}' key can't be changed to '${key}'. useOnyx() only supports dynamic keys if they are both collection member keys from the same collection e.g. from 'collection_id1' to 'collection_id2'.`);
|
|
51
53
|
}, [previousKey, key]);
|
|
52
54
|
const getSnapshot = (0, react_1.useCallback)(() => {
|
|
53
|
-
var _a;
|
|
55
|
+
var _a, _b;
|
|
54
56
|
// We get the value from the cache, supplying a selector too in case it's defined.
|
|
55
57
|
// If `newValue` is `undefined` it means that the cache doesn't have a value for that key yet.
|
|
56
|
-
// If `newValue` is `null` or any other value
|
|
58
|
+
// If `newValue` is `null` or any other value it means that the cache does have a value for that key.
|
|
57
59
|
// This difference between `undefined` and other values is crucial and it's used to address the following
|
|
58
60
|
// conditions and use cases.
|
|
59
61
|
let newValue = getCachedValue(key, selectorRef.current);
|
|
62
|
+
const hasCacheForKey = OnyxCache_1.default.hasCacheForKey(key);
|
|
60
63
|
// Since the fetch status can be different given the use cases below, we define the variable right away.
|
|
61
64
|
let newFetchStatus;
|
|
62
65
|
// If we have pending merge operations for the key during the first connection, we set the new value to `undefined`
|
|
@@ -66,18 +69,19 @@ function useOnyx(key, options) {
|
|
|
66
69
|
newValue = undefined;
|
|
67
70
|
newFetchStatus = 'loading';
|
|
68
71
|
}
|
|
69
|
-
// If data is not present in cache
|
|
72
|
+
// If data is not present in cache and `initialValue` is set during the first connection,
|
|
70
73
|
// we set the new value to `initialValue` and fetch status to `loaded` since we already have some data to return to the consumer.
|
|
71
|
-
if (isFirstConnectionRef.current &&
|
|
72
|
-
newValue = options === null || options === void 0 ? void 0 : options.initialValue;
|
|
74
|
+
if (isFirstConnectionRef.current && !hasCacheForKey && (options === null || options === void 0 ? void 0 : options.initialValue) !== undefined) {
|
|
75
|
+
newValue = ((_a = options === null || options === void 0 ? void 0 : options.initialValue) !== null && _a !== void 0 ? _a : undefined);
|
|
73
76
|
newFetchStatus = 'loaded';
|
|
74
77
|
}
|
|
75
78
|
// If the previously cached value is different from the new value, we update both cached value
|
|
76
79
|
// and the result to be returned by the hook.
|
|
77
|
-
|
|
80
|
+
// If the cache was set for the first time, we also update the cached value and the result.
|
|
81
|
+
const isCacheSetFirstTime = cachedValueRef.current === null && hasCacheForKey;
|
|
82
|
+
if (isCacheSetFirstTime || !(0, fast_equals_1.deepEqual)((_b = cachedValueRef.current) !== null && _b !== void 0 ? _b : undefined, newValue)) {
|
|
78
83
|
cachedValueRef.current = newValue;
|
|
79
|
-
|
|
80
|
-
resultRef.current = [((_a = cachedValueRef.current) !== null && _a !== void 0 ? _a : undefined), { status: newFetchStatus !== null && newFetchStatus !== void 0 ? newFetchStatus : 'loaded' }];
|
|
84
|
+
resultRef.current = [cachedValueRef.current, { status: newFetchStatus !== null && newFetchStatus !== void 0 ? newFetchStatus : 'loaded' }];
|
|
81
85
|
}
|
|
82
86
|
return resultRef.current;
|
|
83
87
|
}, [key, selectorRef, options === null || options === void 0 ? void 0 : options.allowStaleData, options === null || options === void 0 ? void 0 : options.initialValue]);
|