pinia-react
Version:
Intuitive, type safe and flexible Store for React
2 lines • 4.14 kB
JavaScript
import{ReactiveEffect as e,activeEffect as t,computed as n,effectScope as r,isReactive as i,isRef as a,markRaw as o,nextTick as s,reactive as c,ref as l,toRaw as u,toRefs as d,watch as f}from"@maoism/runtime-core";import{useCallback as p,useEffect as m,useId as h,useRef as g,useSyncExternalStore as _}from"react";import"savage-types";import"savage-utils";const v=()=>y;let y;function b(e){y=e}function x(){let e=r(!0),t=e.run(()=>l({})),n=[],i=o({use(e){return n.push(e),this},_p:n,_e:e,_s:new Map,state:t});return b(i),i}const S=()=>{};function C(e,t,n,r,i=S){t.add(n);let a=()=>{t.delete(n),i()};return r||e[0].push(a),a}function w(e,...t){e.forEach(e=>{e(...t)})}let T=function(e){return e.direct=`direct`,e.patchObject=`patch object`,e.patchFunction=`patch function`,e}({});function E(){return{}}function D(e){return e&&typeof e==`object`&&Object.prototype.toString.call(e)===`[object Object]`&&typeof e.toJSON!=`function`}function O(e,t){for(let n in e instanceof Map&&t instanceof Map&&t.forEach((t,n)=>e.set(n,t)),e instanceof Set&&t instanceof Set&&t.forEach(e.add,e),t){if(!Object.hasOwn(t,n))continue;let r=t[n],o=e[n];D(o)&&D(r)&&e.hasOwnProperty(n)&&!a(r)&&!i(r)?e[n]=O(o,r):e[n]=r}return e}const k=Symbol(),A=Symbol(),{assign:j}=Object;function M(e,t,r){let{state:i,actions:a,getters:s}=t,c=r.state.value[e],l;function u(){c||(r.state.value[e]=i?i():{});let t=d(r.state.value[e]);return j(t,a,Object.keys(s||{}).reduce((i,a)=>(a in t&&console.warn(`[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${a}" in store "${e}".`),i[a]=o(n(()=>{b(r);let t=r._s.get(e);return s[a].call(t,t)})),i),{}))}return l=N(e,u,t,r),l}function N(e,t,n={},i){let a,o=j({actions:{}},n),l={deep:!0},d,p,m=new Set,h=new Set,g=[],_;function v(t){let n;d=p=!1,typeof t==`function`?(t(i.state.value[e]),n={type:T.patchFunction,storeId:e,events:g}):(O(i.state.value[e],t),n={type:T.patchObject,payload:t,storeId:e,events:g}),_=Symbol();let r=_;s().then(()=>{_===r&&(d=!0)}),p=!0,w(m,n,i.state.value[e])}let y=function(){let{state:e}=n,t=e?e():{};this.$patch(e=>{j(e,t)})},x=(t,n=``)=>{if(k in t)return t[A]=n,t;let r=function(){b(i);let n=Array.from(arguments),a=new Set,o=new Set;function s(e){a.add(e)}function c(e){o.add(e)}w(h,{args:n,name:r[A],store:E,after:s,onError:c});let l;try{l=t.apply(this&&this.$id===e?this:E,n)}catch(e){throw w(o,e),e}return l instanceof Promise?l.then(e=>(w(a,e),e)).catch(e=>(w(o,e),Promise.reject(e))):(w(a,l),l)};return r[k]=!0,r[A]=n,r},S={_p:i,$id:e,$onAction:C.bind(null,P,h),$patch:v,$reset:y,$subscribe(t,n={}){let r=C([[]],m,t,n.detached,()=>o()),o=a.run(()=>f(()=>i.state.value[e],r=>{(n.flush===`sync`?p:d)&&t({storeId:e,type:T.direct,events:g},r)},j({},l,n)));return r}},E=c(S);i._s.set(e,E),a=r();let D=a.run(()=>t({action:x}));for(let e in D){let t=D[e];if(typeof t==`function`){let n=x(t,e);D[e]=n,o.actions[e]=t}}return j(E,D),j(u(E),D),Object.defineProperty(E,`$state`,{get:()=>i.state.value[e],set:e=>{v(t=>{j(t,e)})}}),i._p.forEach(e=>{j(E,a.run(()=>e({store:E,pinia:i,options:o})))}),d=!0,p=!0,E}const P=[[]];function F(n,r){let i=new WeakMap,a=new WeakMap,o=new WeakMap;function s(s){if(s&&b(s),!y)throw Error(`[🍍]: "getActivePinia()" was called but there was no active Pinia. Are you trying to use a store before calling "createPinia()"?
`);s=y;let c=t.value;t.value=void 0,s._s.has(n)||M(n,r,s),t.value=c;let l=s._s.get(n),u=g([h()]),d=g({...l}),f=g(!1);o.get(u.current)||o.set(u.current,[]),P[0]=o.get(u.current),m(()=>(P[0]=o.get(u.current),()=>{o.get(u.current).forEach(e=>e())}),[]);let v=p(e=>(a.set(u.current,e),()=>{let e=i.get(u.current);e&&e.stop(),a.delete(u.current),i.delete(u.current)}),[]);_(v,()=>d.current,()=>d.current);let x=i.get(u.current);if(!x){let n=()=>{let e=a.get(u.current);f.current||(d.current={...l},e?.())};x=new e(n,E,()=>{x?.dirty&&x.run()}),t.value=x,f.current=!0,x.run(),i.set(u.current,x),f.current=!1}return l}return s.$id=n,s.$getStore=e=>{e&&b(e),e=y,e._s.has(n)||M(n,r,e);let t=e._s.get(n);return t},s}export{T as MutationType,x as createPinia,F as defineStore,v as getActivePinia,b as setActivePinia};