zustand-tracked
Version:
A small library that allows all your zustand store selectors to be tracked, optimizing computations and rerenders.
34 lines (33 loc) • 9.69 kB
JavaScript
(function(h,v){typeof exports=="object"&&typeof module<"u"?v(exports,require("react"),require("zustand")):typeof define=="function"&&define.amd?define(["exports","react","zustand"],v):(h=typeof globalThis<"u"?globalThis:h||self,v(h.ZustandTracked={},h.React,h.Zustand))})(this,function(h,v,W){"use strict";const de=Symbol(),C=Symbol(),g="a",j="f",N="p",U="c",F="t",z="h",P="w",x="o",q="k";let _e=(e,t)=>new Proxy(e,t);const V=Object.getPrototypeOf,b=new WeakMap,X=e=>e&&(b.has(e)?b.get(e):V(e)===Object.prototype||V(e)===Array.prototype),Z=e=>typeof e=="object"&&e!==null,Se=e=>Object.values(Object.getOwnPropertyDescriptors(e)).some(t=>!t.configurable&&!t.writable),Oe=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(V(e),t)},Ee=(e,t)=>{const n={[j]: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===P)i[P]=!0;else{let _=i[r];_||(_=new Set,i[r]=_),_.add(d)}}},l=()=>{o=!0,n[g].delete(e)},c={get(r,d){return d===C?e:(f(q,d),K(Reflect.get(r,d),n[g],n[U],n[F]))},has(r,d){return d===de?(l(),!0):(f(z,d),Reflect.has(r,d))},getOwnPropertyDescriptor(r,d){return f(x,d),Reflect.getOwnPropertyDescriptor(r,d)},ownKeys(r){return f(P),Reflect.ownKeys(r)}};return t&&(c.set=c.deleteProperty=()=>!1),[c,n]},J=e=>e[C]||e,K=(e,t,n,o)=>{if(!X(e))return e;let f=o&&o.get(e);if(!f){const d=J(e);Se(d)?f=[d,Oe(d)]:f=[d],o==null||o.set(e,f)}const[l,c]=f;let r=n&&n.get(l);return(!r||r[1][j]!==!!c)&&(r=Ee(l,!!c),r[1][N]=_e(c||l,r[0]),n&&n.set(l,r)),r[1][g]=t,r[1][U]=n,r[1][F]=o,r[1][N]},pe=(e,t)=>{const n=Reflect.ownKeys(e),o=Reflect.ownKeys(t);return n.length!==o.length||n.some((f,l)=>f!==o[l])},G=(e,t,n,o,f=Object.is)=>{if(f(e,t))return!1;if(!Z(e)||!Z(t))return!0;const l=n.get(J(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[P]===!0){if(c=pe(e,t),c)return c}else for(const r of l[x]||[]){const d=!!Reflect.getOwnPropertyDescriptor(e,r),i=!!Reflect.getOwnPropertyDescriptor(t,r);if(c=d!==i,c)return c}for(const r of l[q]||[])if(c=G(e[r],t[r],n,o,f),c)return c;if(c===null)throw new Error("invalid used");return c},Q=e=>X(e)&&e[C]||null,$=(e,t=!0)=>{b.set(e,t)};function ye(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var k={exports:{}},I={},D={exports:{}},Y={};/**
* @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 ee;function ve(){if(ee)return Y;ee=1;var e=v;function t(S,O){return S===O&&(S!==0||1/S===1/O)||S!==S&&O!==O}var n=typeof Object.is=="function"?Object.is:t,o=e.useState,f=e.useEffect,l=e.useLayoutEffect,c=e.useDebugValue;function r(S,O){var s=O(),u=o({inst:{value:s,getSnapshot:O}}),a=u[0].inst,E=u[1];return l(function(){a.value=s,a.getSnapshot=O,d(a)&&E({inst:a})},[S,s,O]),f(function(){return d(a)&&E({inst:a}),S(function(){d(a)&&E({inst:a})})},[S]),c(s),s}function d(S){var O=S.getSnapshot;S=S.value;try{var s=O();return!n(S,s)}catch{return!0}}function i(S,O){return O()}var _=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?i:r;return Y.useSyncExternalStore=e.useSyncExternalStore!==void 0?e.useSyncExternalStore:_,Y}var H={};/**
* @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 te;function me(){return te||(te=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(!S){var E=u();l(a,E)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),S=!0)}E=c({inst:{value:a,getSnapshot:u}});var p=E[0].inst,m=E[1];return d(function(){p.value=a,p.getSnapshot=u,n(p)&&m({inst:p})},[s,a,u]),r(function(){return n(p)&&m({inst:p}),s(function(){n(p)&&m({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=v,l=typeof Object.is=="function"?Object.is:e,c=f.useState,r=f.useEffect,d=f.useLayoutEffect,i=f.useDebugValue,_=!1,S=!1,O=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?o:t;H.useSyncExternalStore=f.useSyncExternalStore!==void 0?f.useSyncExternalStore:O,typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop=="function"&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()),H}var re;function ne(){return re||(re=1,process.env.NODE_ENV==="production"?D.exports=ve():D.exports=me()),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 oe;function Re(){if(oe)return I;oe=1;var e=v,t=ne();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 I.useSyncExternalStoreWithSelector=function(i,_,S,O,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(y){if(!m){if(m=!0,T=y,y=O(y),s!==void 0&&a.hasValue){var R=a.value;if(s(R,y))return w=R}return w=y}if(R=w,o(T,y))return R;var A=O(y);return s!==void 0&&s(R,A)?(T=y,R):(T=y,w=A)}var m=!1,T,w,L=S===void 0?null:S;return[function(){return p(_())},L===null?void 0:function(){return p(L())}]},[_,S,O,s]);var E=f(i,u[0],u[1]);return c(function(){a.hasValue=!0,a.value=E},[E]),d(E),E},I}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 ue;function he(){return ue||(ue=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=v,n=ne(),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,_,S,O,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(y){if(!m){if(m=!0,T=y,y=O(y),s!==void 0&&a.hasValue){var R=a.value;if(s(R,y))return w=R}return w=y}if(R=w,o(T,y))return R;var A=O(y);return s!==void 0&&s(R,A)?(T=y,R):(T=y,w=A)}var m=!1,T,w,L=S===void 0?null:S;return[function(){return p(_())},L===null?void 0:function(){return p(L())}]},[_,S,O,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 ce;function Te(){return ce||(ce=1,process.env.NODE_ENV==="production"?k.exports=Re():k.exports=he()),k.exports}var we=Te();const ge=ye(we),{useSyncExternalStoreWithSelector:Le}=ge,Ae=e=>e;function Pe(e,t=Ae,n){const o=Le(e.subscribe,e.getState,e.getInitialState,t,n);return v.useDebugValue(o),o}function M(e){return e}const ke=De;function De(e){return t=>(n,o,f)=>{let l=new WeakMap;const c=new WeakMap,r=new WeakMap,d=new WeakMap;let i;function _(u){i={...o(),...u},l=new WeakMap;for(const p in i){const m=i[p];typeof m=="object"&&m!==null&&$(m,!1)}const a=K(i,l,c,r),E=e(a);return Q(E)??E}const S=(u,a)=>{const E=typeof u=="function"?u(o()):u,p={...o(),...E};if(G(i,p,l,d,Object.is)){const T=_(p),w={...E,...T};n(w,a)}else n(E,a)};Object.assign(f,{setState:S});const O=t(S,o,f),s=_(O);return{...O,...s}}}function se(e){const t=v.useRef(new WeakMap),n=v.useRef(new WeakMap),o=v.useRef(new WeakMap),f=v.useRef(new WeakMap);return v.useMemo(()=>{let l,c;return r=>{if(G(l,r,t.current,f.current,Object.is)||!l){l=r,t.current=new WeakMap;for(const S in r){const O=r[S];typeof O=="object"&&O!==null&&$(O,!1)}const i=K(r,t.current,n.current,o.current),_=e(i);c=Q(_)??_}return c}},[e])}const ie=(e,t=M)=>W.useStore(e,se(t)),ae=e=>{if(e){const t=W.createStore(e);return Object.assign((o=M)=>ie(t,o),t)}else return t=>ae(t)},fe=(e,t=M,n=Object.is)=>Pe(e,se(t),n),le=(e,t=Object.is)=>{if(e){const n=W.createStore(e),o=t;return Object.assign((l=M,c=o)=>fe(n,l,c),n)}else return(n,o=Object.is)=>le(n,o)};h.createTracked=ae,h.createTrackedComputer=ke,h.createTrackedWithEqualityFn=le,h.useTrackedStore=ie,h.useTrackedStoreWithEqualityFn=fe,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});