react-native-onyx 2.0.129 → 2.0.130

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.
Files changed (2) hide show
  1. package/dist/useOnyx.js +34 -13
  2. package/package.json +1 -1
package/dist/useOnyx.js CHANGED
@@ -42,15 +42,33 @@ const OnyxCache_1 = __importStar(require("./OnyxCache"));
42
42
  const OnyxConnectionManager_1 = __importDefault(require("./OnyxConnectionManager"));
43
43
  const OnyxUtils_1 = __importDefault(require("./OnyxUtils"));
44
44
  const GlobalSettings = __importStar(require("./GlobalSettings"));
45
- const useLiveRef_1 = __importDefault(require("./useLiveRef"));
46
45
  const usePrevious_1 = __importDefault(require("./usePrevious"));
47
46
  const metrics_1 = __importDefault(require("./metrics"));
48
47
  const Logger = __importStar(require("./Logger"));
49
48
  function useOnyx(key, options, dependencies = []) {
50
49
  const connectionRef = (0, react_1.useRef)(null);
51
50
  const previousKey = (0, usePrevious_1.default)(key);
52
- // Used to stabilize the selector reference and avoid unnecessary calls to `getSnapshot()`.
53
- const selectorRef = (0, useLiveRef_1.default)(options === null || options === void 0 ? void 0 : options.selector);
51
+ // Create memoized version of selector for performance
52
+ const memoizedSelector = (0, react_1.useMemo)(() => {
53
+ if (!(options === null || options === void 0 ? void 0 : options.selector))
54
+ return null;
55
+ let lastInput;
56
+ let lastOutput;
57
+ let hasComputed = false;
58
+ return (input) => {
59
+ // Always recompute when input changes
60
+ if (!hasComputed || lastInput !== input) {
61
+ const newOutput = options.selector(input);
62
+ // Deep equality mode: only update if output actually changed
63
+ if (!hasComputed || !(0, fast_equals_1.deepEqual)(lastOutput, newOutput)) {
64
+ lastInput = input;
65
+ lastOutput = newOutput;
66
+ hasComputed = true;
67
+ }
68
+ }
69
+ return lastOutput;
70
+ };
71
+ }, [options === null || options === void 0 ? void 0 : options.selector]);
54
72
  // Stores the previous cached value as it's necessary to compare with the new value in `getSnapshot()`.
55
73
  // We initialize it to `null` to simulate that we don't have any value from cache yet.
56
74
  const previousValueRef = (0, react_1.useRef)(null);
@@ -120,7 +138,7 @@ function useOnyx(key, options, dependencies = []) {
120
138
  }
121
139
  }, [key, options === null || options === void 0 ? void 0 : options.canEvict]);
122
140
  const getSnapshot = (0, react_1.useCallback)(() => {
123
- var _a, _b, _c;
141
+ var _a, _b, _c, _d;
124
142
  let isOnyxValueDefined = true;
125
143
  // We return the initial result right away during the first connection if `initWithStoredValues` is set to `false`.
126
144
  if (isFirstConnectionRef.current && (options === null || options === void 0 ? void 0 : options.initWithStoredValues) === false) {
@@ -132,7 +150,7 @@ function useOnyx(key, options, dependencies = []) {
132
150
  if (isFirstConnectionRef.current || shouldGetCachedValueRef.current) {
133
151
  // Gets the value from cache and maps it with selector. It changes `null` to `undefined` for `useOnyx` compatibility.
134
152
  const value = OnyxUtils_1.default.tryGetCachedValue(key);
135
- const selectedValue = selectorRef.current ? selectorRef.current(value) : value;
153
+ const selectedValue = memoizedSelector ? memoizedSelector(value) : value;
136
154
  newValueRef.current = (selectedValue !== null && selectedValue !== void 0 ? selectedValue : undefined);
137
155
  // This flag is `false` when the original Onyx value (without selector) is not defined yet.
138
156
  // It will be used later to check if we need to log an alert that the value is missing.
@@ -151,15 +169,18 @@ function useOnyx(key, options, dependencies = []) {
151
169
  newValueRef.current = undefined;
152
170
  newFetchStatus = 'loading';
153
171
  }
154
- // We do a deep equality check if `selector` is defined, since each `tryGetCachedValue()` call will
155
- // generate a plain new primitive/object/array that was created using the `selector` function.
156
- // For the other cases we will only deal with object reference checks, so just a shallow equality check is enough.
172
+ // Optimized equality checking - eliminated redundant deep equality:
173
+ // - Memoized selectors already handle deep equality internally, so we can use fast reference equality
174
+ // - Non-selector cases use shallow equality for object reference checks
175
+ // - Normalize null to undefined to ensure consistent comparison (both represent "no value")
157
176
  let areValuesEqual;
158
- if (selectorRef.current) {
159
- areValuesEqual = (0, fast_equals_1.deepEqual)((_a = previousValueRef.current) !== null && _a !== void 0 ? _a : undefined, newValueRef.current);
177
+ if (memoizedSelector) {
178
+ const normalizedPrevious = (_a = previousValueRef.current) !== null && _a !== void 0 ? _a : undefined;
179
+ const normalizedNew = (_b = newValueRef.current) !== null && _b !== void 0 ? _b : undefined;
180
+ areValuesEqual = normalizedPrevious === normalizedNew;
160
181
  }
161
182
  else {
162
- areValuesEqual = (0, fast_equals_1.shallowEqual)((_b = previousValueRef.current) !== null && _b !== void 0 ? _b : undefined, newValueRef.current);
183
+ areValuesEqual = (0, fast_equals_1.shallowEqual)((_c = previousValueRef.current) !== null && _c !== void 0 ? _c : undefined, newValueRef.current);
163
184
  }
164
185
  // We update the cached value and the result in the following conditions:
165
186
  // We will update the cached value and the result in any of the following situations:
@@ -173,7 +194,7 @@ function useOnyx(key, options, dependencies = []) {
173
194
  // If the new value is `null` we default it to `undefined` to ensure the consumer gets a consistent result from the hook.
174
195
  const newStatus = newFetchStatus !== null && newFetchStatus !== void 0 ? newFetchStatus : 'loaded';
175
196
  resultRef.current = [
176
- (_c = previousValueRef.current) !== null && _c !== void 0 ? _c : undefined,
197
+ (_d = previousValueRef.current) !== null && _d !== void 0 ? _d : undefined,
177
198
  {
178
199
  status: newStatus,
179
200
  sourceValue: sourceValueRef.current,
@@ -187,7 +208,7 @@ function useOnyx(key, options, dependencies = []) {
187
208
  }
188
209
  }
189
210
  return resultRef.current;
190
- }, [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, key, selectorRef]);
211
+ }, [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, key, memoizedSelector]);
191
212
  const subscribe = (0, react_1.useCallback)((onStoreChange) => {
192
213
  isConnectingRef.current = true;
193
214
  onStoreChangeFnRef.current = onStoreChange;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.129",
3
+ "version": "2.0.130",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",