react-native-onyx 3.0.37 → 3.0.38
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/README.md +0 -15
- package/dist/OnyxSnapshotCache.d.ts +1 -2
- package/dist/OnyxSnapshotCache.js +2 -4
- package/dist/useOnyx.d.ts +0 -7
- package/dist/useOnyx.js +2 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -206,21 +206,6 @@ export default App;
|
|
|
206
206
|
|
|
207
207
|
It's also beneficial to use a [selector](https://github.com/Expensify/react-native-onyx/blob/main/API.md#connectmapping--number) with the mapping in case you need to grab a single item in a collection (like a single report action).
|
|
208
208
|
|
|
209
|
-
### useOnyx()'s `canBeMissing` option
|
|
210
|
-
|
|
211
|
-
You must pass the `canBeMissing` configuration flag to `useOnyx` if you want the hook to log an alert when data is missing from Onyx store. Regarding usage in `Expensify/App` repo, if the component calling this is the one loading the data by calling an action, then you should set this to `true`. If the component calling this does not load the data then you should set it to `false`, which means that if the data is not there, it will log an alert, as it means we are using data that no one loaded and that's most probably a bug.
|
|
212
|
-
|
|
213
|
-
```javascript
|
|
214
|
-
const Component = ({reportID}) => {
|
|
215
|
-
// This hook will log an alert (via `Logger.logAlert()`) if `report` is `undefined`.
|
|
216
|
-
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {canBeMissing: false});
|
|
217
|
-
|
|
218
|
-
// rest of the component's code.
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
export default Component;
|
|
222
|
-
```
|
|
223
|
-
|
|
224
209
|
## Collections
|
|
225
210
|
|
|
226
211
|
Collections allow keys with similar value types to be subscribed together by subscribing to the collection key. To define one, it must be included in the `ONYXKEYS.COLLECTION` object and it must be suffixed with an underscore. Member keys should use a unique identifier or index after the collection key prefix (e.g. `report_42`).
|
|
@@ -40,12 +40,11 @@ declare class OnyxSnapshotCache {
|
|
|
40
40
|
* - `selector`: Different selectors produce different results, so each selector needs its own cache entry
|
|
41
41
|
* - `initWithStoredValues`: This flag changes the initial loading behavior and affects the returned fetch status
|
|
42
42
|
* - `allowStaleData`: Controls whether stale data can be returned during pending merges, affecting result timing
|
|
43
|
-
* - `canBeMissing`: Determines logging behavior for missing data, but doesn't affect the actual data returned
|
|
44
43
|
*
|
|
45
44
|
* Other options like `canEvict`, `reuseConnection`, and `allowDynamicKey` don't affect the data transformation
|
|
46
45
|
* or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
|
|
47
46
|
*/
|
|
48
|
-
registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector' | 'initWithStoredValues' | 'allowStaleData'
|
|
47
|
+
registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector' | 'initWithStoredValues' | 'allowStaleData'>): string;
|
|
49
48
|
/**
|
|
50
49
|
* Deregister a consumer for a cache key.
|
|
51
50
|
* Decrements reference counter and removes cache entry if no consumers remain.
|
|
@@ -37,19 +37,17 @@ class OnyxSnapshotCache {
|
|
|
37
37
|
* - `selector`: Different selectors produce different results, so each selector needs its own cache entry
|
|
38
38
|
* - `initWithStoredValues`: This flag changes the initial loading behavior and affects the returned fetch status
|
|
39
39
|
* - `allowStaleData`: Controls whether stale data can be returned during pending merges, affecting result timing
|
|
40
|
-
* - `canBeMissing`: Determines logging behavior for missing data, but doesn't affect the actual data returned
|
|
41
40
|
*
|
|
42
41
|
* Other options like `canEvict`, `reuseConnection`, and `allowDynamicKey` don't affect the data transformation
|
|
43
42
|
* or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
|
|
44
43
|
*/
|
|
45
44
|
registerConsumer(options) {
|
|
46
|
-
var _a, _b
|
|
45
|
+
var _a, _b;
|
|
47
46
|
const selectorID = (options === null || options === void 0 ? void 0 : options.selector) ? this.getSelectorID(options.selector) : 'no_selector';
|
|
48
47
|
// Create options hash without expensive JSON.stringify
|
|
49
48
|
const initWithStoredValues = (_a = options === null || options === void 0 ? void 0 : options.initWithStoredValues) !== null && _a !== void 0 ? _a : true;
|
|
50
49
|
const allowStaleData = (_b = options === null || options === void 0 ? void 0 : options.allowStaleData) !== null && _b !== void 0 ? _b : false;
|
|
51
|
-
const
|
|
52
|
-
const cacheKey = `${selectorID}_${initWithStoredValues}_${allowStaleData}_${canBeMissing}`;
|
|
50
|
+
const cacheKey = `${selectorID}_${initWithStoredValues}_${allowStaleData}`;
|
|
53
51
|
// Increment reference count for this cache key
|
|
54
52
|
const currentCount = this.cacheKeyRefCounts.get(cacheKey) || 0;
|
|
55
53
|
this.cacheKeyRefCounts.set(cacheKey, currentCount + 1);
|
package/dist/useOnyx.d.ts
CHANGED
|
@@ -24,13 +24,6 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
|
|
|
24
24
|
* If set to `true`, the key can be changed dynamically during the component lifecycle.
|
|
25
25
|
*/
|
|
26
26
|
allowDynamicKey?: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* If the component calling this is the one loading the data by calling an action, then you should set this to `true`.
|
|
29
|
-
*
|
|
30
|
-
* If the component calling this does not load the data then you should set it to `false`, which means that if the data
|
|
31
|
-
* is not there, it will log an alert, as it means we are using data that no one loaded and that's most probably a bug.
|
|
32
|
-
*/
|
|
33
|
-
canBeMissing?: boolean;
|
|
34
27
|
/**
|
|
35
28
|
* This will be used to subscribe to a subset of an Onyx key's data.
|
|
36
29
|
* Using this setting on `useOnyx` can have very positive performance benefits because the component will only re-render
|
package/dist/useOnyx.js
CHANGED
|
@@ -44,7 +44,6 @@ const OnyxUtils_1 = __importDefault(require("./OnyxUtils"));
|
|
|
44
44
|
const GlobalSettings = __importStar(require("./GlobalSettings"));
|
|
45
45
|
const usePrevious_1 = __importDefault(require("./usePrevious"));
|
|
46
46
|
const metrics_1 = __importDefault(require("./metrics"));
|
|
47
|
-
const Logger = __importStar(require("./Logger"));
|
|
48
47
|
const OnyxSnapshotCache_1 = __importDefault(require("./OnyxSnapshotCache"));
|
|
49
48
|
const useLiveRef_1 = __importDefault(require("./useLiveRef"));
|
|
50
49
|
function useOnyx(key, options, dependencies = []) {
|
|
@@ -111,8 +110,7 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
111
110
|
selector: options === null || options === void 0 ? void 0 : options.selector,
|
|
112
111
|
initWithStoredValues: options === null || options === void 0 ? void 0 : options.initWithStoredValues,
|
|
113
112
|
allowStaleData: options === null || options === void 0 ? void 0 : options.allowStaleData,
|
|
114
|
-
|
|
115
|
-
}), [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]);
|
|
113
|
+
}), [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]);
|
|
116
114
|
(0, react_1.useEffect)(() => () => OnyxSnapshotCache_1.default.deregisterConsumer(key, cacheKey), [key, cacheKey]);
|
|
117
115
|
(0, react_1.useEffect)(() => {
|
|
118
116
|
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
|
|
@@ -178,7 +176,6 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
178
176
|
return cachedResult;
|
|
179
177
|
}
|
|
180
178
|
}
|
|
181
|
-
let isOnyxValueDefined = true;
|
|
182
179
|
// We return the initial result right away during the first connection if `initWithStoredValues` is set to `false`.
|
|
183
180
|
if (isFirstConnectionRef.current && (options === null || options === void 0 ? void 0 : options.initWithStoredValues) === false) {
|
|
184
181
|
const result = resultRef.current;
|
|
@@ -194,9 +191,6 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
194
191
|
const value = OnyxUtils_1.default.tryGetCachedValue(key);
|
|
195
192
|
const selectedValue = memoizedSelector ? memoizedSelector(value) : value;
|
|
196
193
|
newValueRef.current = (selectedValue !== null && selectedValue !== void 0 ? selectedValue : undefined);
|
|
197
|
-
// This flag is `false` when the original Onyx value (without selector) is not defined yet.
|
|
198
|
-
// It will be used later to check if we need to log an alert that the value is missing.
|
|
199
|
-
isOnyxValueDefined = value !== null && value !== undefined;
|
|
200
194
|
// We set this flag to `false` again since we don't want to get the newest cached value every time `getSnapshot()` is executed,
|
|
201
195
|
// and only when `Onyx.connect()` callback is fired.
|
|
202
196
|
shouldGetCachedValueRef.current = false;
|
|
@@ -243,18 +237,12 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
243
237
|
sourceValue: sourceValueRef.current,
|
|
244
238
|
},
|
|
245
239
|
];
|
|
246
|
-
// If `canBeMissing` is set to `false` and the Onyx value of that key is not defined,
|
|
247
|
-
// we log an alert so it can be acknowledged by the consumer. Additionally, we won't log alerts
|
|
248
|
-
// if there's a `Onyx.clear()` task in progress.
|
|
249
|
-
if ((options === null || options === void 0 ? void 0 : options.canBeMissing) === false && newFetchStatus === 'loaded' && !isOnyxValueDefined && !OnyxCache_1.default.hasPendingTask(OnyxCache_1.TASK.CLEAR)) {
|
|
250
|
-
Logger.logAlert(`useOnyx returned no data for key with canBeMissing set to false for key ${key}`, { showAlert: true });
|
|
251
|
-
}
|
|
252
240
|
}
|
|
253
241
|
if (newFetchStatus !== 'loading') {
|
|
254
242
|
OnyxSnapshotCache_1.default.setCachedResult(key, cacheKey, resultRef.current);
|
|
255
243
|
}
|
|
256
244
|
return resultRef.current;
|
|
257
|
-
}, [options === null || options === void 0 ? void 0 : options.initWithStoredValues, options === null || options === void 0 ? void 0 : options.allowStaleData,
|
|
245
|
+
}, [options === null || options === void 0 ? void 0 : options.initWithStoredValues, options === null || options === void 0 ? void 0 : options.allowStaleData, key, memoizedSelector, cacheKey, previousKey]);
|
|
258
246
|
const subscribe = (0, react_1.useCallback)((onStoreChange) => {
|
|
259
247
|
isConnectingRef.current = true;
|
|
260
248
|
onStoreChangeFnRef.current = onStoreChange;
|