@storix-js/sync 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var at=Symbol.for("preact-signals");function W(){if(k>1)k--;else{for(var e,t=!1;D!==void 0;){var o=D;for(D=void 0,B++;o!==void 0;){var l=o.o;if(o.o=void 0,o.f&=-3,!(8&o.f)&&nt(o))try{o.c()}catch(x){t||(e=x,t=!0)}o=l}}if(B=0,k--,t)throw e}}function Z(e){if(k>0)return e();k++;try{return e()}finally{W()}}var u=void 0;function tt(e){var t=u;u=void 0;try{return e()}finally{u=t}}var D=void 0,k=0,B=0,J=0;function et(e){if(u!==void 0){var t=e.n;if(t===void 0||t.t!==u)return t={i:0,S:e,p:u.s,n:void 0,t:u,e:void 0,x:void 0,r:t},u.s!==void 0&&(u.s.n=t),u.s=t,e.n=t,32&u.f&&e.S(t),t;if(t.i===-1)return t.i=0,t.n!==void 0&&(t.n.p=t.p,t.p!==void 0&&(t.p.n=t.n),t.p=u.s,t.n=void 0,u.s.n=t,u.s=t),t}}function m(e,t){this.v=e,this.i=0,this.n=void 0,this.t=void 0,this.W=t==null?void 0:t.watched,this.Z=t==null?void 0:t.unwatched,this.name=t==null?void 0:t.name}m.prototype.brand=at;m.prototype.h=function(){return!0};m.prototype.S=function(e){var t=this,o=this.t;o!==e&&e.e===void 0&&(e.x=o,this.t=e,o!==void 0?o.e=e:tt(function(){var l;(l=t.W)==null||l.call(t)}))};m.prototype.U=function(e){var t=this;if(this.t!==void 0){var o=e.e,l=e.x;o!==void 0&&(o.x=l,e.e=void 0),l!==void 0&&(l.e=o,e.x=void 0),e===this.t&&(this.t=l,l===void 0&&tt(function(){var x;(x=t.Z)==null||x.call(t)}))}};m.prototype.subscribe=function(e){var t=this;return ut(function(){var o=t.value,l=u;u=void 0;try{e(o)}finally{u=l}},{name:"sub"})};m.prototype.valueOf=function(){return this.value};m.prototype.toString=function(){return this.value+""};m.prototype.toJSON=function(){return this.value};m.prototype.peek=function(){var e=u;u=void 0;try{return this.value}finally{u=e}};Object.defineProperty(m.prototype,"value",{get:function(){var e=et(this);return e!==void 0&&(e.i=this.i),this.v},set:function(e){if(e!==this.v){if(B>100)throw new Error("Cycle detected");this.v=e,this.i++,J++,k++;try{for(var t=this.t;t!==void 0;t=t.x)t.t.N()}finally{W()}}}});function ft(e,t){return new m(e,t)}function nt(e){for(var t=e.s;t!==void 0;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function it(e){for(var t=e.s;t!==void 0;t=t.n){var o=t.S.n;if(o!==void 0&&(t.r=o),t.S.n=t,t.i=-1,t.n===void 0){e.s=t;break}}}function ot(e){for(var t=e.s,o=void 0;t!==void 0;){var l=t.p;t.i===-1?(t.S.U(t),l!==void 0&&(l.n=t.n),t.n!==void 0&&(t.n.p=l)):o=t,t.S.n=t.r,t.r!==void 0&&(t.r=void 0),t=l}e.s=o}function R(e,t){m.call(this,void 0),this.x=e,this.s=void 0,this.g=J-1,this.f=4,this.W=t==null?void 0:t.watched,this.Z=t==null?void 0:t.unwatched,this.name=t==null?void 0:t.name}R.prototype=new m;R.prototype.h=function(){if(this.f&=-3,1&this.f)return!1;if((36&this.f)==32||(this.f&=-5,this.g===J))return!0;if(this.g=J,this.f|=1,this.i>0&&!nt(this))return this.f&=-2,!0;var e=u;try{it(this),u=this;var t=this.x();(16&this.f||this.v!==t||this.i===0)&&(this.v=t,this.f&=-17,this.i++)}catch(o){this.v=o,this.f|=16,this.i++}return u=e,ot(this),this.f&=-2,!0};R.prototype.S=function(e){if(this.t===void 0){this.f|=36;for(var t=this.s;t!==void 0;t=t.n)t.S.S(t)}m.prototype.S.call(this,e)};R.prototype.U=function(e){if(this.t!==void 0&&(m.prototype.U.call(this,e),this.t===void 0)){this.f&=-33;for(var t=this.s;t!==void 0;t=t.n)t.S.U(t)}};R.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var e=this.t;e!==void 0;e=e.x)e.t.N()}};Object.defineProperty(R.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var e=et(this);if(this.h(),e!==void 0&&(e.i=this.i),16&this.f)throw this.v;return this.v}});function Y(e,t){return new R(e,t)}function st(e){var t=e.u;if(e.u=void 0,typeof t=="function"){k++;var o=u;u=void 0;try{t()}catch(l){throw e.f&=-2,e.f|=8,K(e),l}finally{u=o,W()}}}function K(e){for(var t=e.s;t!==void 0;t=t.n)t.S.U(t);e.x=void 0,e.s=void 0,st(e)}function ct(e){if(u!==this)throw new Error("Out-of-order effect");ot(this),u=e,this.f&=-2,8&this.f&&K(this),W()}function C(e,t){this.x=e,this.u=void 0,this.s=void 0,this.o=void 0,this.f=32,this.name=t==null?void 0:t.name}C.prototype.c=function(){var e=this.S();try{if(8&this.f||this.x===void 0)return;var t=this.x();typeof t=="function"&&(this.u=t)}finally{e()}};C.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,st(this),it(this),k++;var e=u;return u=this,ct.bind(this,e)};C.prototype.N=function(){2&this.f||(this.f|=2,this.o=D,D=this)};C.prototype.d=function(){this.f|=8,1&this.f||K(this)};C.prototype.dispose=function(){this.d()};function ut(e,t){var o=new C(e,t);try{o.c()}catch(x){throw o.d(),x}var l=o.d.bind(o);return l[Symbol.dispose]=l,l}function lt(e={}){const t=(e.namespace??"default").trim()||"default",o=e.plugins??[],l=new Map;function x(S,j){return function(){var _;const N=`${t}:${S}`,E=l.get(S);if(E)return E;const U=j.state(),I=j.modules??{},H={...U};for(const[i,n]of Object.entries(I))H[i]=n.state();const h=ft(H),y=new Map,A=new Map,L=new Map,M=new Map,b=new Map,T=new Set,d=new Set(Object.keys(U)),a=new Set(Object.keys(j.getters??{})),p=new Set(Object.keys(j.actions??{})),w=i=>h.value[i]??{},q=(i,n)=>{h.value={...h.value,[i]:n}},rt=new Proxy({},{get(i,n){var r,f;if(y.has(n))return(r=y.get(n))==null?void 0:r.value;if(b.has(n))return(f=b.get(n))==null?void 0:f.value;if(d.has(n))return h.value[n];const v=M.get(n);return v?w(v.moduleName)[v.key]:h.value[n]}});for(const[i,n]of Object.entries(j.getters??{}))y.set(i,Y(()=>n(h.value,rt)));const F=(i,n)=>{var r;for(const f of T)f(i);for(const f of o)(r=f.onMutation)==null||r.call(f,n,i)},G=(i,n,r,f)=>{const v=h.value[i];Object.is(v,n)||(h.value={...h.value,[i]:n},F({namespace:t,scopeId:N,storeId:S,key:i,oldValue:v,newValue:n,origin:r},f))},z=(i,n,r,f,v)=>{const g=w(i),s=g[n];Object.is(s,r)||(q(i,{...g,[n]:r}),F({namespace:t,scopeId:N,storeId:S,key:`${i}.${n}`,oldValue:s,newValue:r,origin:f},v))},P={$id:S,$namespace:t,$scopeId:N,$subscribe(i){return T.add(i),()=>T.delete(i)},$dispose(){var i;T.clear();for(const n of o)(i=n.onDispose)==null||i.call(n,$);l.delete(S)},$patch(i,n="local"){Z(()=>{for(const[r,f]of Object.entries(i)){const v=M.get(r);if(v){z(v.moduleName,v.key,f,n,$);continue}if(r.includes(".")){const[g,s]=r.split(".",2);if(I[g]){z(g,s,f,n,$);continue}}if(I[r]&&f&&typeof f=="object"&&!Array.isArray(f)){for(const[g,s]of Object.entries(f))z(r,g,s,n,$);continue}G(r,f,n,$)}})},$snapshot(){return{...h.value}}};for(const[i,n]of Object.entries(I)){const r=new Map,f=new Map,v=new Proxy({},{get(s,c){var O,V,Q;if(r.has(c))return(O=r.get(c))==null?void 0:O.value;if(y.has(c))return(V=y.get(c))==null?void 0:V.value;if(b.has(c))return(Q=b.get(c))==null?void 0:Q.value;const X=w(i);if(c in X)return X[c];if(d.has(c))return h.value[c]}});for(const[s,c]of Object.entries(n.getters??{}))r.set(s,Y(()=>c(w(i),h.value,v)));let g;for(const[s,c]of Object.entries(n.actions??{}))f.set(s,(...O)=>Z(()=>c.apply(g,O)));g=new Proxy({},{get(s,c){var O;if(f.has(c))return f.get(c);if(r.has(c))return(O=r.get(c))==null?void 0:O.value;const V=w(i);return c in V?V[c]:$[c]},set(s,c,O){if(f.has(c)||r.has(c))return!1;const V=w(i);return c in V?z(i,c,O,"local",$):$[c]=O,!0}}),L.set(i,g),A.set(i,r);for(const s of Object.keys(w(i)))!(d.has(s)||a.has(s)||p.has(s)||Reflect.has(P,s)||I[s]!==void 0)&&!M.has(s)&&M.set(s,{moduleName:i,key:s});for(const[s,c]of r.entries())!(d.has(s)||a.has(s)||p.has(s)||Reflect.has(P,s)||I[s]!==void 0)&&!b.has(s)&&b.set(s,c);for(const[s,c]of f.entries())!(d.has(s)||a.has(s)||p.has(s)||Reflect.has(P,s)||I[s]!==void 0)&&!Reflect.has(P,s)&&(P[s]=c)}const $=new Proxy(P,{get(i,n,r){var f,v;if(Reflect.has(i,n))return Reflect.get(i,n,r);if(L.has(n))return L.get(n);if(y.has(n))return(f=y.get(n))==null?void 0:f.value;if(b.has(n))return(v=b.get(n))==null?void 0:v.value;if(d.has(n))return h.value[n];const g=M.get(n);return g?w(g.moduleName)[g.key]:h.value[n]},set(i,n,r,f){if(Reflect.has(i,n))return Reflect.set(i,n,r,f);const v=M.get(n);return v?(z(v.moduleName,v.key,r,"local",f),!0):(G(n,r,"local",f),!0)}});for(const[i,n]of Object.entries(j.actions??{}))P[i]=(...r)=>Z(()=>n.apply($,r));for(const i of o)(_=i.onInit)==null||_.call(i,$);return l.set(S,$),$}}return{defineStore:x,use(S){o.push(S)}}}lt();function dt(e={}){const t=e.channelName??"storix-sync",o=new Set(e.include??[]),l=new Set(e.exclude??[]),x=e.namespaces??[],S=x==="*"?null:new Set(x),j=e.debounce??16,_=crypto.randomUUID(),N=`__storix_sync__:${t}`,E=new Map,U=new Set,I=new Map,H=new Map;let h=null;const y=typeof window<"u"&&"BroadcastChannel"in window?new BroadcastChannel(t):null,A=d=>{const a=[d.$id,d.$scopeId];return!(a.some(p=>l.has(p))||o.size>0&&!a.some(p=>o.has(p)))},L=(d,a)=>a===d||S===null?!0:S.has(a),M=(d,a)=>{!a||a.originTabId===_||L(d.$namespace,a.namespace)&&a.storeId===d.$id&&A(d)&&d.$patch({[a.key]:a.newValue},"remote")},b=()=>{if(h=null,E.size===0)return;const d=Array.from(E.values());E.clear();for(const a of d){const p={...a,originTabId:_};if(y&&y.postMessage(p),typeof window<"u"&&"localStorage"in window)try{window.localStorage.setItem(N,JSON.stringify(p)),window.localStorage.removeItem(N)}catch{}}},T=d=>{if(E.set(`${d.scopeId}:${d.key}`,d),j<=0){b();return}h||(h=setTimeout(b,j))};return{name:"sync",onInit(d){const a=d;if(A(a)){if(U.add(a.$scopeId),y){const p=w=>{M(a,w.data)};I.set(a.$scopeId,p),y.addEventListener("message",p)}if(typeof window<"u"){const p=w=>{if(!(w.key!==N||!w.newValue))try{const q=JSON.parse(w.newValue);M(a,q)}catch{}};H.set(a.$scopeId,p),window.addEventListener("storage",p)}}},onMutation(d,a){a.origin!=="remote"&&A(d)&&T(a)},onDispose(d){const a=d;U.delete(a.$scopeId);const p=I.get(a.$scopeId);y&&p&&(y.removeEventListener("message",p),I.delete(a.$scopeId));const w=H.get(a.$scopeId);typeof window<"u"&&w&&(window.removeEventListener("storage",w),H.delete(a.$scopeId)),h&&(clearTimeout(h),h=null),b(),U.size===0&&(y==null||y.close())}}}exports.syncPlugin=dt;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var fe=Symbol.for("preact-signals");function G(){if(H>1)H--;else{var t,e=!1;for((function(){var w=K;for(K=void 0;w!==void 0;)w.S.v===w.v&&(w.S.i=w.i),w=w.o})();Z!==void 0;){var s=Z;for(Z=void 0,B++;s!==void 0;){var l=s.u;if(s.u=void 0,s.f&=-3,!(8&s.f)&&ae(s))try{s.c()}catch(w){e||(t=w,e=!0)}s=l}}if(B=0,H--,e)throw t}}function L(t){if(H>0)return t();X=++ve,H++;try{return t()}finally{G()}}var p=void 0;function Y(t){var e=p;p=void 0;try{return t()}finally{p=e}}var Z=void 0,H=0,B=0,ve=0,X=0,K=void 0,F=0;function re(t){if(p!==void 0){var e=t.n;if(e===void 0||e.t!==p)return e={i:0,S:t,p:p.s,n:void 0,t:p,e:void 0,x:void 0,r:e},p.s!==void 0&&(p.s.n=e),p.s=e,t.n=e,32&p.f&&t.S(e),e;if(e.i===-1)return e.i=0,e.n!==void 0&&(e.n.p=e.p,e.p!==void 0&&(e.p.n=e.n),e.p=p.s,e.n=void 0,p.s.n=e,p.s=e),e}}function x(t,e){this.v=t,this.i=0,this.n=void 0,this.t=void 0,this.l=0,this.W=e==null?void 0:e.watched,this.Z=e==null?void 0:e.unwatched,this.name=e==null?void 0:e.name}x.prototype.brand=fe;x.prototype.h=function(){return!0};x.prototype.S=function(t){var e=this,s=this.t;s!==t&&t.e===void 0&&(t.x=s,this.t=t,s!==void 0?s.e=t:Y(function(){var l;(l=e.W)==null||l.call(e)}))};x.prototype.U=function(t){var e=this;if(this.t!==void 0){var s=t.e,l=t.x;s!==void 0&&(s.x=l,t.e=void 0),l!==void 0&&(l.e=s,t.x=void 0),t===this.t&&(this.t=l,l===void 0&&Y(function(){var w;(w=e.Z)==null||w.call(e)}))}};x.prototype.subscribe=function(t){var e=this;return he(function(){var s=e.value,l=p;p=void 0;try{t(s)}finally{p=l}},{name:"sub"})};x.prototype.valueOf=function(){return this.value};x.prototype.toString=function(){return this.value+""};x.prototype.toJSON=function(){return this.value};x.prototype.peek=function(){var t=this;return Y(function(){return t.value})};Object.defineProperty(x.prototype,"value",{get:function(){var t=re(this);return t!==void 0&&(t.i=this.i),this.v},set:function(t){if(t!==this.v){if(B>100)throw new Error("Cycle detected");(function(s){H!==0&&B===0&&s.l!==X&&(s.l=X,K={S:s,v:s.v,i:s.i,o:K})})(this),this.v=t,this.i++,F++,H++;try{for(var e=this.t;e!==void 0;e=e.x)e.t.N()}finally{G()}}}});function Q(t,e){return new x(t,e)}function ae(t){for(var e=t.s;e!==void 0;e=e.n)if(e.S.i!==e.i||!e.S.h()||e.S.i!==e.i)return!0;return!1}function ce(t){for(var e=t.s;e!==void 0;e=e.n){var s=e.S.n;if(s!==void 0&&(e.r=s),e.S.n=e,e.i=-1,e.n===void 0){t.s=e;break}}}function ue(t){for(var e=t.s,s=void 0;e!==void 0;){var l=e.p;e.i===-1?(e.S.U(e),l!==void 0&&(l.n=e.n),e.n!==void 0&&(e.n.p=l)):s=e,e.S.n=e.r,e.r!==void 0&&(e.r=void 0),e=l}t.s=s}function A(t,e){x.call(this,void 0),this.x=t,this.s=void 0,this.g=F-1,this.f=4,this.W=e==null?void 0:e.watched,this.Z=e==null?void 0:e.unwatched,this.name=e==null?void 0:e.name}A.prototype=new x;A.prototype.h=function(){if(this.f&=-3,1&this.f)return!1;if((36&this.f)==32||(this.f&=-5,this.g===F))return!0;if(this.g=F,this.f|=1,this.i>0&&!ae(this))return this.f&=-2,!0;var t=p;try{ce(this),p=this;var e=this.x();(16&this.f||this.v!==e||this.i===0)&&(this.v=e,this.f&=-17,this.i++)}catch(s){this.v=s,this.f|=16,this.i++}return p=t,ue(this),this.f&=-2,!0};A.prototype.S=function(t){if(this.t===void 0){this.f|=36;for(var e=this.s;e!==void 0;e=e.n)e.S.S(e)}x.prototype.S.call(this,t)};A.prototype.U=function(t){if(this.t!==void 0&&(x.prototype.U.call(this,t),this.t===void 0)){this.f&=-33;for(var e=this.s;e!==void 0;e=e.n)e.S.U(e)}};A.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var t=this.t;t!==void 0;t=t.x)t.t.N()}};Object.defineProperty(A.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var t=re(this);if(this.h(),t!==void 0&&(t.i=this.i),16&this.f)throw this.v;return this.v}});function se(t,e){return new A(t,e)}function le(t){var e=t.m;if(t.m=void 0,typeof e=="function"){H++;var s=p;p=void 0;try{e()}catch(l){throw t.f&=-2,t.f|=8,ee(t),l}finally{p=s,G()}}}function ee(t){for(var e=t.s;e!==void 0;e=e.n)e.S.U(e);t.x=void 0,t.s=void 0,le(t)}function de(t){if(p!==this)throw new Error("Out-of-order effect");ue(this),p=t,this.f&=-2,8&this.f&&ee(this),G()}function _(t,e){this.x=t,this.m=void 0,this.s=void 0,this.u=void 0,this.f=32,this.name=e==null?void 0:e.name}_.prototype.c=function(){var t=this.S();try{if(8&this.f||this.x===void 0)return;var e=this.x();typeof e=="function"&&(this.m=e)}finally{t()}};_.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,le(this),ce(this),H++;var t=p;return p=this,de.bind(this,t)};_.prototype.N=function(){2&this.f||(this.f|=2,this.u=Z,Z=this)};_.prototype.d=function(){this.f|=8,1&this.f||ee(this)};_.prototype.dispose=function(){this.d()};function he(t,e){var s=new _(t,e);try{s.c()}catch(w){throw s.d(),w}var l=s.d.bind(s);return l[Symbol.dispose]=l,l}function pe(t={}){const e=(t.namespace??"default").trim()||"default",s=t.plugins??[],l=new Map;function w(S,I){return function(){var E;const M=`${e}:${S}`,P=l.get(S);if(P)return P;const R=I.state(),g=I.modules??{},b={...R};for(const[r,o]of Object.entries(g))b[r]=o.state();const h=Q(b),j=new Map,T=new Map,U=new Map,N=new Map,m=new Map,i=new Set,n=new Set(Object.keys(R)),c=new Set(Object.keys(I.getters??{})),d=new Set(Object.keys(I.actions??{})),$=r=>h.value[r]??{},W=(r,o)=>{h.value={...h.value,[r]:o}},q=new Proxy({},{get(r,o){var u,f;if(j.has(o))return(u=j.get(o))==null?void 0:u.value;if(m.has(o))return(f=m.get(o))==null?void 0:f.value;if(n.has(o))return h.value[o];const y=N.get(o);return y?$(y.moduleName)[y.key]:h.value[o]}});for(const[r,o]of Object.entries(I.getters??{}))j.set(r,se(()=>o(h.value,q)));const te=(r,o)=>{var u;for(const f of i)f(r);for(const f of s)(u=f.onMutation)==null||u.call(f,o,r)},ne=(r,o,u,f)=>{const y=h.value[r];Object.is(y,o)||(h.value={...h.value,[r]:o},te({namespace:e,scopeId:M,storeId:S,key:r,oldValue:y,newValue:o,origin:u},f))},C=(r,o,u,f,y)=>{const O=$(r),a=O[o];Object.is(a,u)||(W(r,{...O,[o]:u}),te({namespace:e,scopeId:M,storeId:S,key:`${r}.${o}`,oldValue:a,newValue:u,origin:f},y))},D={$id:S,$namespace:e,$scopeId:M,$subscribe(r){return i.add(r),()=>i.delete(r)},$dispose(){var r;i.clear();for(const o of s)(r=o.onDispose)==null||r.call(o,k);l.delete(S)},$patch(r,o="local"){L(()=>{for(const[u,f]of Object.entries(r)){const y=N.get(u);if(y){C(y.moduleName,y.key,f,o,k);continue}if(u.includes(".")){const[O,a]=u.split(".",2);if(g[O]){C(O,a,f,o,k);continue}}if(g[u]&&f&&typeof f=="object"&&!Array.isArray(f)){for(const[O,a]of Object.entries(f))C(u,O,a,o,k);continue}ne(u,f,o,k)}})},$snapshot(){return{...h.value}}};for(const[r,o]of Object.entries(g)){const u=new Map,f=new Map,y=new Proxy({},{get(a,v){var V,z,ie;if(u.has(v))return(V=u.get(v))==null?void 0:V.value;if(j.has(v))return(z=j.get(v))==null?void 0:z.value;if(m.has(v))return(ie=m.get(v))==null?void 0:ie.value;const oe=$(r);if(v in oe)return oe[v];if(n.has(v))return h.value[v]}});for(const[a,v]of Object.entries(o.getters??{}))u.set(a,se(()=>v($(r),h.value,y)));let O;for(const[a,v]of Object.entries(o.actions??{}))f.set(a,(...V)=>L(()=>v.apply(O,V)));O=new Proxy({},{get(a,v){var V;if(f.has(v))return f.get(v);if(u.has(v))return(V=u.get(v))==null?void 0:V.value;const z=$(r);return v in z?z[v]:k[v]},set(a,v,V){if(f.has(v)||u.has(v))return!1;const z=$(r);return v in z?C(r,v,V,"local",k):k[v]=V,!0}}),U.set(r,O),T.set(r,u);for(const a of Object.keys($(r)))!(n.has(a)||c.has(a)||d.has(a)||Reflect.has(D,a)||g[a]!==void 0)&&!N.has(a)&&N.set(a,{moduleName:r,key:a});for(const[a,v]of u.entries())!(n.has(a)||c.has(a)||d.has(a)||Reflect.has(D,a)||g[a]!==void 0)&&!m.has(a)&&m.set(a,v);for(const[a,v]of f.entries())!(n.has(a)||c.has(a)||d.has(a)||Reflect.has(D,a)||g[a]!==void 0)&&!Reflect.has(D,a)&&(D[a]=v)}const k=new Proxy(D,{get(r,o,u){var f,y;if(Reflect.has(r,o))return Reflect.get(r,o,u);if(U.has(o))return U.get(o);if(j.has(o))return(f=j.get(o))==null?void 0:f.value;if(m.has(o))return(y=m.get(o))==null?void 0:y.value;if(n.has(o))return h.value[o];const O=N.get(o);return O?$(O.moduleName)[O.key]:h.value[o]},set(r,o,u,f){if(Reflect.has(r,o))return Reflect.set(r,o,u,f);const y=N.get(o);return y?(C(y.moduleName,y.key,u,"local",f),!0):(ne(o,u,"local",f),!0)}});for(const[r,o]of Object.entries(I.actions??{}))D[r]=(...u)=>L(()=>o.apply(k,u));for(const r of s)(E=r.onInit)==null||E.call(r,k);return l.set(S,k),k}}function J(S,I){return function(){var E;const M=`\0machine:${S}`,P=l.get(M);if(P)return P;const R=`${e}:${S}`,g=Q(I.initial),b=Q((I.context??(()=>({})))()),h=new Set,j=(i,n)=>{var c;for(const d of h)d(i);for(const d of s)(c=d.onMutation)==null||c.call(d,n,i)},T=(i,n,c,d)=>{const $=b.value[i];Object.is($,n)||(b.value={...b.value,[i]:n},j({namespace:e,scopeId:R,storeId:S,key:i,oldValue:$,newValue:n,origin:c},d))},U=(i,n,c)=>{const d=g.value;Object.is(d,i)||(g.value=i,j({namespace:e,scopeId:R,storeId:S,key:"$state",oldValue:d,newValue:i,origin:n},c))},N={$id:S,$namespace:e,$scopeId:R,$subscribe(i){return h.add(i),()=>h.delete(i)},$dispose(){var i;h.clear();for(const n of s)(i=n.onDispose)==null||i.call(n,m);l.delete(M)},$patch(i,n="local"){L(()=>{for(const[c,d]of Object.entries(i))c==="$state"?U(d,n,m):T(c,d,n,m)})},$snapshot(){return{$state:g.value,...b.value}},send(i,n){L(()=>{var c,d,$;const W=(c=I.actions)==null?void 0:c[i];W&&W.call(m,n);const q=($=(d=I.states[g.value])==null?void 0:d.on)==null?void 0:$[i];q!==void 0&&U(q,"local",m)})},matches(i){return g.value===i},can(i){const n=I.states[g.value];return!!(n!=null&&n.on&&i in n.on)}},m=new Proxy(N,{get(i,n,c){return n==="state"?g.value:Reflect.has(i,n)?Reflect.get(i,n,c):b.value[n]},set(i,n,c,d){return n==="state"?!1:Reflect.has(i,n)?Reflect.set(i,n,c,d):(T(n,c,"local",d),!0)}});for(const i of s)(E=i.onInit)==null||E.call(i,m);return l.set(M,m),m}}return{defineStore:w,defineMachine:J,use(S){s.push(S)}}}pe();function ye(t={}){const e=t.channelName??"storix-sync",s=new Set(t.include??[]),l=new Set(t.exclude??[]),w=t.namespaces??[],J=w==="*"?null:new Set(w),S=t.debounce??16,I=crypto.randomUUID(),E=`__storix_sync__:${e}`,M=new Map,P=new Set,R=new Map,g=new Map;let b=null;const h=typeof window<"u"&&"BroadcastChannel"in window?new BroadcastChannel(e):null,j=i=>{const n=[i.$id,i.$scopeId];return!(n.some(c=>l.has(c))||s.size>0&&!n.some(c=>s.has(c)))},T=(i,n)=>n===i||J===null?!0:J.has(n),U=(i,n)=>{!n||n.originTabId===I||T(i.$namespace,n.namespace)&&n.storeId===i.$id&&j(i)&&i.$patch({[n.key]:n.newValue},"remote")},N=()=>{if(b=null,M.size===0)return;const i=Array.from(M.values());M.clear();for(const n of i){const c={...n,originTabId:I};if(h&&h.postMessage(c),typeof window<"u"&&"localStorage"in window)try{window.localStorage.setItem(E,JSON.stringify(c)),window.localStorage.removeItem(E)}catch{}}},m=i=>{if(M.set(`${i.scopeId}:${i.key}`,i),S<=0){N();return}b||(b=setTimeout(N,S))};return{name:"sync",onInit(i){const n=i;if(j(n)){if(P.add(n.$scopeId),h){const c=d=>{U(n,d.data)};R.set(n.$scopeId,c),h.addEventListener("message",c)}if(typeof window<"u"){const c=d=>{if(!(d.key!==E||!d.newValue))try{const $=JSON.parse(d.newValue);U(n,$)}catch{}};g.set(n.$scopeId,c),window.addEventListener("storage",c)}}},onMutation(i,n){n.origin!=="remote"&&j(i)&&m(n)},onDispose(i){const n=i;P.delete(n.$scopeId);const c=R.get(n.$scopeId);h&&c&&(h.removeEventListener("message",c),R.delete(n.$scopeId));const d=g.get(n.$scopeId);typeof window<"u"&&d&&(window.removeEventListener("storage",d),g.delete(n.$scopeId)),b&&(clearTimeout(b),b=null),N(),P.size===0&&(h==null||h.close())}}}exports.syncPlugin=ye;
package/dist/index.d.ts CHANGED
@@ -5,5 +5,5 @@ export interface SyncPluginOptions {
5
5
  namespaces?: string[] | '*';
6
6
  debounce?: number;
7
7
  }
8
- export declare function syncPlugin(options?: SyncPluginOptions): any;
8
+ export declare function syncPlugin(options?: SyncPluginOptions): import("@storix-js/core").StorePlugin;
9
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,iBAAiB;IAChC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AASD,wBAAgB,UAAU,CAAC,OAAO,GAAE,iBAAsB,OA8IzD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,iBAAiB;IAChC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AASD,wBAAgB,UAAU,CAAC,OAAO,GAAE,iBAAsB,yCA8IzD"}
package/dist/index.js CHANGED
@@ -1,358 +1,362 @@
1
- var at = Symbol.for("preact-signals");
2
- function W() {
3
- if (M > 1)
4
- M--;
1
+ var fe = Symbol.for("preact-signals");
2
+ function G() {
3
+ if (H > 1)
4
+ H--;
5
5
  else {
6
- for (var e, t = !1; D !== void 0; ) {
7
- var o = D;
8
- for (D = void 0, B++; o !== void 0; ) {
9
- var l = o.o;
10
- if (o.o = void 0, o.f &= -3, !(8 & o.f) && nt(o)) try {
11
- o.c();
12
- } catch (x) {
13
- t || (e = x, t = !0);
6
+ var t, e = !1;
7
+ for ((function() {
8
+ var w = K;
9
+ for (K = void 0; w !== void 0; )
10
+ w.S.v === w.v && (w.S.i = w.i), w = w.o;
11
+ })(); Z !== void 0; ) {
12
+ var s = Z;
13
+ for (Z = void 0, B++; s !== void 0; ) {
14
+ var l = s.u;
15
+ if (s.u = void 0, s.f &= -3, !(8 & s.f) && ae(s)) try {
16
+ s.c();
17
+ } catch (w) {
18
+ e || (t = w, e = !0);
14
19
  }
15
- o = l;
20
+ s = l;
16
21
  }
17
22
  }
18
- if (B = 0, M--, t) throw e;
23
+ if (B = 0, H--, e) throw t;
19
24
  }
20
25
  }
21
- function Z(e) {
22
- if (M > 0) return e();
23
- M++;
26
+ function T(t) {
27
+ if (H > 0) return t();
28
+ X = ++ve, H++;
24
29
  try {
25
- return e();
30
+ return t();
26
31
  } finally {
27
- W();
32
+ G();
28
33
  }
29
34
  }
30
- var u = void 0;
31
- function tt(e) {
32
- var t = u;
33
- u = void 0;
35
+ var p = void 0;
36
+ function Y(t) {
37
+ var e = p;
38
+ p = void 0;
34
39
  try {
35
- return e();
40
+ return t();
36
41
  } finally {
37
- u = t;
42
+ p = e;
38
43
  }
39
44
  }
40
- var D = void 0, M = 0, B = 0, J = 0;
41
- function et(e) {
42
- if (u !== void 0) {
43
- var t = e.n;
44
- if (t === void 0 || t.t !== u)
45
- return t = { i: 0, S: e, p: u.s, n: void 0, t: u, e: void 0, x: void 0, r: t }, u.s !== void 0 && (u.s.n = t), u.s = t, e.n = t, 32 & u.f && e.S(t), t;
46
- if (t.i === -1)
47
- return t.i = 0, t.n !== void 0 && (t.n.p = t.p, t.p !== void 0 && (t.p.n = t.n), t.p = u.s, t.n = void 0, u.s.n = t, u.s = t), t;
45
+ var Z = void 0, H = 0, B = 0, ve = 0, X = 0, K = void 0, F = 0;
46
+ function re(t) {
47
+ if (p !== void 0) {
48
+ var e = t.n;
49
+ if (e === void 0 || e.t !== p)
50
+ return e = { i: 0, S: t, p: p.s, n: void 0, t: p, e: void 0, x: void 0, r: e }, p.s !== void 0 && (p.s.n = e), p.s = e, t.n = e, 32 & p.f && t.S(e), e;
51
+ if (e.i === -1)
52
+ return e.i = 0, e.n !== void 0 && (e.n.p = e.p, e.p !== void 0 && (e.p.n = e.n), e.p = p.s, e.n = void 0, p.s.n = e, p.s = e), e;
48
53
  }
49
54
  }
50
- function m(e, t) {
51
- this.v = e, this.i = 0, this.n = void 0, this.t = void 0, this.W = t == null ? void 0 : t.watched, this.Z = t == null ? void 0 : t.unwatched, this.name = t == null ? void 0 : t.name;
55
+ function O(t, e) {
56
+ this.v = t, this.i = 0, this.n = void 0, this.t = void 0, this.l = 0, this.W = e == null ? void 0 : e.watched, this.Z = e == null ? void 0 : e.unwatched, this.name = e == null ? void 0 : e.name;
52
57
  }
53
- m.prototype.brand = at;
54
- m.prototype.h = function() {
58
+ O.prototype.brand = fe;
59
+ O.prototype.h = function() {
55
60
  return !0;
56
61
  };
57
- m.prototype.S = function(e) {
58
- var t = this, o = this.t;
59
- o !== e && e.e === void 0 && (e.x = o, this.t = e, o !== void 0 ? o.e = e : tt(function() {
62
+ O.prototype.S = function(t) {
63
+ var e = this, s = this.t;
64
+ s !== t && t.e === void 0 && (t.x = s, this.t = t, s !== void 0 ? s.e = t : Y(function() {
60
65
  var l;
61
- (l = t.W) == null || l.call(t);
66
+ (l = e.W) == null || l.call(e);
62
67
  }));
63
68
  };
64
- m.prototype.U = function(e) {
65
- var t = this;
69
+ O.prototype.U = function(t) {
70
+ var e = this;
66
71
  if (this.t !== void 0) {
67
- var o = e.e, l = e.x;
68
- o !== void 0 && (o.x = l, e.e = void 0), l !== void 0 && (l.e = o, e.x = void 0), e === this.t && (this.t = l, l === void 0 && tt(function() {
69
- var x;
70
- (x = t.Z) == null || x.call(t);
72
+ var s = t.e, l = t.x;
73
+ s !== void 0 && (s.x = l, t.e = void 0), l !== void 0 && (l.e = s, t.x = void 0), t === this.t && (this.t = l, l === void 0 && Y(function() {
74
+ var w;
75
+ (w = e.Z) == null || w.call(e);
71
76
  }));
72
77
  }
73
78
  };
74
- m.prototype.subscribe = function(e) {
75
- var t = this;
76
- return ut(function() {
77
- var o = t.value, l = u;
78
- u = void 0;
79
+ O.prototype.subscribe = function(t) {
80
+ var e = this;
81
+ return he(function() {
82
+ var s = e.value, l = p;
83
+ p = void 0;
79
84
  try {
80
- e(o);
85
+ t(s);
81
86
  } finally {
82
- u = l;
87
+ p = l;
83
88
  }
84
89
  }, { name: "sub" });
85
90
  };
86
- m.prototype.valueOf = function() {
91
+ O.prototype.valueOf = function() {
87
92
  return this.value;
88
93
  };
89
- m.prototype.toString = function() {
94
+ O.prototype.toString = function() {
90
95
  return this.value + "";
91
96
  };
92
- m.prototype.toJSON = function() {
97
+ O.prototype.toJSON = function() {
93
98
  return this.value;
94
99
  };
95
- m.prototype.peek = function() {
96
- var e = u;
97
- u = void 0;
98
- try {
99
- return this.value;
100
- } finally {
101
- u = e;
102
- }
100
+ O.prototype.peek = function() {
101
+ var t = this;
102
+ return Y(function() {
103
+ return t.value;
104
+ });
103
105
  };
104
- Object.defineProperty(m.prototype, "value", { get: function() {
105
- var e = et(this);
106
- return e !== void 0 && (e.i = this.i), this.v;
107
- }, set: function(e) {
108
- if (e !== this.v) {
106
+ Object.defineProperty(O.prototype, "value", { get: function() {
107
+ var t = re(this);
108
+ return t !== void 0 && (t.i = this.i), this.v;
109
+ }, set: function(t) {
110
+ if (t !== this.v) {
109
111
  if (B > 100) throw new Error("Cycle detected");
110
- this.v = e, this.i++, J++, M++;
112
+ (function(s) {
113
+ H !== 0 && B === 0 && s.l !== X && (s.l = X, K = { S: s, v: s.v, i: s.i, o: K });
114
+ })(this), this.v = t, this.i++, F++, H++;
111
115
  try {
112
- for (var t = this.t; t !== void 0; t = t.x) t.t.N();
116
+ for (var e = this.t; e !== void 0; e = e.x) e.t.N();
113
117
  } finally {
114
- W();
118
+ G();
115
119
  }
116
120
  }
117
121
  } });
118
- function ft(e, t) {
119
- return new m(e, t);
122
+ function Q(t, e) {
123
+ return new O(t, e);
120
124
  }
121
- function nt(e) {
122
- for (var t = e.s; t !== void 0; t = t.n) if (t.S.i !== t.i || !t.S.h() || t.S.i !== t.i) return !0;
125
+ function ae(t) {
126
+ for (var e = t.s; e !== void 0; e = e.n) if (e.S.i !== e.i || !e.S.h() || e.S.i !== e.i) return !0;
123
127
  return !1;
124
128
  }
125
- function it(e) {
126
- for (var t = e.s; t !== void 0; t = t.n) {
127
- var o = t.S.n;
128
- if (o !== void 0 && (t.r = o), t.S.n = t, t.i = -1, t.n === void 0) {
129
- e.s = t;
129
+ function ce(t) {
130
+ for (var e = t.s; e !== void 0; e = e.n) {
131
+ var s = e.S.n;
132
+ if (s !== void 0 && (e.r = s), e.S.n = e, e.i = -1, e.n === void 0) {
133
+ t.s = e;
130
134
  break;
131
135
  }
132
136
  }
133
137
  }
134
- function ot(e) {
135
- for (var t = e.s, o = void 0; t !== void 0; ) {
136
- var l = t.p;
137
- t.i === -1 ? (t.S.U(t), l !== void 0 && (l.n = t.n), t.n !== void 0 && (t.n.p = l)) : o = t, t.S.n = t.r, t.r !== void 0 && (t.r = void 0), t = l;
138
+ function ue(t) {
139
+ for (var e = t.s, s = void 0; e !== void 0; ) {
140
+ var l = e.p;
141
+ e.i === -1 ? (e.S.U(e), l !== void 0 && (l.n = e.n), e.n !== void 0 && (e.n.p = l)) : s = e, e.S.n = e.r, e.r !== void 0 && (e.r = void 0), e = l;
138
142
  }
139
- e.s = o;
143
+ t.s = s;
140
144
  }
141
- function U(e, t) {
142
- m.call(this, void 0), this.x = e, this.s = void 0, this.g = J - 1, this.f = 4, this.W = t == null ? void 0 : t.watched, this.Z = t == null ? void 0 : t.unwatched, this.name = t == null ? void 0 : t.name;
145
+ function A(t, e) {
146
+ O.call(this, void 0), this.x = t, this.s = void 0, this.g = F - 1, this.f = 4, this.W = e == null ? void 0 : e.watched, this.Z = e == null ? void 0 : e.unwatched, this.name = e == null ? void 0 : e.name;
143
147
  }
144
- U.prototype = new m();
145
- U.prototype.h = function() {
148
+ A.prototype = new O();
149
+ A.prototype.h = function() {
146
150
  if (this.f &= -3, 1 & this.f) return !1;
147
- if ((36 & this.f) == 32 || (this.f &= -5, this.g === J)) return !0;
148
- if (this.g = J, this.f |= 1, this.i > 0 && !nt(this))
151
+ if ((36 & this.f) == 32 || (this.f &= -5, this.g === F)) return !0;
152
+ if (this.g = F, this.f |= 1, this.i > 0 && !ae(this))
149
153
  return this.f &= -2, !0;
150
- var e = u;
154
+ var t = p;
151
155
  try {
152
- it(this), u = this;
153
- var t = this.x();
154
- (16 & this.f || this.v !== t || this.i === 0) && (this.v = t, this.f &= -17, this.i++);
155
- } catch (o) {
156
- this.v = o, this.f |= 16, this.i++;
156
+ ce(this), p = this;
157
+ var e = this.x();
158
+ (16 & this.f || this.v !== e || this.i === 0) && (this.v = e, this.f &= -17, this.i++);
159
+ } catch (s) {
160
+ this.v = s, this.f |= 16, this.i++;
157
161
  }
158
- return u = e, ot(this), this.f &= -2, !0;
162
+ return p = t, ue(this), this.f &= -2, !0;
159
163
  };
160
- U.prototype.S = function(e) {
164
+ A.prototype.S = function(t) {
161
165
  if (this.t === void 0) {
162
166
  this.f |= 36;
163
- for (var t = this.s; t !== void 0; t = t.n) t.S.S(t);
167
+ for (var e = this.s; e !== void 0; e = e.n) e.S.S(e);
164
168
  }
165
- m.prototype.S.call(this, e);
169
+ O.prototype.S.call(this, t);
166
170
  };
167
- U.prototype.U = function(e) {
168
- if (this.t !== void 0 && (m.prototype.U.call(this, e), this.t === void 0)) {
171
+ A.prototype.U = function(t) {
172
+ if (this.t !== void 0 && (O.prototype.U.call(this, t), this.t === void 0)) {
169
173
  this.f &= -33;
170
- for (var t = this.s; t !== void 0; t = t.n) t.S.U(t);
174
+ for (var e = this.s; e !== void 0; e = e.n) e.S.U(e);
171
175
  }
172
176
  };
173
- U.prototype.N = function() {
177
+ A.prototype.N = function() {
174
178
  if (!(2 & this.f)) {
175
179
  this.f |= 6;
176
- for (var e = this.t; e !== void 0; e = e.x) e.t.N();
180
+ for (var t = this.t; t !== void 0; t = t.x) t.t.N();
177
181
  }
178
182
  };
179
- Object.defineProperty(U.prototype, "value", { get: function() {
183
+ Object.defineProperty(A.prototype, "value", { get: function() {
180
184
  if (1 & this.f) throw new Error("Cycle detected");
181
- var e = et(this);
182
- if (this.h(), e !== void 0 && (e.i = this.i), 16 & this.f) throw this.v;
185
+ var t = re(this);
186
+ if (this.h(), t !== void 0 && (t.i = this.i), 16 & this.f) throw this.v;
183
187
  return this.v;
184
188
  } });
185
- function Y(e, t) {
186
- return new U(e, t);
189
+ function se(t, e) {
190
+ return new A(t, e);
187
191
  }
188
- function st(e) {
189
- var t = e.u;
190
- if (e.u = void 0, typeof t == "function") {
191
- M++;
192
- var o = u;
193
- u = void 0;
192
+ function le(t) {
193
+ var e = t.m;
194
+ if (t.m = void 0, typeof e == "function") {
195
+ H++;
196
+ var s = p;
197
+ p = void 0;
194
198
  try {
195
- t();
199
+ e();
196
200
  } catch (l) {
197
- throw e.f &= -2, e.f |= 8, K(e), l;
201
+ throw t.f &= -2, t.f |= 8, ee(t), l;
198
202
  } finally {
199
- u = o, W();
203
+ p = s, G();
200
204
  }
201
205
  }
202
206
  }
203
- function K(e) {
204
- for (var t = e.s; t !== void 0; t = t.n) t.S.U(t);
205
- e.x = void 0, e.s = void 0, st(e);
207
+ function ee(t) {
208
+ for (var e = t.s; e !== void 0; e = e.n) e.S.U(e);
209
+ t.x = void 0, t.s = void 0, le(t);
206
210
  }
207
- function ct(e) {
208
- if (u !== this) throw new Error("Out-of-order effect");
209
- ot(this), u = e, this.f &= -2, 8 & this.f && K(this), W();
211
+ function de(t) {
212
+ if (p !== this) throw new Error("Out-of-order effect");
213
+ ue(this), p = t, this.f &= -2, 8 & this.f && ee(this), G();
210
214
  }
211
- function T(e, t) {
212
- this.x = e, this.u = void 0, this.s = void 0, this.o = void 0, this.f = 32, this.name = t == null ? void 0 : t.name;
215
+ function C(t, e) {
216
+ this.x = t, this.m = void 0, this.s = void 0, this.u = void 0, this.f = 32, this.name = e == null ? void 0 : e.name;
213
217
  }
214
- T.prototype.c = function() {
215
- var e = this.S();
218
+ C.prototype.c = function() {
219
+ var t = this.S();
216
220
  try {
217
221
  if (8 & this.f || this.x === void 0) return;
218
- var t = this.x();
219
- typeof t == "function" && (this.u = t);
222
+ var e = this.x();
223
+ typeof e == "function" && (this.m = e);
220
224
  } finally {
221
- e();
225
+ t();
222
226
  }
223
227
  };
224
- T.prototype.S = function() {
228
+ C.prototype.S = function() {
225
229
  if (1 & this.f) throw new Error("Cycle detected");
226
- this.f |= 1, this.f &= -9, st(this), it(this), M++;
227
- var e = u;
228
- return u = this, ct.bind(this, e);
230
+ this.f |= 1, this.f &= -9, le(this), ce(this), H++;
231
+ var t = p;
232
+ return p = this, de.bind(this, t);
229
233
  };
230
- T.prototype.N = function() {
231
- 2 & this.f || (this.f |= 2, this.o = D, D = this);
234
+ C.prototype.N = function() {
235
+ 2 & this.f || (this.f |= 2, this.u = Z, Z = this);
232
236
  };
233
- T.prototype.d = function() {
234
- this.f |= 8, 1 & this.f || K(this);
237
+ C.prototype.d = function() {
238
+ this.f |= 8, 1 & this.f || ee(this);
235
239
  };
236
- T.prototype.dispose = function() {
240
+ C.prototype.dispose = function() {
237
241
  this.d();
238
242
  };
239
- function ut(e, t) {
240
- var o = new T(e, t);
243
+ function he(t, e) {
244
+ var s = new C(t, e);
241
245
  try {
242
- o.c();
243
- } catch (x) {
244
- throw o.d(), x;
246
+ s.c();
247
+ } catch (w) {
248
+ throw s.d(), w;
245
249
  }
246
- var l = o.d.bind(o);
250
+ var l = s.d.bind(s);
247
251
  return l[Symbol.dispose] = l, l;
248
252
  }
249
- function lt(e = {}) {
250
- const t = (e.namespace ?? "default").trim() || "default", o = e.plugins ?? [], l = /* @__PURE__ */ new Map();
251
- function x(S, j) {
253
+ function pe(t = {}) {
254
+ const e = (t.namespace ?? "default").trim() || "default", s = t.plugins ?? [], l = /* @__PURE__ */ new Map();
255
+ function w(S, I) {
252
256
  return function() {
253
- var _;
254
- const N = `${t}:${S}`, E = l.get(S);
255
- if (E) return E;
256
- const P = j.state(), I = j.modules ?? {}, H = { ...P };
257
- for (const [i, n] of Object.entries(I))
258
- H[i] = n.state();
259
- const h = ft(H), y = /* @__PURE__ */ new Map(), A = /* @__PURE__ */ new Map(), L = /* @__PURE__ */ new Map(), k = /* @__PURE__ */ new Map(), b = /* @__PURE__ */ new Map(), C = /* @__PURE__ */ new Set(), d = new Set(Object.keys(P)), a = new Set(Object.keys(j.getters ?? {})), p = new Set(Object.keys(j.actions ?? {})), w = (i) => h.value[i] ?? {}, q = (i, n) => {
257
+ var E;
258
+ const M = `${e}:${S}`, U = l.get(S);
259
+ if (U) return U;
260
+ const R = I.state(), m = I.modules ?? {}, b = { ...R };
261
+ for (const [r, i] of Object.entries(m))
262
+ b[r] = i.state();
263
+ const h = Q(b), j = /* @__PURE__ */ new Map(), _ = /* @__PURE__ */ new Map(), P = /* @__PURE__ */ new Map(), N = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Set(), n = new Set(Object.keys(R)), c = new Set(Object.keys(I.getters ?? {})), d = new Set(Object.keys(I.actions ?? {})), $ = (r) => h.value[r] ?? {}, W = (r, i) => {
260
264
  h.value = {
261
265
  ...h.value,
262
- [i]: n
266
+ [r]: i
263
267
  };
264
- }, rt = new Proxy(
268
+ }, q = new Proxy(
265
269
  {},
266
270
  {
267
- get(i, n) {
268
- var r, f;
269
- if (y.has(n)) return (r = y.get(n)) == null ? void 0 : r.value;
270
- if (b.has(n)) return (f = b.get(n)) == null ? void 0 : f.value;
271
- if (d.has(n)) return h.value[n];
272
- const v = k.get(n);
273
- return v ? w(v.moduleName)[v.key] : h.value[n];
271
+ get(r, i) {
272
+ var u, f;
273
+ if (j.has(i)) return (u = j.get(i)) == null ? void 0 : u.value;
274
+ if (g.has(i)) return (f = g.get(i)) == null ? void 0 : f.value;
275
+ if (n.has(i)) return h.value[i];
276
+ const y = N.get(i);
277
+ return y ? $(y.moduleName)[y.key] : h.value[i];
274
278
  }
275
279
  }
276
280
  );
277
- for (const [i, n] of Object.entries(j.getters ?? {}))
278
- y.set(
279
- i,
280
- Y(() => n(h.value, rt))
281
+ for (const [r, i] of Object.entries(I.getters ?? {}))
282
+ j.set(
283
+ r,
284
+ se(() => i(h.value, q))
281
285
  );
282
- const F = (i, n) => {
283
- var r;
284
- for (const f of C) f(i);
285
- for (const f of o)
286
- (r = f.onMutation) == null || r.call(f, n, i);
287
- }, G = (i, n, r, f) => {
288
- const v = h.value[i];
289
- Object.is(v, n) || (h.value = {
286
+ const te = (r, i) => {
287
+ var u;
288
+ for (const f of o) f(r);
289
+ for (const f of s)
290
+ (u = f.onMutation) == null || u.call(f, i, r);
291
+ }, ne = (r, i, u, f) => {
292
+ const y = h.value[r];
293
+ Object.is(y, i) || (h.value = {
290
294
  ...h.value,
291
- [i]: n
292
- }, F(
295
+ [r]: i
296
+ }, te(
293
297
  {
294
- namespace: t,
295
- scopeId: N,
298
+ namespace: e,
299
+ scopeId: M,
296
300
  storeId: S,
297
- key: i,
298
- oldValue: v,
299
- newValue: n,
300
- origin: r
301
+ key: r,
302
+ oldValue: y,
303
+ newValue: i,
304
+ origin: u
301
305
  },
302
306
  f
303
307
  ));
304
- }, z = (i, n, r, f, v) => {
305
- const g = w(i), s = g[n];
306
- Object.is(s, r) || (q(i, {
307
- ...g,
308
- [n]: r
309
- }), F(
308
+ }, L = (r, i, u, f, y) => {
309
+ const x = $(r), a = x[i];
310
+ Object.is(a, u) || (W(r, {
311
+ ...x,
312
+ [i]: u
313
+ }), te(
310
314
  {
311
- namespace: t,
312
- scopeId: N,
315
+ namespace: e,
316
+ scopeId: M,
313
317
  storeId: S,
314
- key: `${i}.${n}`,
315
- oldValue: s,
316
- newValue: r,
318
+ key: `${r}.${i}`,
319
+ oldValue: a,
320
+ newValue: u,
317
321
  origin: f
318
322
  },
319
- v
323
+ y
320
324
  ));
321
- }, V = {
325
+ }, D = {
322
326
  $id: S,
323
- $namespace: t,
324
- $scopeId: N,
325
- $subscribe(i) {
326
- return C.add(i), () => C.delete(i);
327
+ $namespace: e,
328
+ $scopeId: M,
329
+ $subscribe(r) {
330
+ return o.add(r), () => o.delete(r);
327
331
  },
328
332
  $dispose() {
329
- var i;
330
- C.clear();
331
- for (const n of o)
332
- (i = n.onDispose) == null || i.call(n, $);
333
+ var r;
334
+ o.clear();
335
+ for (const i of s)
336
+ (r = i.onDispose) == null || r.call(i, k);
333
337
  l.delete(S);
334
338
  },
335
- $patch(i, n = "local") {
336
- Z(() => {
337
- for (const [r, f] of Object.entries(i)) {
338
- const v = k.get(r);
339
- if (v) {
340
- z(v.moduleName, v.key, f, n, $);
339
+ $patch(r, i = "local") {
340
+ T(() => {
341
+ for (const [u, f] of Object.entries(r)) {
342
+ const y = N.get(u);
343
+ if (y) {
344
+ L(y.moduleName, y.key, f, i, k);
341
345
  continue;
342
346
  }
343
- if (r.includes(".")) {
344
- const [g, s] = r.split(".", 2);
345
- if (I[g]) {
346
- z(g, s, f, n, $);
347
+ if (u.includes(".")) {
348
+ const [x, a] = u.split(".", 2);
349
+ if (m[x]) {
350
+ L(x, a, f, i, k);
347
351
  continue;
348
352
  }
349
353
  }
350
- if (I[r] && f && typeof f == "object" && !Array.isArray(f)) {
351
- for (const [g, s] of Object.entries(f))
352
- z(r, g, s, n, $);
354
+ if (m[u] && f && typeof f == "object" && !Array.isArray(f)) {
355
+ for (const [x, a] of Object.entries(f))
356
+ L(u, x, a, i, k);
353
357
  continue;
354
358
  }
355
- G(r, f, n, $);
359
+ ne(u, f, i, k);
356
360
  }
357
361
  });
358
362
  },
@@ -360,153 +364,225 @@ function lt(e = {}) {
360
364
  return { ...h.value };
361
365
  }
362
366
  };
363
- for (const [i, n] of Object.entries(I)) {
364
- const r = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map(), v = new Proxy(
367
+ for (const [r, i] of Object.entries(m)) {
368
+ const u = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map(), y = new Proxy(
365
369
  {},
366
370
  {
367
- get(s, c) {
368
- var O, R, Q;
369
- if (r.has(c)) return (O = r.get(c)) == null ? void 0 : O.value;
370
- if (y.has(c)) return (R = y.get(c)) == null ? void 0 : R.value;
371
- if (b.has(c)) return (Q = b.get(c)) == null ? void 0 : Q.value;
372
- const X = w(i);
373
- if (c in X) return X[c];
374
- if (d.has(c)) return h.value[c];
371
+ get(a, v) {
372
+ var V, z, oe;
373
+ if (u.has(v)) return (V = u.get(v)) == null ? void 0 : V.value;
374
+ if (j.has(v)) return (z = j.get(v)) == null ? void 0 : z.value;
375
+ if (g.has(v)) return (oe = g.get(v)) == null ? void 0 : oe.value;
376
+ const ie = $(r);
377
+ if (v in ie) return ie[v];
378
+ if (n.has(v)) return h.value[v];
375
379
  }
376
380
  }
377
381
  );
378
- for (const [s, c] of Object.entries(n.getters ?? {}))
379
- r.set(
380
- s,
381
- Y(() => c(w(i), h.value, v))
382
+ for (const [a, v] of Object.entries(i.getters ?? {}))
383
+ u.set(
384
+ a,
385
+ se(() => v($(r), h.value, y))
382
386
  );
383
- let g;
384
- for (const [s, c] of Object.entries(n.actions ?? {}))
385
- f.set(s, (...O) => Z(() => c.apply(g, O)));
386
- g = new Proxy(
387
+ let x;
388
+ for (const [a, v] of Object.entries(i.actions ?? {}))
389
+ f.set(a, (...V) => T(() => v.apply(x, V)));
390
+ x = new Proxy(
387
391
  {},
388
392
  {
389
- get(s, c) {
390
- var O;
391
- if (f.has(c)) return f.get(c);
392
- if (r.has(c)) return (O = r.get(c)) == null ? void 0 : O.value;
393
- const R = w(i);
394
- return c in R ? R[c] : $[c];
393
+ get(a, v) {
394
+ var V;
395
+ if (f.has(v)) return f.get(v);
396
+ if (u.has(v)) return (V = u.get(v)) == null ? void 0 : V.value;
397
+ const z = $(r);
398
+ return v in z ? z[v] : k[v];
395
399
  },
396
- set(s, c, O) {
397
- if (f.has(c) || r.has(c)) return !1;
398
- const R = w(i);
399
- return c in R ? z(i, c, O, "local", $) : $[c] = O, !0;
400
+ set(a, v, V) {
401
+ if (f.has(v) || u.has(v)) return !1;
402
+ const z = $(r);
403
+ return v in z ? L(r, v, V, "local", k) : k[v] = V, !0;
400
404
  }
401
405
  }
402
- ), L.set(i, g), A.set(i, r);
403
- for (const s of Object.keys(w(i)))
404
- !(d.has(s) || a.has(s) || p.has(s) || Reflect.has(V, s) || I[s] !== void 0) && !k.has(s) && k.set(s, { moduleName: i, key: s });
405
- for (const [s, c] of r.entries())
406
- !(d.has(s) || a.has(s) || p.has(s) || Reflect.has(V, s) || I[s] !== void 0) && !b.has(s) && b.set(s, c);
407
- for (const [s, c] of f.entries())
408
- !(d.has(s) || a.has(s) || p.has(s) || Reflect.has(V, s) || I[s] !== void 0) && !Reflect.has(V, s) && (V[s] = c);
406
+ ), P.set(r, x), _.set(r, u);
407
+ for (const a of Object.keys($(r)))
408
+ !(n.has(a) || c.has(a) || d.has(a) || Reflect.has(D, a) || m[a] !== void 0) && !N.has(a) && N.set(a, { moduleName: r, key: a });
409
+ for (const [a, v] of u.entries())
410
+ !(n.has(a) || c.has(a) || d.has(a) || Reflect.has(D, a) || m[a] !== void 0) && !g.has(a) && g.set(a, v);
411
+ for (const [a, v] of f.entries())
412
+ !(n.has(a) || c.has(a) || d.has(a) || Reflect.has(D, a) || m[a] !== void 0) && !Reflect.has(D, a) && (D[a] = v);
409
413
  }
410
- const $ = new Proxy(V, {
411
- get(i, n, r) {
412
- var f, v;
413
- if (Reflect.has(i, n)) return Reflect.get(i, n, r);
414
- if (L.has(n)) return L.get(n);
415
- if (y.has(n)) return (f = y.get(n)) == null ? void 0 : f.value;
416
- if (b.has(n)) return (v = b.get(n)) == null ? void 0 : v.value;
417
- if (d.has(n)) return h.value[n];
418
- const g = k.get(n);
419
- return g ? w(g.moduleName)[g.key] : h.value[n];
414
+ const k = new Proxy(D, {
415
+ get(r, i, u) {
416
+ var f, y;
417
+ if (Reflect.has(r, i)) return Reflect.get(r, i, u);
418
+ if (P.has(i)) return P.get(i);
419
+ if (j.has(i)) return (f = j.get(i)) == null ? void 0 : f.value;
420
+ if (g.has(i)) return (y = g.get(i)) == null ? void 0 : y.value;
421
+ if (n.has(i)) return h.value[i];
422
+ const x = N.get(i);
423
+ return x ? $(x.moduleName)[x.key] : h.value[i];
424
+ },
425
+ set(r, i, u, f) {
426
+ if (Reflect.has(r, i))
427
+ return Reflect.set(r, i, u, f);
428
+ const y = N.get(i);
429
+ return y ? (L(y.moduleName, y.key, u, "local", f), !0) : (ne(i, u, "local", f), !0);
430
+ }
431
+ });
432
+ for (const [r, i] of Object.entries(I.actions ?? {}))
433
+ D[r] = (...u) => T(() => i.apply(k, u));
434
+ for (const r of s)
435
+ (E = r.onInit) == null || E.call(r, k);
436
+ return l.set(S, k), k;
437
+ };
438
+ }
439
+ function J(S, I) {
440
+ return function() {
441
+ var E;
442
+ const M = `\0machine:${S}`, U = l.get(M);
443
+ if (U) return U;
444
+ const R = `${e}:${S}`, m = Q(I.initial), b = Q((I.context ?? (() => ({})))()), h = /* @__PURE__ */ new Set(), j = (o, n) => {
445
+ var c;
446
+ for (const d of h) d(o);
447
+ for (const d of s)
448
+ (c = d.onMutation) == null || c.call(d, n, o);
449
+ }, _ = (o, n, c, d) => {
450
+ const $ = b.value[o];
451
+ Object.is($, n) || (b.value = { ...b.value, [o]: n }, j({ namespace: e, scopeId: R, storeId: S, key: o, oldValue: $, newValue: n, origin: c }, d));
452
+ }, P = (o, n, c) => {
453
+ const d = m.value;
454
+ Object.is(d, o) || (m.value = o, j(
455
+ { namespace: e, scopeId: R, storeId: S, key: "$state", oldValue: d, newValue: o, origin: n },
456
+ c
457
+ ));
458
+ }, N = {
459
+ $id: S,
460
+ $namespace: e,
461
+ $scopeId: R,
462
+ $subscribe(o) {
463
+ return h.add(o), () => h.delete(o);
464
+ },
465
+ $dispose() {
466
+ var o;
467
+ h.clear();
468
+ for (const n of s)
469
+ (o = n.onDispose) == null || o.call(n, g);
470
+ l.delete(M);
471
+ },
472
+ $patch(o, n = "local") {
473
+ T(() => {
474
+ for (const [c, d] of Object.entries(o))
475
+ c === "$state" ? P(d, n, g) : _(c, d, n, g);
476
+ });
477
+ },
478
+ $snapshot() {
479
+ return { $state: m.value, ...b.value };
480
+ },
481
+ send(o, n) {
482
+ T(() => {
483
+ var c, d, $;
484
+ const W = (c = I.actions) == null ? void 0 : c[o];
485
+ W && W.call(g, n);
486
+ const q = ($ = (d = I.states[m.value]) == null ? void 0 : d.on) == null ? void 0 : $[o];
487
+ q !== void 0 && P(q, "local", g);
488
+ });
489
+ },
490
+ matches(o) {
491
+ return m.value === o;
492
+ },
493
+ can(o) {
494
+ const n = I.states[m.value];
495
+ return !!(n != null && n.on && o in n.on);
496
+ }
497
+ }, g = new Proxy(N, {
498
+ get(o, n, c) {
499
+ return n === "state" ? m.value : Reflect.has(o, n) ? Reflect.get(o, n, c) : b.value[n];
420
500
  },
421
- set(i, n, r, f) {
422
- if (Reflect.has(i, n))
423
- return Reflect.set(i, n, r, f);
424
- const v = k.get(n);
425
- return v ? (z(v.moduleName, v.key, r, "local", f), !0) : (G(n, r, "local", f), !0);
501
+ set(o, n, c, d) {
502
+ return n === "state" ? !1 : Reflect.has(o, n) ? Reflect.set(o, n, c, d) : (_(n, c, "local", d), !0);
426
503
  }
427
504
  });
428
- for (const [i, n] of Object.entries(j.actions ?? {}))
429
- V[i] = (...r) => Z(() => n.apply($, r));
430
- for (const i of o)
431
- (_ = i.onInit) == null || _.call(i, $);
432
- return l.set(S, $), $;
505
+ for (const o of s)
506
+ (E = o.onInit) == null || E.call(o, g);
507
+ return l.set(M, g), g;
433
508
  };
434
509
  }
435
510
  return {
436
- defineStore: x,
511
+ defineStore: w,
512
+ defineMachine: J,
437
513
  use(S) {
438
- o.push(S);
514
+ s.push(S);
439
515
  }
440
516
  };
441
517
  }
442
- lt();
443
- function dt(e = {}) {
444
- const t = e.channelName ?? "storix-sync", o = new Set(e.include ?? []), l = new Set(e.exclude ?? []), x = e.namespaces ?? [], S = x === "*" ? null : new Set(x), j = e.debounce ?? 16, _ = crypto.randomUUID(), N = `__storix_sync__:${t}`, E = /* @__PURE__ */ new Map(), P = /* @__PURE__ */ new Set(), I = /* @__PURE__ */ new Map(), H = /* @__PURE__ */ new Map();
445
- let h = null;
446
- const y = typeof window < "u" && "BroadcastChannel" in window ? new BroadcastChannel(t) : null, A = (d) => {
447
- const a = [d.$id, d.$scopeId];
448
- return !(a.some((p) => l.has(p)) || o.size > 0 && !a.some((p) => o.has(p)));
449
- }, L = (d, a) => a === d || S === null ? !0 : S.has(a), k = (d, a) => {
450
- !a || a.originTabId === _ || L(d.$namespace, a.namespace) && a.storeId === d.$id && A(d) && d.$patch({ [a.key]: a.newValue }, "remote");
451
- }, b = () => {
452
- if (h = null, E.size === 0) return;
453
- const d = Array.from(E.values());
454
- E.clear();
455
- for (const a of d) {
456
- const p = {
457
- ...a,
458
- originTabId: _
518
+ pe();
519
+ function ye(t = {}) {
520
+ const e = t.channelName ?? "storix-sync", s = new Set(t.include ?? []), l = new Set(t.exclude ?? []), w = t.namespaces ?? [], J = w === "*" ? null : new Set(w), S = t.debounce ?? 16, I = crypto.randomUUID(), E = `__storix_sync__:${e}`, M = /* @__PURE__ */ new Map(), U = /* @__PURE__ */ new Set(), R = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new Map();
521
+ let b = null;
522
+ const h = typeof window < "u" && "BroadcastChannel" in window ? new BroadcastChannel(e) : null, j = (o) => {
523
+ const n = [o.$id, o.$scopeId];
524
+ return !(n.some((c) => l.has(c)) || s.size > 0 && !n.some((c) => s.has(c)));
525
+ }, _ = (o, n) => n === o || J === null ? !0 : J.has(n), P = (o, n) => {
526
+ !n || n.originTabId === I || _(o.$namespace, n.namespace) && n.storeId === o.$id && j(o) && o.$patch({ [n.key]: n.newValue }, "remote");
527
+ }, N = () => {
528
+ if (b = null, M.size === 0) return;
529
+ const o = Array.from(M.values());
530
+ M.clear();
531
+ for (const n of o) {
532
+ const c = {
533
+ ...n,
534
+ originTabId: I
459
535
  };
460
- if (y && y.postMessage(p), typeof window < "u" && "localStorage" in window)
536
+ if (h && h.postMessage(c), typeof window < "u" && "localStorage" in window)
461
537
  try {
462
- window.localStorage.setItem(N, JSON.stringify(p)), window.localStorage.removeItem(N);
538
+ window.localStorage.setItem(E, JSON.stringify(c)), window.localStorage.removeItem(E);
463
539
  } catch {
464
540
  }
465
541
  }
466
- }, C = (d) => {
467
- if (E.set(`${d.scopeId}:${d.key}`, d), j <= 0) {
468
- b();
542
+ }, g = (o) => {
543
+ if (M.set(`${o.scopeId}:${o.key}`, o), S <= 0) {
544
+ N();
469
545
  return;
470
546
  }
471
- h || (h = setTimeout(b, j));
547
+ b || (b = setTimeout(N, S));
472
548
  };
473
549
  return {
474
550
  name: "sync",
475
- onInit(d) {
476
- const a = d;
477
- if (A(a)) {
478
- if (P.add(a.$scopeId), y) {
479
- const p = (w) => {
480
- k(a, w.data);
551
+ onInit(o) {
552
+ const n = o;
553
+ if (j(n)) {
554
+ if (U.add(n.$scopeId), h) {
555
+ const c = (d) => {
556
+ P(n, d.data);
481
557
  };
482
- I.set(a.$scopeId, p), y.addEventListener("message", p);
558
+ R.set(n.$scopeId, c), h.addEventListener("message", c);
483
559
  }
484
560
  if (typeof window < "u") {
485
- const p = (w) => {
486
- if (!(w.key !== N || !w.newValue))
561
+ const c = (d) => {
562
+ if (!(d.key !== E || !d.newValue))
487
563
  try {
488
- const q = JSON.parse(w.newValue);
489
- k(a, q);
564
+ const $ = JSON.parse(d.newValue);
565
+ P(n, $);
490
566
  } catch {
491
567
  }
492
568
  };
493
- H.set(a.$scopeId, p), window.addEventListener("storage", p);
569
+ m.set(n.$scopeId, c), window.addEventListener("storage", c);
494
570
  }
495
571
  }
496
572
  },
497
- onMutation(d, a) {
498
- a.origin !== "remote" && A(d) && C(a);
573
+ onMutation(o, n) {
574
+ n.origin !== "remote" && j(o) && g(n);
499
575
  },
500
- onDispose(d) {
501
- const a = d;
502
- P.delete(a.$scopeId);
503
- const p = I.get(a.$scopeId);
504
- y && p && (y.removeEventListener("message", p), I.delete(a.$scopeId));
505
- const w = H.get(a.$scopeId);
506
- typeof window < "u" && w && (window.removeEventListener("storage", w), H.delete(a.$scopeId)), h && (clearTimeout(h), h = null), b(), P.size === 0 && (y == null || y.close());
576
+ onDispose(o) {
577
+ const n = o;
578
+ U.delete(n.$scopeId);
579
+ const c = R.get(n.$scopeId);
580
+ h && c && (h.removeEventListener("message", c), R.delete(n.$scopeId));
581
+ const d = m.get(n.$scopeId);
582
+ typeof window < "u" && d && (window.removeEventListener("storage", d), m.delete(n.$scopeId)), b && (clearTimeout(b), b = null), N(), U.size === 0 && (h == null || h.close());
507
583
  }
508
584
  };
509
585
  }
510
586
  export {
511
- dt as syncPlugin
587
+ ye as syncPlugin
512
588
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storix-js/sync",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -26,6 +26,6 @@
26
26
  "typecheck": "tsc -p tsconfig.json --noEmit"
27
27
  },
28
28
  "dependencies": {
29
- "@storix-js/core": "^0.1.1"
29
+ "@storix-js/core": "workspace:*"
30
30
  }
31
31
  }