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