react-native-onyx 3.0.23 → 3.0.25
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/useOnyx.js +12 -1
- package/package.json +1 -1
package/dist/useOnyx.js
CHANGED
|
@@ -86,6 +86,7 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
86
86
|
const previousValueRef = (0, react_1.useRef)(null);
|
|
87
87
|
// Stores the newest cached value in order to compare with the previous one and optimize `getSnapshot()` execution.
|
|
88
88
|
const newValueRef = (0, react_1.useRef)(null);
|
|
89
|
+
const lastConnectedKeyRef = (0, react_1.useRef)(key);
|
|
89
90
|
// Stores the previously result returned by the hook, containing the data from cache and the fetch status.
|
|
90
91
|
// We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache.
|
|
91
92
|
// However, if `initWithStoredValues` is `false` we set the fetch status to `loaded` since we want to signal that data is ready.
|
|
@@ -114,6 +115,15 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
114
115
|
canBeMissing: options === null || options === void 0 ? void 0 : options.canBeMissing,
|
|
115
116
|
}), [options === null || options === void 0 ? void 0 : options.selector, options === null || options === void 0 ? void 0 : options.initWithStoredValues, options === null || options === void 0 ? void 0 : options.allowStaleData, options === null || options === void 0 ? void 0 : options.canBeMissing]);
|
|
116
117
|
(0, react_1.useEffect)(() => () => OnyxSnapshotCache_1.default.deregisterConsumer(key, cacheKey), [key, cacheKey]);
|
|
118
|
+
(0, react_1.useEffect)(() => {
|
|
119
|
+
if (lastConnectedKeyRef.current === key) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
lastConnectedKeyRef.current = key;
|
|
123
|
+
shouldGetCachedValueRef.current = true;
|
|
124
|
+
previousValueRef.current = null;
|
|
125
|
+
resultRef.current = [undefined, { status: (options === null || options === void 0 ? void 0 : options.initWithStoredValues) === false ? 'loaded' : 'loading' }];
|
|
126
|
+
}, [key, options === null || options === void 0 ? void 0 : options.initWithStoredValues]);
|
|
117
127
|
(0, react_1.useEffect)(() => {
|
|
118
128
|
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
|
|
119
129
|
if ((options === null || options === void 0 ? void 0 : options.allowDynamicKey) || previousKey === key) {
|
|
@@ -229,8 +239,9 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
229
239
|
// - The previously cached value is different from the new value.
|
|
230
240
|
// - The previously cached value is `null` (not set from cache yet) and we have cache for this key
|
|
231
241
|
// OR we have a pending `Onyx.clear()` task (if `Onyx.clear()` is running cache might not be available anymore
|
|
242
|
+
// OR the subscriber is triggered (the value is gotten from the storage)
|
|
232
243
|
// so we update the cached value/result right away in order to prevent infinite loading state issues).
|
|
233
|
-
const shouldUpdateResult = !areValuesEqual || (previousValueRef.current === null && (hasCacheForKey || OnyxCache_1.default.hasPendingTask(OnyxCache_1.TASK.CLEAR)));
|
|
244
|
+
const shouldUpdateResult = !areValuesEqual || (previousValueRef.current === null && (hasCacheForKey || OnyxCache_1.default.hasPendingTask(OnyxCache_1.TASK.CLEAR) || !isFirstConnectionRef.current));
|
|
234
245
|
if (shouldUpdateResult) {
|
|
235
246
|
previousValueRef.current = newValueRef.current;
|
|
236
247
|
// If the new value is `null` we default it to `undefined` to ensure the consumer gets a consistent result from the hook.
|