react-native-onyx 2.0.130 → 2.0.132
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.d.ts +1 -1
- package/dist/Onyx.js +2 -2
- package/dist/OnyxUtils.d.ts +1 -2
- package/dist/OnyxUtils.js +2 -14
- package/dist/types.d.ts +0 -7
- package/dist/useOnyx.js +28 -9
- package/package.json +1 -1
package/dist/Onyx.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as Logger from './Logger';
|
|
|
2
2
|
import type { CollectionKeyBase, ConnectOptions, InitOptions, Mapping, OnyxKey, OnyxMergeCollectionInput, OnyxMergeInput, OnyxMultiSetInput, OnyxSetInput, OnyxUpdate, SetOptions } from './types';
|
|
3
3
|
import type { Connection } from './OnyxConnectionManager';
|
|
4
4
|
/** Initialize the store with actions and listening for storage events */
|
|
5
|
-
declare function init({ keys, initialKeyStates, evictableKeys, maxCachedKeysCount, shouldSyncMultipleInstances, debugSetState, enablePerformanceMetrics, skippableCollectionMemberIDs,
|
|
5
|
+
declare function init({ keys, initialKeyStates, evictableKeys, maxCachedKeysCount, shouldSyncMultipleInstances, debugSetState, enablePerformanceMetrics, skippableCollectionMemberIDs, }: InitOptions): void;
|
|
6
6
|
/**
|
|
7
7
|
* Connects to an Onyx key given the options passed and listens to its changes.
|
|
8
8
|
* This method will be deprecated soon. Please use `Onyx.connectWithoutView()` instead.
|
package/dist/Onyx.js
CHANGED
|
@@ -49,7 +49,7 @@ const GlobalSettings = __importStar(require("./GlobalSettings"));
|
|
|
49
49
|
const metrics_1 = __importDefault(require("./metrics"));
|
|
50
50
|
const OnyxMerge_1 = __importDefault(require("./OnyxMerge"));
|
|
51
51
|
/** Initialize the store with actions and listening for storage events */
|
|
52
|
-
function init({ keys = {}, initialKeyStates = {}, evictableKeys = [], maxCachedKeysCount = 1000, shouldSyncMultipleInstances = !!global.localStorage, debugSetState = false, enablePerformanceMetrics = false, skippableCollectionMemberIDs = [],
|
|
52
|
+
function init({ keys = {}, initialKeyStates = {}, evictableKeys = [], maxCachedKeysCount = 1000, shouldSyncMultipleInstances = !!global.localStorage, debugSetState = false, enablePerformanceMetrics = false, skippableCollectionMemberIDs = [], }) {
|
|
53
53
|
var _a;
|
|
54
54
|
if (enablePerformanceMetrics) {
|
|
55
55
|
GlobalSettings.setPerformanceMetricsEnabled(true);
|
|
@@ -70,7 +70,7 @@ function init({ keys = {}, initialKeyStates = {}, evictableKeys = [], maxCachedK
|
|
|
70
70
|
if (maxCachedKeysCount > 0) {
|
|
71
71
|
OnyxCache_1.default.setRecentKeysLimit(maxCachedKeysCount);
|
|
72
72
|
}
|
|
73
|
-
OnyxUtils_1.default.initStoreValues(keys, initialKeyStates, evictableKeys
|
|
73
|
+
OnyxUtils_1.default.initStoreValues(keys, initialKeyStates, evictableKeys);
|
|
74
74
|
// Initialize all of our keys with data provided then give green light to any pending connections
|
|
75
75
|
Promise.all([OnyxCache_1.default.addEvictableKeysToRecentlyAccessedList(OnyxUtils_1.default.isCollectionKey, OnyxUtils_1.default.getAllKeys), OnyxUtils_1.default.initializeWithDefaultKeyStates()]).then(OnyxUtils_1.default.getDeferredInitTask().resolve);
|
|
76
76
|
}
|
package/dist/OnyxUtils.d.ts
CHANGED
|
@@ -45,9 +45,8 @@ declare function setSkippableCollectionMemberIDs(ids: Set<string>): void;
|
|
|
45
45
|
* @param keys - `ONYXKEYS` constants object from Onyx.init()
|
|
46
46
|
* @param initialKeyStates - initial data to set when `init()` and `clear()` are called
|
|
47
47
|
* @param evictableKeys - This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal.
|
|
48
|
-
* @param fullyMergedSnapshotKeys - Array of snapshot collection keys where full merge is supported and data structure can be changed after merge.
|
|
49
48
|
*/
|
|
50
|
-
declare function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Partial<KeyValueMapping>, evictableKeys: OnyxKey[]
|
|
49
|
+
declare function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Partial<KeyValueMapping>, evictableKeys: OnyxKey[]): void;
|
|
51
50
|
/**
|
|
52
51
|
* Sends an action to DevTools extension
|
|
53
52
|
*
|
package/dist/OnyxUtils.js
CHANGED
|
@@ -79,7 +79,6 @@ let batchUpdatesQueue = [];
|
|
|
79
79
|
// Used for comparison with a new update to avoid invoking the Onyx.connect callback with the same data.
|
|
80
80
|
let lastConnectionCallbackData = new Map();
|
|
81
81
|
let snapshotKey = null;
|
|
82
|
-
let fullyMergedSnapshotKeys;
|
|
83
82
|
// Keeps track of the last subscriptionID that was used so we can keep incrementing it
|
|
84
83
|
let lastSubscriptionID = 0;
|
|
85
84
|
// Connections can be made before `Onyx.init`. They would wait for this task before resolving
|
|
@@ -131,9 +130,8 @@ function setSkippableCollectionMemberIDs(ids) {
|
|
|
131
130
|
* @param keys - `ONYXKEYS` constants object from Onyx.init()
|
|
132
131
|
* @param initialKeyStates - initial data to set when `init()` and `clear()` are called
|
|
133
132
|
* @param evictableKeys - This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal.
|
|
134
|
-
* @param fullyMergedSnapshotKeys - Array of snapshot collection keys where full merge is supported and data structure can be changed after merge.
|
|
135
133
|
*/
|
|
136
|
-
function initStoreValues(keys, initialKeyStates, evictableKeys
|
|
134
|
+
function initStoreValues(keys, initialKeyStates, evictableKeys) {
|
|
137
135
|
var _a;
|
|
138
136
|
// We need the value of the collection keys later for checking if a
|
|
139
137
|
// key is a collection. We store it in a map for faster lookup.
|
|
@@ -151,7 +149,6 @@ function initStoreValues(keys, initialKeyStates, evictableKeys, fullyMergedSnaps
|
|
|
151
149
|
OnyxCache_1.default.setCollectionKeys(onyxCollectionKeySet);
|
|
152
150
|
if (typeof keys.COLLECTION === 'object' && typeof keys.COLLECTION.SNAPSHOT === 'string') {
|
|
153
151
|
snapshotKey = keys.COLLECTION.SNAPSHOT;
|
|
154
|
-
fullyMergedSnapshotKeys = new Set(fullyMergedSnapshotKeysParam !== null && fullyMergedSnapshotKeysParam !== void 0 ? fullyMergedSnapshotKeysParam : []);
|
|
155
152
|
}
|
|
156
153
|
}
|
|
157
154
|
function sendActionToDevTools(method, key, value, mergedValue = undefined) {
|
|
@@ -1220,16 +1217,7 @@ function updateSnapshots(data, mergeFn) {
|
|
|
1220
1217
|
return;
|
|
1221
1218
|
}
|
|
1222
1219
|
const oldValue = updatedData[key] || {};
|
|
1223
|
-
|
|
1224
|
-
try {
|
|
1225
|
-
collectionKey = getCollectionKey(key);
|
|
1226
|
-
}
|
|
1227
|
-
catch (e) {
|
|
1228
|
-
// If getCollectionKey() throws an error it means the key is not a collection key.
|
|
1229
|
-
collectionKey = undefined;
|
|
1230
|
-
}
|
|
1231
|
-
const shouldFullyMerge = fullyMergedSnapshotKeys === null || fullyMergedSnapshotKeys === void 0 ? void 0 : fullyMergedSnapshotKeys.has(collectionKey || key);
|
|
1232
|
-
const newValue = shouldFullyMerge ? value : (0, pick_1.default)(value, Object.keys(snapshotData[key]));
|
|
1220
|
+
const newValue = (0, pick_1.default)(value, Object.keys(snapshotData[key]));
|
|
1233
1221
|
updatedData = Object.assign(Object.assign({}, updatedData), { [key]: Object.assign(oldValue, newValue) });
|
|
1234
1222
|
});
|
|
1235
1223
|
// Skip the update if there's no data to be merged
|
package/dist/types.d.ts
CHANGED
|
@@ -411,13 +411,6 @@ type InitOptions = {
|
|
|
411
411
|
* Additionally, any subscribers from these keys to won't receive any data from Onyx.
|
|
412
412
|
*/
|
|
413
413
|
skippableCollectionMemberIDs?: string[];
|
|
414
|
-
/**
|
|
415
|
-
* Array of snapshot collection keys where full merge is supported and data structure can be changed after merge.
|
|
416
|
-
* For e.g. if oldSnapshotData is {report_1: {name 'Fitsum'}} and BE update is {report_1: {name:'Fitsum2', nickName:'Fitse'}}
|
|
417
|
-
* if it is fullyMergedSnapshotkey the `nickName` prop that didn't exist in the previous data will be merged
|
|
418
|
-
* otherwise only existing prop will be picked from the BE update and merged (in this case only name).
|
|
419
|
-
*/
|
|
420
|
-
fullyMergedSnapshotKeys?: string[];
|
|
421
414
|
};
|
|
422
415
|
type GenericFunction = (...args: any[]) => any;
|
|
423
416
|
/**
|
package/dist/useOnyx.js
CHANGED
|
@@ -45,30 +45,41 @@ const GlobalSettings = __importStar(require("./GlobalSettings"));
|
|
|
45
45
|
const usePrevious_1 = __importDefault(require("./usePrevious"));
|
|
46
46
|
const metrics_1 = __importDefault(require("./metrics"));
|
|
47
47
|
const Logger = __importStar(require("./Logger"));
|
|
48
|
+
const useLiveRef_1 = __importDefault(require("./useLiveRef"));
|
|
48
49
|
function useOnyx(key, options, dependencies = []) {
|
|
49
50
|
const connectionRef = (0, react_1.useRef)(null);
|
|
50
51
|
const previousKey = (0, usePrevious_1.default)(key);
|
|
52
|
+
const currentDependenciesRef = (0, useLiveRef_1.default)(dependencies);
|
|
53
|
+
const currentSelectorRef = (0, useLiveRef_1.default)(options === null || options === void 0 ? void 0 : options.selector);
|
|
51
54
|
// Create memoized version of selector for performance
|
|
52
55
|
const memoizedSelector = (0, react_1.useMemo)(() => {
|
|
53
56
|
if (!(options === null || options === void 0 ? void 0 : options.selector))
|
|
54
57
|
return null;
|
|
55
58
|
let lastInput;
|
|
56
59
|
let lastOutput;
|
|
60
|
+
let lastDependencies = [];
|
|
57
61
|
let hasComputed = false;
|
|
58
62
|
return (input) => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
const currentDependencies = currentDependenciesRef.current;
|
|
64
|
+
const currentSelector = currentSelectorRef.current;
|
|
65
|
+
// Recompute if input changed, dependencies changed, or first time
|
|
66
|
+
const dependenciesChanged = !(0, fast_equals_1.shallowEqual)(lastDependencies, currentDependencies);
|
|
67
|
+
if (!hasComputed || lastInput !== input || dependenciesChanged) {
|
|
68
|
+
// Only proceed if we have a valid selector
|
|
69
|
+
if (currentSelector) {
|
|
70
|
+
const newOutput = currentSelector(input);
|
|
71
|
+
// Deep equality mode: only update if output actually changed
|
|
72
|
+
if (!hasComputed || !(0, fast_equals_1.deepEqual)(lastOutput, newOutput) || dependenciesChanged) {
|
|
73
|
+
lastInput = input;
|
|
74
|
+
lastOutput = newOutput;
|
|
75
|
+
lastDependencies = [...currentDependencies];
|
|
76
|
+
hasComputed = true;
|
|
77
|
+
}
|
|
67
78
|
}
|
|
68
79
|
}
|
|
69
80
|
return lastOutput;
|
|
70
81
|
};
|
|
71
|
-
}, [options === null || options === void 0 ? void 0 : options.selector]);
|
|
82
|
+
}, [currentDependenciesRef, currentSelectorRef, options === null || options === void 0 ? void 0 : options.selector]);
|
|
72
83
|
// Stores the previous cached value as it's necessary to compare with the new value in `getSnapshot()`.
|
|
73
84
|
// We initialize it to `null` to simulate that we don't have any value from cache yet.
|
|
74
85
|
const previousValueRef = (0, react_1.useRef)(null);
|
|
@@ -111,10 +122,18 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
111
122
|
}
|
|
112
123
|
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'.`);
|
|
113
124
|
}, [previousKey, key, options === null || options === void 0 ? void 0 : options.allowDynamicKey]);
|
|
125
|
+
// Track previous dependencies to prevent infinite loops
|
|
126
|
+
const previousDependenciesRef = (0, react_1.useRef)([]);
|
|
114
127
|
(0, react_1.useEffect)(() => {
|
|
115
128
|
// This effect will only run if the `dependencies` array changes. If it changes it will force the hook
|
|
116
129
|
// to trigger a `getSnapshot()` update by calling the stored `onStoreChange()` function reference, thus
|
|
117
130
|
// re-running the hook and returning the latest value to the consumer.
|
|
131
|
+
// Deep equality check to prevent infinite loops when dependencies array reference changes
|
|
132
|
+
// but content remains the same
|
|
133
|
+
if ((0, fast_equals_1.shallowEqual)(previousDependenciesRef.current, dependencies)) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
previousDependenciesRef.current = dependencies;
|
|
118
137
|
if (connectionRef.current === null || isConnectingRef.current || !onStoreChangeFnRef.current) {
|
|
119
138
|
return;
|
|
120
139
|
}
|