zustand-tracked
Version:
A small library that allows all your zustand store selectors to be tracked, optimizing computations and rerenders.
449 lines (448 loc) • 13.6 kB
JavaScript
import L, { useRef as A, useMemo as ie } from "react";
import { useStore as ae, createStore as F } from "zustand";
const fe = Symbol(), I = Symbol(), g = "a", X = "f", B = "p", J = "c", Q = "t", Z = "h", M = "w", ee = "o", te = "k";
let le = (e, t) => new Proxy(e, t);
const K = Object.getPrototypeOf, G = /* @__PURE__ */ new WeakMap(), re = (e) => e && (G.has(e) ? G.get(e) : K(e) === Object.prototype || K(e) === Array.prototype), N = (e) => typeof e == "object" && e !== null, de = (e) => Object.values(Object.getOwnPropertyDescriptors(e)).some((t) => !t.configurable && !t.writable), _e = (e) => {
if (Array.isArray(e))
return Array.from(e);
const t = Object.getOwnPropertyDescriptors(e);
return Object.values(t).forEach((n) => {
n.configurable = !0;
}), Object.create(K(e), t);
}, Oe = (e, t) => {
const n = {
[X]: t
};
let o = !1;
const f = (r, d) => {
if (!o) {
let i = n[g].get(e);
if (i || (i = {}, n[g].set(e, i)), r === M)
i[M] = !0;
else {
let _ = i[r];
_ || (_ = /* @__PURE__ */ new Set(), i[r] = _), _.add(d);
}
}
}, l = () => {
o = !0, n[g].delete(e);
}, c = {
get(r, d) {
return d === I ? e : (f(te, d), Y(Reflect.get(r, d), n[g], n[J], n[Q]));
},
has(r, d) {
return d === fe ? (l(), !0) : (f(Z, d), Reflect.has(r, d));
},
getOwnPropertyDescriptor(r, d) {
return f(ee, d), Reflect.getOwnPropertyDescriptor(r, d);
},
ownKeys(r) {
return f(M), Reflect.ownKeys(r);
}
};
return t && (c.set = c.deleteProperty = () => !1), [c, n];
}, ne = (e) => (
// unwrap proxy
e[I] || // otherwise
e
), Y = (e, t, n, o) => {
if (!re(e))
return e;
let f = o && o.get(e);
if (!f) {
const d = ne(e);
de(d) ? f = [d, _e(d)] : f = [d], o == null || o.set(e, f);
}
const [l, c] = f;
let r = n && n.get(l);
return (!r || r[1][X] !== !!c) && (r = Oe(l, !!c), r[1][B] = le(c || l, r[0]), n && n.set(l, r)), r[1][g] = t, r[1][J] = n, r[1][Q] = o, r[1][B];
}, Se = (e, t) => {
const n = Reflect.ownKeys(e), o = Reflect.ownKeys(t);
return n.length !== o.length || n.some((f, l) => f !== o[l]);
}, H = (e, t, n, o, f = Object.is) => {
if (f(e, t))
return !1;
if (!N(e) || !N(t))
return !0;
const l = n.get(ne(e));
if (!l)
return !0;
if (o) {
if (o.get(e) === t)
return !1;
o.set(e, t);
}
let c = null;
for (const r of l[Z] || [])
if (c = Reflect.has(e, r) !== Reflect.has(t, r), c)
return c;
if (l[M] === !0) {
if (c = Se(e, t), c)
return c;
} else
for (const r of l[ee] || []) {
const d = !!Reflect.getOwnPropertyDescriptor(e, r), i = !!Reflect.getOwnPropertyDescriptor(t, r);
if (c = d !== i, c)
return c;
}
for (const r of l[te] || [])
if (c = H(e[r], t[r], n, o, f), c)
return c;
if (c === null)
throw new Error("invalid used");
return c;
}, oe = (e) => re(e) && e[I] || null, ue = (e, t = !0) => {
G.set(e, t);
};
function Ee(e) {
return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
}
var P = { exports: {} }, k = {}, D = { exports: {} }, C = {};
/**
* @license React
* use-sync-external-store-shim.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var U;
function pe() {
if (U) return C;
U = 1;
var e = L;
function t(O, S) {
return O === S && (O !== 0 || 1 / O === 1 / S) || O !== O && S !== S;
}
var n = typeof Object.is == "function" ? Object.is : t, o = e.useState, f = e.useEffect, l = e.useLayoutEffect, c = e.useDebugValue;
function r(O, S) {
var s = S(), u = o({ inst: { value: s, getSnapshot: S } }), a = u[0].inst, E = u[1];
return l(
function() {
a.value = s, a.getSnapshot = S, d(a) && E({ inst: a });
},
[O, s, S]
), f(
function() {
return d(a) && E({ inst: a }), O(function() {
d(a) && E({ inst: a });
});
},
[O]
), c(s), s;
}
function d(O) {
var S = O.getSnapshot;
O = O.value;
try {
var s = S();
return !n(O, s);
} catch {
return !0;
}
}
function i(O, S) {
return S();
}
var _ = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? i : r;
return C.useSyncExternalStore = e.useSyncExternalStore !== void 0 ? e.useSyncExternalStore : _, C;
}
var W = {};
/**
* @license React
* use-sync-external-store-shim.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var j;
function ve() {
return j || (j = 1, process.env.NODE_ENV !== "production" && function() {
function e(s, u) {
return s === u && (s !== 0 || 1 / s === 1 / u) || s !== s && u !== u;
}
function t(s, u) {
_ || f.startTransition === void 0 || (_ = !0, console.error(
"You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."
));
var a = u();
if (!O) {
var E = u();
l(a, E) || (console.error(
"The result of getSnapshot should be cached to avoid an infinite loop"
), O = !0);
}
E = c({
inst: { value: a, getSnapshot: u }
});
var p = E[0].inst, y = E[1];
return d(
function() {
p.value = a, p.getSnapshot = u, n(p) && y({ inst: p });
},
[s, a, u]
), r(
function() {
return n(p) && y({ inst: p }), s(function() {
n(p) && y({ inst: p });
});
},
[s]
), i(a), a;
}
function n(s) {
var u = s.getSnapshot;
s = s.value;
try {
var a = u();
return !l(s, a);
} catch {
return !0;
}
}
function o(s, u) {
return u();
}
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
var f = L, l = typeof Object.is == "function" ? Object.is : e, c = f.useState, r = f.useEffect, d = f.useLayoutEffect, i = f.useDebugValue, _ = !1, O = !1, S = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? o : t;
W.useSyncExternalStore = f.useSyncExternalStore !== void 0 ? f.useSyncExternalStore : S, typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
}()), W;
}
var q;
function ce() {
return q || (q = 1, process.env.NODE_ENV === "production" ? D.exports = pe() : D.exports = ve()), D.exports;
}
/**
* @license React
* use-sync-external-store-shim/with-selector.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var $;
function ye() {
if ($) return k;
$ = 1;
var e = L, t = ce();
function n(i, _) {
return i === _ && (i !== 0 || 1 / i === 1 / _) || i !== i && _ !== _;
}
var o = typeof Object.is == "function" ? Object.is : n, f = t.useSyncExternalStore, l = e.useRef, c = e.useEffect, r = e.useMemo, d = e.useDebugValue;
return k.useSyncExternalStoreWithSelector = function(i, _, O, S, s) {
var u = l(null);
if (u.current === null) {
var a = { hasValue: !1, value: null };
u.current = a;
} else a = u.current;
u = r(
function() {
function p(v) {
if (!y) {
if (y = !0, R = v, v = S(v), s !== void 0 && a.hasValue) {
var m = a.value;
if (s(m, v))
return h = m;
}
return h = v;
}
if (m = h, o(R, v)) return m;
var w = S(v);
return s !== void 0 && s(m, w) ? (R = v, m) : (R = v, h = w);
}
var y = !1, R, h, T = O === void 0 ? null : O;
return [
function() {
return p(_());
},
T === null ? void 0 : function() {
return p(T());
}
];
},
[_, O, S, s]
);
var E = f(i, u[0], u[1]);
return c(
function() {
a.hasValue = !0, a.value = E;
},
[E]
), d(E), E;
}, k;
}
var b = {};
/**
* @license React
* use-sync-external-store-shim/with-selector.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var x;
function me() {
return x || (x = 1, process.env.NODE_ENV !== "production" && function() {
function e(i, _) {
return i === _ && (i !== 0 || 1 / i === 1 / _) || i !== i && _ !== _;
}
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
var t = L, n = ce(), o = typeof Object.is == "function" ? Object.is : e, f = n.useSyncExternalStore, l = t.useRef, c = t.useEffect, r = t.useMemo, d = t.useDebugValue;
b.useSyncExternalStoreWithSelector = function(i, _, O, S, s) {
var u = l(null);
if (u.current === null) {
var a = { hasValue: !1, value: null };
u.current = a;
} else a = u.current;
u = r(
function() {
function p(v) {
if (!y) {
if (y = !0, R = v, v = S(v), s !== void 0 && a.hasValue) {
var m = a.value;
if (s(m, v))
return h = m;
}
return h = v;
}
if (m = h, o(R, v))
return m;
var w = S(v);
return s !== void 0 && s(m, w) ? (R = v, m) : (R = v, h = w);
}
var y = !1, R, h, T = O === void 0 ? null : O;
return [
function() {
return p(_());
},
T === null ? void 0 : function() {
return p(T());
}
];
},
[_, O, S, s]
);
var E = f(i, u[0], u[1]);
return c(
function() {
a.hasValue = !0, a.value = E;
},
[E]
), d(E), E;
}, typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
}()), b;
}
var z;
function Re() {
return z || (z = 1, process.env.NODE_ENV === "production" ? P.exports = ye() : P.exports = me()), P.exports;
}
var he = Re();
const Te = /* @__PURE__ */ Ee(he), { useSyncExternalStoreWithSelector: we } = Te, ge = (e) => e;
function Le(e, t = ge, n) {
const o = we(
e.subscribe,
e.getState,
e.getInitialState,
t,
n
);
return L.useDebugValue(o), o;
}
function V(e) {
return e;
}
const We = Ae;
function Ae(e) {
return (t) => (n, o, f) => {
let l = /* @__PURE__ */ new WeakMap();
const c = /* @__PURE__ */ new WeakMap(), r = /* @__PURE__ */ new WeakMap(), d = /* @__PURE__ */ new WeakMap();
let i;
function _(u) {
i = { ...o(), ...u }, l = /* @__PURE__ */ new WeakMap();
for (const p in i) {
const y = i[p];
typeof y == "object" && y !== null && ue(y, !1);
}
const a = Y(
i,
l,
c,
r
), E = e(a);
return oe(E) ?? E;
}
const O = (u, a) => {
const E = typeof u == "function" ? u(o()) : u, p = { ...o(), ...E };
if (H(
i,
p,
l,
d,
Object.is
)) {
const R = _(p), h = { ...E, ...R };
n(h, a);
} else
n(E, a);
};
Object.assign(f, {
setState: O
});
const S = t(O, o, f), s = _(S);
return { ...S, ...s };
};
}
function se(e) {
const t = A(/* @__PURE__ */ new WeakMap()), n = A(/* @__PURE__ */ new WeakMap()), o = A(/* @__PURE__ */ new WeakMap()), f = A(/* @__PURE__ */ new WeakMap());
return ie(() => {
let l, c;
return (r) => {
if (H(
l,
r,
t.current,
f.current,
Object.is
) || !l) {
l = r, t.current = /* @__PURE__ */ new WeakMap();
for (const O in r) {
const S = r[O];
typeof S == "object" && S !== null && ue(S, !1);
}
const i = Y(
r,
t.current,
n.current,
o.current
), _ = e(i);
c = oe(_) ?? _;
}
return c;
};
}, [e]);
}
const Pe = (e, t = V) => ae(e, se(t)), De = (e) => {
if (e) {
const t = F(e);
return Object.assign((o = V) => Pe(t, o), t);
} else
return (t) => De(t);
}, Me = (e, t = V, n = Object.is) => Le(e, se(t), n), Ve = (e, t = Object.is) => {
if (e) {
const n = F(e), o = t;
return Object.assign((l = V, c = o) => Me(n, l, c), n);
} else
return (n, o = Object.is) => Ve(n, o);
};
export {
De as createTracked,
We as createTrackedComputer,
Ve as createTrackedWithEqualityFn,
Pe as useTrackedStore,
Me as useTrackedStoreWithEqualityFn
};