react-native-onyx 2.0.98 → 2.0.100
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 +6 -6
- package/dist/useOnyx.d.ts +3 -2
- package/dist/useOnyx.js +12 -2
- package/package.json +1 -1
package/dist/Onyx.js
CHANGED
|
@@ -543,7 +543,7 @@ function clear(keysToPreserve = []) {
|
|
|
543
543
|
function updateSnapshots(data) {
|
|
544
544
|
const snapshotCollectionKey = OnyxUtils_1.default.getSnapshotKey();
|
|
545
545
|
if (!snapshotCollectionKey)
|
|
546
|
-
return;
|
|
546
|
+
return [];
|
|
547
547
|
const promises = [];
|
|
548
548
|
const snapshotCollection = OnyxUtils_1.default.getCachedCollection(snapshotCollectionKey);
|
|
549
549
|
const snapshotCollectionKeyLength = snapshotCollectionKey.length;
|
|
@@ -583,7 +583,7 @@ function updateSnapshots(data) {
|
|
|
583
583
|
}
|
|
584
584
|
promises.push(() => merge(snapshotKey, { data: updatedData }));
|
|
585
585
|
});
|
|
586
|
-
return
|
|
586
|
+
return promises;
|
|
587
587
|
}
|
|
588
588
|
/**
|
|
589
589
|
* Insert API responses and lifecycle data into Onyx
|
|
@@ -699,10 +699,10 @@ function update(data) {
|
|
|
699
699
|
promises.push(() => merge(key, batchedChanges));
|
|
700
700
|
}
|
|
701
701
|
});
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
702
|
+
const snapshotPromises = updateSnapshots(data);
|
|
703
|
+
// We need to run the snapshot updates before the other updates so the snapshot data can be updated before the loading state in the snapshot
|
|
704
|
+
const finalPromises = snapshotPromises.concat(promises);
|
|
705
|
+
return clearPromise.then(() => Promise.all(finalPromises.map((p) => p()))).then(() => undefined);
|
|
706
706
|
}
|
|
707
707
|
/**
|
|
708
708
|
* Sets a collection by replacing all existing collection members with new values.
|
package/dist/useOnyx.d.ts
CHANGED
|
@@ -49,10 +49,11 @@ type UseOnyxSelectorOption<TKey extends OnyxKey, TReturnValue> = {
|
|
|
49
49
|
};
|
|
50
50
|
type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & UseOnyxSelectorOption<TKey, TReturnValue>;
|
|
51
51
|
type FetchStatus = 'loading' | 'loaded';
|
|
52
|
-
type ResultMetadata = {
|
|
52
|
+
type ResultMetadata<TValue> = {
|
|
53
53
|
status: FetchStatus;
|
|
54
|
+
sourceValue?: NonNullable<TValue> | undefined;
|
|
54
55
|
};
|
|
55
|
-
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata];
|
|
56
|
+
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata<TValue>];
|
|
56
57
|
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue>>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>;
|
|
57
58
|
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<NoInfer<TReturnValue>>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>;
|
|
58
59
|
export default useOnyx;
|
package/dist/useOnyx.js
CHANGED
|
@@ -87,6 +87,8 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
87
87
|
const onStoreChangeFnRef = (0, react_1.useRef)(null);
|
|
88
88
|
// Indicates if we should get the newest cached value from Onyx during `getSnapshot()` execution.
|
|
89
89
|
const shouldGetCachedValueRef = (0, react_1.useRef)(true);
|
|
90
|
+
// Inside useOnyx.ts, we need to track the sourceValue separately
|
|
91
|
+
const sourceValueRef = (0, react_1.useRef)(undefined);
|
|
90
92
|
(0, react_1.useEffect)(() => {
|
|
91
93
|
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
|
|
92
94
|
if ((options === null || options === void 0 ? void 0 : options.allowDynamicKey) || previousKey === key) {
|
|
@@ -191,7 +193,13 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
191
193
|
previousValueRef.current = newValueRef.current;
|
|
192
194
|
// If the new value is `null` we default it to `undefined` to ensure the consumer gets a consistent result from the hook.
|
|
193
195
|
const newStatus = newFetchStatus !== null && newFetchStatus !== void 0 ? newFetchStatus : 'loaded';
|
|
194
|
-
resultRef.current = [
|
|
196
|
+
resultRef.current = [
|
|
197
|
+
(_c = previousValueRef.current) !== null && _c !== void 0 ? _c : undefined,
|
|
198
|
+
{
|
|
199
|
+
status: newStatus,
|
|
200
|
+
sourceValue: sourceValueRef.current,
|
|
201
|
+
},
|
|
202
|
+
];
|
|
195
203
|
// If `canBeMissing` is set to `false` and the Onyx value of that key is not defined,
|
|
196
204
|
// we log an alert so it can be acknowledged by the consumer. Additionally, we won't log alerts
|
|
197
205
|
// if there's a `Onyx.clear()` task in progress.
|
|
@@ -206,7 +214,7 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
206
214
|
onStoreChangeFnRef.current = onStoreChange;
|
|
207
215
|
connectionRef.current = OnyxConnectionManager_1.default.connect({
|
|
208
216
|
key,
|
|
209
|
-
callback: () => {
|
|
217
|
+
callback: (value, callbackKey, sourceValue) => {
|
|
210
218
|
isConnectingRef.current = false;
|
|
211
219
|
onStoreChangeFnRef.current = onStoreChange;
|
|
212
220
|
// Signals that the first connection was made, so some logics in `getSnapshot()`
|
|
@@ -214,6 +222,8 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
214
222
|
isFirstConnectionRef.current = false;
|
|
215
223
|
// Signals that we want to get the newest cached value again in `getSnapshot()`.
|
|
216
224
|
shouldGetCachedValueRef.current = true;
|
|
225
|
+
// sourceValue is unknown type, so we need to cast it to the correct type.
|
|
226
|
+
sourceValueRef.current = sourceValue;
|
|
217
227
|
// Finally, we signal that the store changed, making `getSnapshot()` be called again.
|
|
218
228
|
onStoreChange();
|
|
219
229
|
},
|