react-native-onyx 3.0.92 → 3.0.93
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.
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fast_equals_1 = require("fast-equals");
|
|
4
|
+
/**
|
|
5
|
+
* Memoizes shallowEqual verdicts by the identity of the compared objects. Onyx values are
|
|
6
|
+
* treated as immutable (merge/set replace objects, never mutate), so a (prev, next) reference
|
|
7
|
+
* pair always yields the same verdict. In the hot case — N no-selector hooks on the same big
|
|
8
|
+
* key — every hook compares the exact same two cache-owned objects, so the first hook pays for
|
|
9
|
+
* the O(keys) walk and the rest resolve in O(1). WeakMap keys make stale entries impossible to
|
|
10
|
+
* read (lookup requires holding both exact objects) and let GC reclaim them.
|
|
11
|
+
*/
|
|
12
|
+
const shallowEqualVerdicts = new WeakMap();
|
|
13
|
+
/**
|
|
14
|
+
* Identity-pair-memoized shallowEqual: same (a, b) references → cached verdict, no walk.
|
|
15
|
+
*/
|
|
16
|
+
function memoizedShallowEqual(a, b) {
|
|
17
|
+
// Only object pairs are memoizable (WeakMap keys) — anything else is O(1) to compare anyway.
|
|
18
|
+
if (typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) {
|
|
19
|
+
return (0, fast_equals_1.shallowEqual)(a, b);
|
|
20
|
+
}
|
|
21
|
+
let verdictsForA = shallowEqualVerdicts.get(a);
|
|
22
|
+
if (!verdictsForA) {
|
|
23
|
+
verdictsForA = new WeakMap();
|
|
24
|
+
shallowEqualVerdicts.set(a, verdictsForA);
|
|
25
|
+
}
|
|
26
|
+
const cachedVerdict = verdictsForA.get(b);
|
|
27
|
+
if (cachedVerdict !== undefined) {
|
|
28
|
+
return cachedVerdict;
|
|
29
|
+
}
|
|
30
|
+
const verdict = (0, fast_equals_1.shallowEqual)(a, b);
|
|
31
|
+
verdictsForA.set(b, verdict);
|
|
32
|
+
return verdict;
|
|
33
|
+
}
|
|
34
|
+
exports.default = memoizedShallowEqual;
|
package/dist/useOnyx.js
CHANGED
|
@@ -42,6 +42,7 @@ 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 OnyxSnapshotCache_1 = __importDefault(require("./OnyxSnapshotCache"));
|
|
45
|
+
const memoizedShallowEqual_1 = __importDefault(require("./memoizedShallowEqual"));
|
|
45
46
|
const useLiveRef_1 = __importDefault(require("./useLiveRef"));
|
|
46
47
|
function useOnyx(key, options, dependencies = []) {
|
|
47
48
|
const connectionRef = (0, react_1.useRef)(null);
|
|
@@ -170,9 +171,11 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
170
171
|
}
|
|
171
172
|
// shallowEqual checks === first (O(1) for frozen snapshots and stable selector references),
|
|
172
173
|
// then falls back to comparing top-level properties for individual keys that may have
|
|
173
|
-
// new references with equivalent content.
|
|
174
|
+
// new references with equivalent content. The comparison is memoized by object identity
|
|
175
|
+
// (see `memoizedShallowEqual`) so N hooks comparing the same two cache objects pay for
|
|
176
|
+
// one walk in total instead of one walk each.
|
|
174
177
|
// Normalize null to undefined to ensure consistent comparison (both represent "no value").
|
|
175
|
-
const areValuesEqual = (0,
|
|
178
|
+
const areValuesEqual = (0, memoizedShallowEqual_1.default)((_a = previousValueRef.current) !== null && _a !== void 0 ? _a : undefined, (_b = newValueRef.current) !== null && _b !== void 0 ? _b : undefined);
|
|
176
179
|
// We update the cached value and the result in the following conditions:
|
|
177
180
|
// We will update the cached value and the result in any of the following situations:
|
|
178
181
|
// - The previously cached value is different from the new value.
|