UNPKG

@sandstack/neuron

Version:

Neuron is a lightweight framework agnostic global state manager for Javascript apps, with React support

3 lines (2 loc) 3.02 kB
var react=function(o,d,n,I){"use strict";var z=Object.defineProperty;var B=(o,d,n)=>d in o?z(o,d,{enumerable:!0,configurable:!0,writable:!0,value:n}):o[d]=n;var m=(o,d,n)=>(B(o,typeof d!="symbol"?d+"":d,n),n);function M(s,t){const[e,u]=n.useState(t?t(s.getRef()):s.getRef()),r=a=>s.set(a),c=a=>{var f;if(t){const S=(f=t==null?void 0:t.toString().match(/\(\w+\) =>\s*(\S+)/))==null?void 0:f[1],h=typeof S=="string"?S==null?void 0:S.split(".").slice(1):[],N=s.getRef(),A=h.reduce((l,i)=>l[i],N);let R;typeof a=="function"?R=a==null?void 0:a(A):R=a;const p=((l,i,v)=>{const P=i.pop(),U=i.reduce((j,b)=>j[b]=j[b]||{},l);return U[P]=v,l})(N,h,R);s.set({...p})}},g=t?{...s.getActions(),set:r,setSlice:c}:{...s.getActions(),set:r};return n.useEffect(()=>{s.effect(a=>{u(t?t(a.state):a.state)})},[]),[e,g]}const O=(s,t,e)=>{const u=(e==null?void 0:e(s,t))??new d.Neuron(s,t);return r=>M(u,r)},_=(s,t)=>{const[e,u]=n.useState(t.getRef(s)),r=c=>{t.dispatch(s,g=>{g.state=typeof c=="function"?c==null?void 0:c(g.prevState):c})};return n.useEffect(()=>{u(t.getRef(s))},[s]),n.useEffect(()=>{t.listen(c=>{c.key===s&&u(c.state)})},[s]),[e,r]};class x{constructor(t){m(this,"neuron");m(this,"useNeuron",t=>_(t,this.client));m(this,"name");m(this,"client");const e=new d.NeuronClient(t);this.name=t==null?void 0:t.name;const u=t==null?void 0:t.modules;this.neuron=(r,c)=>O(r,{modules:u,...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 q(s,t,e){const[u,r]=n.useState(e?e(s.getRef(t)):s.getRef(t)),c=f=>s.dispatch(t,S=>S.state=f),g=f=>{var S;if(e){const h=(S=e==null?void 0:e.toString().match(/\(\w+\) =>\s*(\S+)/))==null?void 0:S[1],N=typeof h=="string"?h==null?void 0:h.split(".").slice(1):[],A=s.getRef(t),R=N.reduce((i,v)=>i[v],A);let C;typeof f=="function"?C=f==null?void 0:f(R):C=f;const l=((i,v,P)=>{const U=v.pop(),j=v.reduce((b,D)=>b[D]=b[D]||{},i);return j[U]=P,i})(A,N,C);s.dispatch(t,i=>i.state={...l})}},a=e?{...s.getActions(t),set:c,setSlice:g}:{...s.getActions(t),set:c};return n.useEffect(()=>{s.listen(f=>{f.key===t&&r(e?e(f.state):f.state)})},[]),[u,a]}class E{constructor(t){m(this,"clientContext");m(this,"modules");m(this,"privateNeuron",(t,e)=>{const u=(e==null?void 0:e.key)??crypto.randomUUID();return[g=>{g.neuron(t,{...e,key:u})},g=>{const a=n.useContext(this.clientContext);return q(a,u,g)}]});m(this,"useNeuronClient",t=>{const e=new d.NeuronClient({name:t==null?void 0:t.name,modules:this.modules});return{client:e,Private:({children:r})=>{const c=this.clientContext;return I.jsx(c.Provider,{value:e,children:r})}}});this.modules=(t==null?void 0:t.modules)??[],this.clientContext=n.createContext(null)}}return globalThis.ReactNeuron={neuron:O,NeuronClient:x,PrivateNeuronClient:E},o.NeuronClient=x,o.PrivateNeuronClient=E,o.neuron=O,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({},core,react,jsxRuntime); //# sourceMappingURL=react.js.map