react-native-onyx 2.0.131 → 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.
Files changed (2) hide show
  1. package/dist/useOnyx.js +28 -9
  2. package/package.json +1 -1
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
- // 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;
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.131",
3
+ "version": "2.0.132",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",