@sandstack/neuron
Version:
Neuron is a lightweight framework agnostic global state manager for Javascript apps, with React support
3 lines (2 loc) • 5.86 kB
JavaScript
(function(c,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(c=typeof globalThis<"u"?globalThis:c||self,a(c.core={}))})(this,function(c){"use strict";var o=Object.defineProperty;var I=(c,a,n)=>a in c?o(c,a,{enumerable:!0,configurable:!0,writable:!0,value:n}):c[a]=n;var i=(c,a,n)=>(I(c,typeof a!="symbol"?a+"":a,n),n),v=(c,a,n)=>{if(!a.has(c))throw TypeError("Cannot "+n)};var C=(c,a,n)=>(v(c,a,"read from private field"),n?n.call(c):a.get(c)),E=(c,a,n)=>{if(a.has(c))throw TypeError("Cannot add the same private member more than once");a instanceof WeakSet?a.add(c):a.set(c,n)},b=(c,a,n,y)=>(v(c,a,"write to private field"),y?y.call(c,n):a.set(c,n),n);var k;class a{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)})}}class n{constructor(e){i(this,"key");i(this,"prevState");i(this,"state");E(this,k,!1);i(this,"cancelDispatch",()=>{b(this,k,!0)});i(this,"isDispatchCancelled",()=>C(this,k));this.key=e.key,this.prevState=e.prevState,this.state=e.state}}k=new WeakMap;class y{constructor(e,t,s,h){i(this,"store");i(this,"modules");i(this,"dispatcher");i(this,"key");i(this,"set",e=>{var l,g,r,f;const t=this.store.get(this.key),s=t.state,h=new n({key:this.key,prevState:s,state:typeof e=="function"?e(t==null?void 0:t.state):e});if(h.state!==h.prevState){(l=t==null?void 0:t.onDispatch)==null||l.call(t,h);for(let p=0;p<this.modules.length;p++){if(!h.isDispatchCancelled()){(r=(g=this.modules[p]).onDispatch)==null||r.call(g,h);continue}break}h.isDispatchCancelled()||(this.store.set(this.key,{...t,state:h==null?void 0:h.state,prevState:s}),this.dispatcher.dispatch(h),(f=t==null?void 0:t.onCallback)==null||f.call(t,h),this.modules.forEach(p=>{var S;(S=p==null?void 0:p.onCallback)==null||S.call(p,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,l,g,r;const t=this.store.get(this.key),s=new n({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 f=0;f<this.modules.length;f++){if(!s.isDispatchCancelled()){(g=(l=this.modules[f]).onDispatch)==null||g.call(l,s);continue}break}}if(!s.isDispatchCancelled()){const f={...t,state:s.state,prevState:t.state};this.store.set(this.key,f),this.dispatcher.dispatch(s),(r=t==null?void 0:t.onCallback)==null||r.call(t,s),this.modules.forEach(p=>{var S;(S=p==null?void 0:p.onCallback)==null||S.call(p,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 g;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 a;const l=new n({key:this.key,state:e,prevState:e});(g=t==null?void 0:t.onInit)==null||g.call(t,l),this.modules.forEach(r=>{var f;(f=r==null?void 0:r.onInit)==null||f.call(r,l)}),this.store.set(l.key,{...t,key:l.key,state:l==null?void 0:l.state,prevState:l==null?void 0:l.prevState})}}class M{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 l,g;const s=this.clientStore.get(e),h=new n({key:e,state:s==null?void 0:s.state,prevState:s==null?void 0:s.state});t(h),this.clientModules.forEach(r=>{var f;(f=r==null?void 0:r.onDispatch)==null||f.call(r,h)}),(l=s==null?void 0:s.onDispatch)==null||l.call(s,h),this.clientStore.set(e,{...s,state:h.state,prevState:s.state}),this.clientDispatcher.dispatch(h),this.clientModules.forEach(r=>{var f;(f=r==null?void 0:r.onCallback)==null||f.call(r,h)}),(g=s==null?void 0:s.onCallback)==null||g.call(s,h)});i(this,"neuron",(e,t)=>new y(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 a,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 w{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}}c.Module=w,c.Neuron=y,c.NeuronClient=M,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=core.js.map