UNPKG

@sandstack/neuron

Version:

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

3 lines (2 loc) 5.67 kB
"use strict";var M=Object.defineProperty;var b=(c,e,t)=>e in c?M(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var i=(c,e,t)=>(b(c,typeof e!="symbol"?e+"":e,t),t),S=(c,e,t)=>{if(!e.has(c))throw TypeError("Cannot "+t)};var y=(c,e,t)=>(S(c,e,"read from private field"),t?t.call(c):e.get(c)),v=(c,e,t)=>{if(e.has(c))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(c):e.set(c,t)},C=(c,e,t,s)=>(S(c,e,"write to private field"),s?s.call(c,t):e.set(c,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class d{constructor(){i(this,"eventEmitters",new Map);i(this,"payload")}listen(e,t){var s,h;this.eventEmitters.has(e)||this.eventEmitters.set(e,new Set),(s=this.eventEmitters.get(e))==null||s.add(t),((h=this.payload)==null?void 0:h.key)===e&&t(this.payload)}stopListening(e,t){const s=this.eventEmitters.get(e);s&&(s.delete(t),s.size===0&&this.eventEmitters.delete(e))}dispatch(e){this.payload=e;const t=this.eventEmitters.get(e.key);t==null||t.forEach(s=>{s(e)})}}var g;class k{constructor(e){i(this,"key");i(this,"prevState");i(this,"state");v(this,g,!1);i(this,"cancelDispatch",()=>{C(this,g,!0)});i(this,"isDispatchCancelled",()=>y(this,g));this.key=e.key,this.prevState=e.prevState,this.state=e.state}}g=new WeakMap;class E{constructor(e,t,s,h){i(this,"store");i(this,"modules");i(this,"dispatcher");i(this,"key");i(this,"set",e=>{var r,p,a,n;const t=this.store.get(this.key),s=t.state,h=new k({key:this.key,prevState:s,state:typeof e=="function"?e(t==null?void 0:t.state):e});if(h.state!==h.prevState){(r=t==null?void 0:t.onDispatch)==null||r.call(t,h);for(let l=0;l<this.modules.length;l++){if(!h.isDispatchCancelled()){(a=(p=this.modules[l]).onDispatch)==null||a.call(p,h);continue}break}h.isDispatchCancelled()||(this.store.set(this.key,{...t,state:h==null?void 0:h.state,prevState:s}),this.dispatcher.dispatch(h),(n=t==null?void 0:t.onCallback)==null||n.call(t,h),this.modules.forEach(l=>{var f;(f=l==null?void 0:l.onCallback)==null||f.call(l,h)}))}});i(this,"getClone",()=>{var t;const e=(t=this.store.get(this.key))==null?void 0:t.state;return structuredClone!==void 0||structuredClone!==null?structuredClone(e):e});i(this,"getRef",()=>{var e;return(e=this.store.get(this.key))==null?void 0:e.state});i(this,"dispatch",e=>{var h,r,p,a;const t=this.store.get(this.key),s=new k({key:this.key,state:t==null?void 0:t.state,prevState:t==null?void 0:t.state});if(e(s),!s.isDispatchCancelled()){(h=t==null?void 0:t.onDispatch)==null||h.call(t,s);for(let n=0;n<this.modules.length;n++){if(!s.isDispatchCancelled()){(p=(r=this.modules[n]).onDispatch)==null||p.call(r,s);continue}break}}if(!s.isDispatchCancelled()){const n={...t,state:s.state,prevState:t.state};this.store.set(this.key,n),this.dispatcher.dispatch(s),(a=t==null?void 0:t.onCallback)==null||a.call(t,s),this.modules.forEach(l=>{var f;(f=l==null?void 0:l.onCallback)==null||f.call(l,s)})}});i(this,"getActions",()=>{var t;const e=(t=this.store.get(this.key))==null?void 0:t.actions;return(e==null?void 0:e(this.dispatch))??{}});i(this,"effect",e=>{this.dispatcher.stopListening(this.key,e),this.dispatcher.listen(this.key,e)});var p;this.key=(t==null?void 0:t.key)??crypto.randomUUID(),this.store=s??new Map,this.modules=(t==null?void 0:t.modules)??[],this.dispatcher=h??new d;const r=new k({key:this.key,state:e,prevState:e});(p=t==null?void 0:t.onInit)==null||p.call(t,r),this.modules.forEach(a=>{var n;(n=a==null?void 0:a.onInit)==null||n.call(a,r)}),this.store.set(r.key,{...t,key:r.key,state:r==null?void 0:r.state,prevState:r==null?void 0:r.prevState})}}class w{constructor(e){i(this,"clientStore");i(this,"clientModules");i(this,"clientDispatcher");i(this,"name");i(this,"has",e=>this.clientStore.has(e));i(this,"remove",e=>{const t=this.clientStore.delete(e);return this.clientDispatcher.stopListening(e,()=>null),t});i(this,"getRef",e=>{var t;return(t=this.clientStore.get(e))==null?void 0:t.state});i(this,"getActions",e=>{var s;const t=(s=this.clientStore.get(e))==null?void 0:s.actions;return(t==null?void 0:t(h=>this.dispatch(e,h)))??{}});i(this,"getSnapshot",()=>Array.from(this.clientStore.entries()).map(e=>({key:e[1].key,state:e[1].state})));i(this,"listen",e=>{this.clientStore.forEach((t,s)=>{this.clientDispatcher.stopListening(s,e)}),this.clientStore.forEach((t,s)=>{this.clientDispatcher.listen(s,e)})});i(this,"dispatch",(e,t)=>{var r,p;const s=this.clientStore.get(e),h=new k({key:e,state:s==null?void 0:s.state,prevState:s==null?void 0:s.state});t(h),this.clientModules.forEach(a=>{var n;(n=a==null?void 0:a.onDispatch)==null||n.call(a,h)}),(r=s==null?void 0:s.onDispatch)==null||r.call(s,h),this.clientStore.set(e,{...s,state:h.state,prevState:s.state}),this.clientDispatcher.dispatch(h),this.clientModules.forEach(a=>{var n;(n=a==null?void 0:a.onCallback)==null||n.call(a,h)}),(p=s==null?void 0:s.onCallback)==null||p.call(s,h)});i(this,"neuron",(e,t)=>new E(e,{modules:this.clientModules,...t},this.clientStore,this.clientDispatcher));i(this,"client");this.name=(e==null?void 0:e.name)??crypto.randomUUID(),this.clientStore=new Map,this.clientDispatcher=new d,this.clientModules=(e==null?void 0:e.modules)??[],this.client={name:this.name,has:this.has,remove:this.remove,getRef:this.getRef,getActions:this.getActions,getSnapshot:this.getSnapshot,listen:this.listen,dispatch:this.dispatch,neuron:this.neuron}}}class I{constructor(e){i(this,"name");i(this,"onInit");i(this,"onDispatch");i(this,"onCallback");this.name=e.name,this.onInit=e.onInit,this.onDispatch=e.onDispatch,this.onCallback=e.onCallback}}exports.Module=I;exports.Neuron=E;exports.NeuronClient=w; //# sourceMappingURL=index.cjs.map