react-native-onyx 2.0.51 → 2.0.52
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.d.ts +1 -1
- package/dist/useOnyx.js +44 -18
- package/package.json +1 -1
package/dist/useOnyx.d.ts
CHANGED
|
@@ -38,4 +38,4 @@ type UseOnyxResult<TKey extends OnyxKey, TValue> = [CachedValue<TKey, TValue>, R
|
|
|
38
38
|
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue>>): UseOnyxResult<TKey, TReturnValue>;
|
|
39
39
|
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<NoInfer<TReturnValue>>): UseOnyxResult<TKey, TReturnValue>;
|
|
40
40
|
export default useOnyx;
|
|
41
|
-
export type {
|
|
41
|
+
export type { FetchStatus, ResultMetadata, UseOnyxResult };
|
package/dist/useOnyx.js
CHANGED
|
@@ -5,11 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const fast_equals_1 = require("fast-equals");
|
|
7
7
|
const react_1 = require("react");
|
|
8
|
+
const Onyx_1 = __importDefault(require("./Onyx"));
|
|
9
|
+
const OnyxCache_1 = __importDefault(require("./OnyxCache"));
|
|
8
10
|
const OnyxUtils_1 = __importDefault(require("./OnyxUtils"));
|
|
9
11
|
const useLiveRef_1 = __importDefault(require("./useLiveRef"));
|
|
10
12
|
const usePrevious_1 = __importDefault(require("./usePrevious"));
|
|
11
|
-
const Onyx_1 = __importDefault(require("./Onyx"));
|
|
12
|
-
const OnyxCache_1 = __importDefault(require("./OnyxCache"));
|
|
13
13
|
function getCachedValue(key, selector) {
|
|
14
14
|
var _a;
|
|
15
15
|
return ((_a = OnyxUtils_1.default.tryGetCachedValue(key, { selector })) !== null && _a !== void 0 ? _a : undefined);
|
|
@@ -21,7 +21,9 @@ function useOnyx(key, options) {
|
|
|
21
21
|
const selectorRef = (0, useLiveRef_1.default)(options === null || options === void 0 ? void 0 : options.selector);
|
|
22
22
|
// Stores the previous cached value as it's necessary to compare with the new value in `getSnapshot()`.
|
|
23
23
|
// We initialize it to `null` to simulate that we don't have any value from cache yet.
|
|
24
|
-
const
|
|
24
|
+
const previousValueRef = (0, react_1.useRef)(null);
|
|
25
|
+
// Stores the newest cached value in order to compare with the previous one and optimize `getSnapshot()` execution.
|
|
26
|
+
const newValueRef = (0, react_1.useRef)(null);
|
|
25
27
|
// Stores the previously result returned by the hook, containing the data from cache and the fetch status.
|
|
26
28
|
// We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache.
|
|
27
29
|
// However, if `initWithStoredValues` is `true` we set the fetch status to `loaded` since we want to signal that data is ready.
|
|
@@ -34,6 +36,8 @@ function useOnyx(key, options) {
|
|
|
34
36
|
// Indicates if it's the first Onyx connection of this hook or not, as we don't want certain use cases
|
|
35
37
|
// in `getSnapshot()` to be satisfied several times.
|
|
36
38
|
const isFirstConnectionRef = (0, react_1.useRef)(true);
|
|
39
|
+
// Indicates if we should get the newest cached value from Onyx during `getSnapshot()` execution.
|
|
40
|
+
const shouldGetCachedValueRef = (0, react_1.useRef)(true);
|
|
37
41
|
(0, react_1.useEffect)(() => {
|
|
38
42
|
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
|
|
39
43
|
if (previousKey === key) {
|
|
@@ -52,13 +56,20 @@ function useOnyx(key, options) {
|
|
|
52
56
|
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'.`);
|
|
53
57
|
}, [previousKey, key]);
|
|
54
58
|
const getSnapshot = (0, react_1.useCallback)(() => {
|
|
55
|
-
var _a, _b;
|
|
56
|
-
// We get the value from
|
|
57
|
-
//
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
var _a, _b, _c;
|
|
60
|
+
// We get the value from cache while the first connection to Onyx is being made,
|
|
61
|
+
// so we can return any cached value right away. After the connection is made, we only
|
|
62
|
+
// update `newValueRef` when `Onyx.connect()` callback is fired.
|
|
63
|
+
if (isFirstConnectionRef.current || shouldGetCachedValueRef.current) {
|
|
64
|
+
// If `newValueRef.current` is `undefined` it means that the cache doesn't have a value for that key yet.
|
|
65
|
+
// If `newValueRef.current` is `null` or any other value it means that the cache does have a value for that key.
|
|
66
|
+
// This difference between `undefined` and other values is crucial and it's used to address the following
|
|
67
|
+
// conditions and use cases.
|
|
68
|
+
newValueRef.current = getCachedValue(key, selectorRef.current);
|
|
69
|
+
// We set this flag to `false` again since we don't want to get the newest cached value every time `getSnapshot()` is executed,
|
|
70
|
+
// and only when `Onyx.connect()` callback is fired.
|
|
71
|
+
shouldGetCachedValueRef.current = false;
|
|
72
|
+
}
|
|
62
73
|
const hasCacheForKey = OnyxCache_1.default.hasCacheForKey(key);
|
|
63
74
|
// Since the fetch status can be different given the use cases below, we define the variable right away.
|
|
64
75
|
let newFetchStatus;
|
|
@@ -66,22 +77,34 @@ function useOnyx(key, options) {
|
|
|
66
77
|
// and fetch status to `loading` to simulate that it is still being loaded until we have the most updated data.
|
|
67
78
|
// If `allowStaleData` is `true` this logic will be ignored and cached value will be used, even if it's stale data.
|
|
68
79
|
if (isFirstConnectionRef.current && OnyxUtils_1.default.hasPendingMergeForKey(key) && !(options === null || options === void 0 ? void 0 : options.allowStaleData)) {
|
|
69
|
-
|
|
80
|
+
newValueRef.current = undefined;
|
|
70
81
|
newFetchStatus = 'loading';
|
|
71
82
|
}
|
|
72
83
|
// If data is not present in cache and `initialValue` is set during the first connection,
|
|
73
84
|
// we set the new value to `initialValue` and fetch status to `loaded` since we already have some data to return to the consumer.
|
|
74
85
|
if (isFirstConnectionRef.current && !hasCacheForKey && (options === null || options === void 0 ? void 0 : options.initialValue) !== undefined) {
|
|
75
|
-
|
|
86
|
+
newValueRef.current = ((_a = options === null || options === void 0 ? void 0 : options.initialValue) !== null && _a !== void 0 ? _a : undefined);
|
|
76
87
|
newFetchStatus = 'loaded';
|
|
77
88
|
}
|
|
89
|
+
// We do a deep equality check if we are subscribed to a collection key and `selector` is defined,
|
|
90
|
+
// since each `OnyxUtils.tryGetCachedValue()` call will generate a plain new collection object with new records as well,
|
|
91
|
+
// all of them created using the `selector` function.
|
|
92
|
+
// For the other cases we will only deal with object reference checks, so just a shallow equality check is enough.
|
|
93
|
+
let areValuesEqual;
|
|
94
|
+
if (OnyxUtils_1.default.isCollectionKey(key) && selectorRef.current) {
|
|
95
|
+
areValuesEqual = (0, fast_equals_1.deepEqual)((_b = previousValueRef.current) !== null && _b !== void 0 ? _b : undefined, newValueRef.current);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
areValuesEqual = (0, fast_equals_1.shallowEqual)((_c = previousValueRef.current) !== null && _c !== void 0 ? _c : undefined, newValueRef.current);
|
|
99
|
+
}
|
|
78
100
|
// If the previously cached value is different from the new value, we update both cached value
|
|
79
101
|
// and the result to be returned by the hook.
|
|
80
102
|
// If the cache was set for the first time, we also update the cached value and the result.
|
|
81
|
-
const isCacheSetFirstTime =
|
|
82
|
-
if (isCacheSetFirstTime || !
|
|
83
|
-
|
|
84
|
-
|
|
103
|
+
const isCacheSetFirstTime = previousValueRef.current === null && hasCacheForKey;
|
|
104
|
+
if (isCacheSetFirstTime || !areValuesEqual) {
|
|
105
|
+
previousValueRef.current = newValueRef.current;
|
|
106
|
+
// If the new value is `null` we default it to `undefined` to ensure the consumer gets a consistent result from the hook.
|
|
107
|
+
resultRef.current = [previousValueRef.current, { status: newFetchStatus !== null && newFetchStatus !== void 0 ? newFetchStatus : 'loaded' }];
|
|
85
108
|
}
|
|
86
109
|
return resultRef.current;
|
|
87
110
|
}, [key, selectorRef, options === null || options === void 0 ? void 0 : options.allowStaleData, options === null || options === void 0 ? void 0 : options.initialValue]);
|
|
@@ -89,9 +112,12 @@ function useOnyx(key, options) {
|
|
|
89
112
|
connectionIDRef.current = Onyx_1.default.connect({
|
|
90
113
|
key,
|
|
91
114
|
callback: () => {
|
|
92
|
-
//
|
|
93
|
-
//
|
|
115
|
+
// Signals that the first connection was made, so some logics in `getSnapshot()`
|
|
116
|
+
// won't be executed anymore.
|
|
94
117
|
isFirstConnectionRef.current = false;
|
|
118
|
+
// Signals that we want to get the newest cached value again in `getSnapshot()`.
|
|
119
|
+
shouldGetCachedValueRef.current = true;
|
|
120
|
+
// Finally, we signal that the store changed, making `getSnapshot()` be called again.
|
|
95
121
|
onStoreChange();
|
|
96
122
|
},
|
|
97
123
|
initWithStoredValues: options === null || options === void 0 ? void 0 : options.initWithStoredValues,
|