mute8-solid
Version:
Solid State Container
9 lines (6 loc) • 2.18 kB
JavaScript
;
var solidJs = require('solid-js');
var y=Object,P=JSON,l=P.stringify,m=i=>P.parse(l(i)),A=y.freeze,T=y.assign,h=y.entries,f=y.defineProperty,p="mut",u=Symbol(),O=()=>({BI:i=>i,BU:i=>i,AC:()=>{}}),d=class i{id=0;c={};s;p;ps;constructor(t,e){this.p=e??O(),this.s=A(this.p.BI(t));}destroy(){this.ps?.destroy(),this.ps=null;}sanp(){return this.s}sub(t){let e=this.id++;return this.c[e]=t,{destroy:()=>delete this.c[e]}}select(t){let e=new i(t(this.sanp()));return e.ps=this.sub(n=>e.mut(t(n))),e}mut(t,e,n){let s=t;this.ps||(s=this.p.BU(T(m(this.s),t))),l(this.s)!==l(s)&&(this.p.AC(this.s,s,e,n),this.s=A(s),y.keys(this.c).forEach(r=>this.c[r]?.(this.s)));}set(t,e){let n="set."+t,s=t===p?e:{[t]:e};this.mut(s,n,[e]);}},x=class i{p;s;constructor(t){let e=this;this.s=new d(t.value);let n=b(t.async??{},(o,a)=>async(...c)=>await a.bind(e.p)(...c)),s=b(t.actions??{},(o,a)=>(...c)=>e.mut(g=>a.bind(g)(...c),o,c));e[u]=b(t.actions??{},(o,a)=>(...c)=>a.bind(e[u][u])(...c)),this.p={snap:()=>e.s.sanp(),sub:o=>e.s.sub(o),select:o=>e.s.select(o),actions:A(s),async:A(n),set mut(o){e.s.set(p,o);},get mut(){return e.mut.bind(e)}};let r=t.plugin?.(e.p)??O();this.s=new d(t.value,r);}mut(t,e,n){let s=this,r=m(s.s.sanp());s[u][u]=r,T(r,{actions:s[u]}),t(r),delete r.actions,s.s.mut(r,e,n??t);}static build(t,e){let n=new i(t),s=e?{[e.name]:e.init(n)}:{},r=T(s,n.p),o=n.s,a={};for(let[c,g]of h(t.value))f(a,c,{get(){return o.sanp()[c]},set(R){o.set(c,R);}});return f(a,p,{get(){return n.mut.bind(n)},set(c){o.set(p,c);}}),A(T(a,r))}},b=(i,t)=>{let e={};return h(i).forEach(([n,s])=>e[n]=t(n,s)),e},F=x.build;var j={equals:(i,t)=>l(i)==l(t)},V=i=>F(i,{name:"solid",init(e){return {use(){let[n,s]=solidJs.createSignal(e.s.sanp()),r=e.s.sub(o=>s(o));return solidJs.onCleanup(()=>r.destroy()),[n,o=>e.s.mut(o)]},useOne(n){let[s,r]=solidJs.createSignal(e.s.sanp()[n],j),o=e.s.sub(a=>r(a[n]));return solidJs.onCleanup(()=>o.destroy()),[s,a=>e.s.set(n,a)]},select(n){let[s,r]=solidJs.createSignal(n(e.s.sanp())),o=e.s.select(n);return o.sub(a=>r(a)),solidJs.onCleanup(()=>o.destroy()),s}}}});
exports.newStore = V;
//# sourceMappingURL=out.js.map
//# sourceMappingURL=mute8-solid.cjs.map