@sandstack/neuron
Version:
Neuron is a lightweight framework agnostic global state manager for Javascript apps, with React support
3 lines (2 loc) • 3.05 kB
JavaScript
var q=Object.defineProperty;var D=(s,t,e)=>t in s?q(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var d=(s,t,e)=>(D(s,typeof t!="symbol"?t+"":t,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("../core"),g=require("react"),I=require("react/jsx-runtime");function M(s,t){const[e,r]=g.useState(t?t(s.getRef()):s.getRef()),n=u=>s.set(u),c=u=>{var a;if(t){const i=(a=t==null?void 0:t.toString().match(/\(\w+\) =>\s*(\S+)/))==null?void 0:a[1],S=typeof i=="string"?i==null?void 0:i.split(".").slice(1):[],l=s.getRef(),R=S.reduce((m,f)=>m[f],l);let v;typeof u=="function"?v=u==null?void 0:u(R):v=u;const P=((m,f,h)=>{const A=f.pop(),C=f.reduce((x,N)=>x[N]=x[N]||{},m);return C[A]=h,m})(l,S,v);s.set({...P})}},o=t?{...s.getActions(),set:n,setSlice:c}:{...s.getActions(),set:n};return g.useEffect(()=>{s.effect(u=>{r(t?t(u.state):u.state)})},[]),[e,o]}const O=(s,t,e)=>{const r=(e==null?void 0:e(s,t))??new j.Neuron(s,t);return n=>M(r,n)},_=(s,t)=>{const[e,r]=g.useState(t.getRef(s)),n=c=>{t.dispatch(s,o=>{o.state=typeof c=="function"?c==null?void 0:c(o.prevState):c})};return g.useEffect(()=>{r(t.getRef(s))},[s]),g.useEffect(()=>{t.listen(c=>{c.key===s&&r(c.state)})},[s]),[e,n]};class p{constructor(t){d(this,"neuron");d(this,"useNeuron",t=>_(t,this.client));d(this,"name");d(this,"client");const e=new j.NeuronClient(t);this.name=t==null?void 0:t.name;const r=t==null?void 0:t.modules;this.neuron=(n,c)=>O(n,{modules:r,...c},e.neuron),this.client={name:e.name,has:e.has,getRef:e.getRef,getSnapshot:e.getSnapshot,listen:e.listen,dispatch:e.dispatch,neuron:e.neuron,getActions:e.getActions,remove:e.remove}}}function z(s,t,e){const[r,n]=g.useState(e?e(s.getRef(t)):s.getRef(t)),c=a=>s.dispatch(t,i=>i.state=a),o=a=>{var i;if(e){const S=(i=e==null?void 0:e.toString().match(/\(\w+\) =>\s*(\S+)/))==null?void 0:i[1],l=typeof S=="string"?S==null?void 0:S.split(".").slice(1):[],R=s.getRef(t),v=l.reduce((f,h)=>f[h],R);let b;typeof a=="function"?b=a==null?void 0:a(v):b=a;const m=((f,h,A)=>{const C=h.pop(),x=h.reduce((N,U)=>N[U]=N[U]||{},f);return x[C]=A,f})(R,l,b);s.dispatch(t,f=>f.state={...m})}},u=e?{...s.getActions(t),set:c,setSlice:o}:{...s.getActions(t),set:c};return g.useEffect(()=>{s.listen(a=>{a.key===t&&n(e?e(a.state):a.state)})},[]),[r,u]}class E{constructor(t){d(this,"clientContext");d(this,"modules");d(this,"privateNeuron",(t,e)=>{const r=(e==null?void 0:e.key)??crypto.randomUUID();return[o=>{o.neuron(t,{...e,key:r})},o=>{const u=g.useContext(this.clientContext);return z(u,r,o)}]});d(this,"useNeuronClient",t=>{const e=new j.NeuronClient({name:t==null?void 0:t.name,modules:this.modules});return{client:e,Private:({children:n})=>{const c=this.clientContext;return I.jsx(c.Provider,{value:e,children:n})}}});this.modules=(t==null?void 0:t.modules)??[],this.clientContext=g.createContext(null)}}globalThis.ReactNeuron={neuron:O,NeuronClient:p,PrivateNeuronClient:E};exports.NeuronClient=p;exports.PrivateNeuronClient=E;exports.neuron=O;
//# sourceMappingURL=index.cjs.map
;