on-zero 0.4.50 → 0.5.1
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/cjs/createUseQuery.cjs +0 -6
- package/dist/cjs/createUseQuery.native.js +0 -6
- package/dist/cjs/createUseQuery.native.js.map +1 -1
- package/dist/cjs/createUseQueryDirect.cjs +1 -5
- package/dist/cjs/createUseQueryDirect.native.js +1 -5
- package/dist/cjs/createUseQueryDirect.native.js.map +1 -1
- package/dist/esm/createUseQuery.mjs +0 -6
- package/dist/esm/createUseQuery.mjs.map +1 -1
- package/dist/esm/createUseQuery.native.js +0 -6
- package/dist/esm/createUseQuery.native.js.map +1 -1
- package/dist/esm/createUseQueryDirect.mjs +1 -5
- package/dist/esm/createUseQueryDirect.mjs.map +1 -1
- package/dist/esm/createUseQueryDirect.native.js +1 -5
- package/dist/esm/createUseQueryDirect.native.js.map +1 -1
- package/package.json +2 -2
- package/src/createUseQuery.tsx +0 -13
- package/src/createUseQueryDirect.tsx +1 -10
- package/types/createUseQuery.d.ts.map +1 -1
- package/types/createUseQueryDirect.d.ts.map +1 -1
- package/dist/cjs/useContentStable.cjs +0 -66
- package/dist/cjs/useContentStable.native.js +0 -93
- package/dist/cjs/useContentStable.native.js.map +0 -1
- package/dist/cjs/useContentStable.test.cjs +0 -105
- package/dist/cjs/useContentStable.test.native.js +0 -108
- package/dist/cjs/useContentStable.test.native.js.map +0 -1
- package/dist/esm/useContentStable.mjs +0 -40
- package/dist/esm/useContentStable.mjs.map +0 -1
- package/dist/esm/useContentStable.native.js +0 -64
- package/dist/esm/useContentStable.native.js.map +0 -1
- package/dist/esm/useContentStable.test.mjs +0 -106
- package/dist/esm/useContentStable.test.mjs.map +0 -1
- package/dist/esm/useContentStable.test.native.js +0 -106
- package/dist/esm/useContentStable.test.native.js.map +0 -1
- package/src/useContentStable.test.ts +0 -61
- package/src/useContentStable.ts +0 -78
- package/types/useContentStable.d.ts +0 -3
- package/types/useContentStable.d.ts.map +0 -1
- package/types/useContentStable.test.d.ts +0 -2
- package/types/useContentStable.test.d.ts.map +0 -1
package/src/useContentStable.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
// content-stable identity for useQuery's inner data + info values.
|
|
2
|
-
//
|
|
3
|
-
// upstream zero-react's view does `data = deepClone(snap)` inside #onData
|
|
4
|
-
// whenever the materialized view notifies, which means each notification
|
|
5
|
-
// produces a fresh JS reference even when the row's content hasn't changed.
|
|
6
|
-
// consumers that destructure `[row] = useQuery(byId, {id})` and then use
|
|
7
|
-
// `row` as a useEffect dep — or that depend on `[rows]` for a list query —
|
|
8
|
-
// see a "change" on every notification and re-run effects redundantly.
|
|
9
|
-
//
|
|
10
|
-
// `useContentStable(value)` keeps a ref to the previous value and returns the
|
|
11
|
-
// old reference when shallow-content equality holds, so downstream identity
|
|
12
|
-
// comparisons (`useMemo`, `useEffect`, `useCallback`) don't false-fire.
|
|
13
|
-
//
|
|
14
|
-
// equality semantics:
|
|
15
|
-
// - primitives + same-reference → return as-is
|
|
16
|
-
// - arrays: same length + each element strict-eq → reuse previous ref
|
|
17
|
-
// - objects: same keys + each value strict-eq → reuse previous ref
|
|
18
|
-
// - mismatches → swap the ref forward
|
|
19
|
-
//
|
|
20
|
-
// the per-element check is strict-eq (not recursive). that's intentional —
|
|
21
|
-
// it catches the common case where a query result is the same row(s) on a
|
|
22
|
-
// re-emit. for nested-object stability the rows themselves get the same
|
|
23
|
-
// treatment one level up (the wrapping useQuery already applies this to
|
|
24
|
-
// `data` so an array-of-rows query holds row references stable if each row
|
|
25
|
-
// content-matches its previous snapshot row).
|
|
26
|
-
|
|
27
|
-
import { useRef } from 'react'
|
|
28
|
-
|
|
29
|
-
function arraysShallowEqual(a: readonly unknown[], b: readonly unknown[]): boolean {
|
|
30
|
-
if (a === b) return true
|
|
31
|
-
if (a.length !== b.length) return false
|
|
32
|
-
for (let i = 0; i < a.length; i++) {
|
|
33
|
-
if (!Object.is(a[i], b[i])) return false
|
|
34
|
-
}
|
|
35
|
-
return true
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function objectsShallowEqual(a: object, b: object): boolean {
|
|
39
|
-
if (a === b) return true
|
|
40
|
-
const ak = Object.keys(a as Record<string, unknown>)
|
|
41
|
-
const bk = Object.keys(b as Record<string, unknown>)
|
|
42
|
-
if (ak.length !== bk.length) return false
|
|
43
|
-
for (const k of ak) {
|
|
44
|
-
if (
|
|
45
|
-
!Object.is(
|
|
46
|
-
(a as Record<string, unknown>)[k],
|
|
47
|
-
(b as Record<string, unknown>)[k],
|
|
48
|
-
)
|
|
49
|
-
)
|
|
50
|
-
return false
|
|
51
|
-
}
|
|
52
|
-
return true
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function isContentEqual(a: unknown, b: unknown): boolean {
|
|
56
|
-
if (Object.is(a, b)) return true
|
|
57
|
-
if (a === null || b === null || a === undefined || b === undefined) return false
|
|
58
|
-
if (typeof a !== 'object' || typeof b !== 'object') return false
|
|
59
|
-
const aIsArr = Array.isArray(a)
|
|
60
|
-
const bIsArr = Array.isArray(b)
|
|
61
|
-
if (aIsArr !== bIsArr) return false
|
|
62
|
-
if (aIsArr && bIsArr) {
|
|
63
|
-
return arraysShallowEqual(a as readonly unknown[], b as readonly unknown[])
|
|
64
|
-
}
|
|
65
|
-
return objectsShallowEqual(a as object, b as object)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// returns `value` on first render, then returns the previous reference for as
|
|
69
|
-
// long as content-equality holds. mutating the ref during render is safe here
|
|
70
|
-
// because we read its current value AFTER deciding whether to swap — no read-
|
|
71
|
-
// then-write race that React would warn about.
|
|
72
|
-
export function useContentStable<T>(value: T): T {
|
|
73
|
-
const ref = useRef<T>(value)
|
|
74
|
-
if (!isContentEqual(ref.current, value)) {
|
|
75
|
-
ref.current = value
|
|
76
|
-
}
|
|
77
|
-
return ref.current
|
|
78
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useContentStable.d.ts","sourceRoot":"","sources":["../src/useContentStable.ts"],"names":[],"mappings":"AAsDA,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAW9D;AAMD,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAM/C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useContentStable.test.d.ts","sourceRoot":"","sources":["../src/useContentStable.test.ts"],"names":[],"mappings":""}
|