react-native-onyx 2.0.83 → 2.0.84
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 +3 -2
- package/dist/useOnyx.js +22 -1
- package/package.json +1 -1
package/dist/useOnyx.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DependencyList } from 'react';
|
|
1
2
|
import type { OnyxKey, OnyxValue } from './types';
|
|
2
3
|
type BaseUseOnyxOptions = {
|
|
3
4
|
/**
|
|
@@ -40,7 +41,7 @@ type ResultMetadata = {
|
|
|
40
41
|
status: FetchStatus;
|
|
41
42
|
};
|
|
42
43
|
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata];
|
|
43
|
-
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue
|
|
44
|
-
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<NoInfer<TReturnValue
|
|
44
|
+
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue>>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>;
|
|
45
|
+
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<NoInfer<TReturnValue>>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>;
|
|
45
46
|
export default useOnyx;
|
|
46
47
|
export type { FetchStatus, ResultMetadata, UseOnyxResult };
|
package/dist/useOnyx.js
CHANGED
|
@@ -64,7 +64,7 @@ function getCachedValue(key, selector) {
|
|
|
64
64
|
const selectedValue = selector ? selector(value) : value;
|
|
65
65
|
return selectedValue !== null && selectedValue !== void 0 ? selectedValue : undefined;
|
|
66
66
|
}
|
|
67
|
-
function useOnyx(key, options) {
|
|
67
|
+
function useOnyx(key, options, dependencies = []) {
|
|
68
68
|
const connectionRef = (0, react_1.useRef)(null);
|
|
69
69
|
const previousKey = (0, usePrevious_1.default)(key);
|
|
70
70
|
// Used to stabilize the selector reference and avoid unnecessary calls to `getSnapshot()`.
|
|
@@ -86,6 +86,10 @@ function useOnyx(key, options) {
|
|
|
86
86
|
// Indicates if it's the first Onyx connection of this hook or not, as we don't want certain use cases
|
|
87
87
|
// in `getSnapshot()` to be satisfied several times.
|
|
88
88
|
const isFirstConnectionRef = (0, react_1.useRef)(true);
|
|
89
|
+
// Indicates if the hook is connecting to a Onyx key.
|
|
90
|
+
const isConnectingRef = (0, react_1.useRef)(false);
|
|
91
|
+
// Stores the `onStoreChange()` function, which can be used to trigger a `getSnapshot()` update when desired.
|
|
92
|
+
const onStoreChangeFnRef = (0, react_1.useRef)(null);
|
|
89
93
|
// Indicates if we should get the newest cached value from Onyx during `getSnapshot()` execution.
|
|
90
94
|
const shouldGetCachedValueRef = (0, react_1.useRef)(true);
|
|
91
95
|
(0, react_1.useEffect)(() => {
|
|
@@ -107,6 +111,17 @@ function useOnyx(key, options) {
|
|
|
107
111
|
}
|
|
108
112
|
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'.`);
|
|
109
113
|
}, [previousKey, key]);
|
|
114
|
+
(0, react_1.useEffect)(() => {
|
|
115
|
+
// This effect will only run if the `dependencies` array changes. If it changes it will force the hook
|
|
116
|
+
// to trigger a `getSnapshot()` update by calling the stored `onStoreChange()` function reference, thus
|
|
117
|
+
// re-running the hook and returning the latest value to the consumer.
|
|
118
|
+
if (connectionRef.current === null || isConnectingRef.current || !onStoreChangeFnRef.current) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
shouldGetCachedValueRef.current = true;
|
|
122
|
+
onStoreChangeFnRef.current();
|
|
123
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
124
|
+
}, [...dependencies]);
|
|
110
125
|
// Mimics withOnyx's checkEvictableKeys() behavior.
|
|
111
126
|
const checkEvictableKey = (0, react_1.useCallback)(() => {
|
|
112
127
|
if ((options === null || options === void 0 ? void 0 : options.canEvict) === undefined || !connectionRef.current) {
|
|
@@ -182,9 +197,13 @@ function useOnyx(key, options) {
|
|
|
182
197
|
return resultRef.current;
|
|
183
198
|
}, [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.initialValue, key, selectorRef]);
|
|
184
199
|
const subscribe = (0, react_1.useCallback)((onStoreChange) => {
|
|
200
|
+
isConnectingRef.current = true;
|
|
201
|
+
onStoreChangeFnRef.current = onStoreChange;
|
|
185
202
|
connectionRef.current = OnyxConnectionManager_1.default.connect({
|
|
186
203
|
key,
|
|
187
204
|
callback: () => {
|
|
205
|
+
isConnectingRef.current = false;
|
|
206
|
+
onStoreChangeFnRef.current = onStoreChange;
|
|
188
207
|
// Signals that the first connection was made, so some logics in `getSnapshot()`
|
|
189
208
|
// won't be executed anymore.
|
|
190
209
|
isFirstConnectionRef.current = false;
|
|
@@ -204,6 +223,8 @@ function useOnyx(key, options) {
|
|
|
204
223
|
}
|
|
205
224
|
OnyxConnectionManager_1.default.disconnect(connectionRef.current);
|
|
206
225
|
isFirstConnectionRef.current = false;
|
|
226
|
+
isConnectingRef.current = false;
|
|
227
|
+
onStoreChangeFnRef.current = null;
|
|
207
228
|
};
|
|
208
229
|
}, [key, options === null || options === void 0 ? void 0 : options.initWithStoredValues, options === null || options === void 0 ? void 0 : options.reuseConnection, checkEvictableKey]);
|
|
209
230
|
(0, react_1.useEffect)(() => {
|