@storix-js/persist 0.1.0 → 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/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @storix-js/persist
2
+
3
+ Persistence plugin for Storix with `localStorage` and `indexedDB` backends.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @storix-js/core @storix-js/persist
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { createStorix } from '@storix-js/core';
15
+ import { persistPlugin } from '@storix-js/persist';
16
+
17
+ const storix = createStorix({
18
+ plugins: [
19
+ persistPlugin({ storage: 'localStorage', debounce: 100 })
20
+ ]
21
+ });
22
+ ```
23
+
24
+ Use `storage: 'indexedDB'` for larger or more durable client-side state.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var at=Symbol.for("preact-signals");function G(){if(E>1)E--;else{for(var e,t=!1;A!==void 0;){var o=A;for(A=void 0,Q++;o!==void 0;){var d=o.o;if(o.o=void 0,o.f&=-3,!(8&o.f)&&it(o))try{o.c()}catch(O){t||(e=O,t=!0)}o=d}}if(Q=0,E--,t)throw e}}function L(e){if(E>0)return e();E++;try{return e()}finally{G()}}var l=void 0;function et(e){var t=l;l=void 0;try{return e()}finally{l=t}}var A=void 0,E=0,Q=0,F=0;function nt(e){if(l!==void 0){var t=e.n;if(t===void 0||t.t!==l)return t={i:0,S:e,p:l.s,n:void 0,t:l,e:void 0,x:void 0,r:t},l.s!==void 0&&(l.s.n=t),l.s=t,e.n=t,32&l.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=l.s,t.n=void 0,l.s.n=t,l.s=t),t}}function b(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}b.prototype.brand=at;b.prototype.h=function(){return!0};b.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:et(function(){var d;(d=t.W)==null||d.call(t)}))};b.prototype.U=function(e){var t=this;if(this.t!==void 0){var o=e.e,d=e.x;o!==void 0&&(o.x=d,e.e=void 0),d!==void 0&&(d.e=o,e.x=void 0),e===this.t&&(this.t=d,d===void 0&&et(function(){var O;(O=t.Z)==null||O.call(t)}))}};b.prototype.subscribe=function(e){var t=this;return ut(function(){var o=t.value,d=l;l=void 0;try{e(o)}finally{l=d}},{name:"sub"})};b.prototype.valueOf=function(){return this.value};b.prototype.toString=function(){return this.value+""};b.prototype.toJSON=function(){return this.value};b.prototype.peek=function(){var e=l;l=void 0;try{return this.value}finally{l=e}};Object.defineProperty(b.prototype,"value",{get:function(){var e=nt(this);return e!==void 0&&(e.i=this.i),this.v},set:function(e){if(e!==this.v){if(Q>100)throw new Error("Cycle detected");this.v=e,this.i++,F++,E++;try{for(var t=this.t;t!==void 0;t=t.x)t.t.N()}finally{G()}}}});function ct(e,t){return new b(e,t)}function it(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 ot(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 rt(e){for(var t=e.s,o=void 0;t!==void 0;){var d=t.p;t.i===-1?(t.S.U(t),d!==void 0&&(d.n=t.n),t.n!==void 0&&(t.n.p=d)):o=t,t.S.n=t.r,t.r!==void 0&&(t.r=void 0),t=d}e.s=o}function U(e,t){b.call(this,void 0),this.x=e,this.s=void 0,this.g=F-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}U.prototype=new b;U.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&&!it(this))return this.f&=-2,!0;var e=l;try{ot(this),l=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 l=e,rt(this),this.f&=-2,!0};U.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)}b.prototype.S.call(this,e)};U.prototype.U=function(e){if(this.t!==void 0&&(b.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)}};U.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(U.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var e=nt(this);if(this.h(),e!==void 0&&(e.i=this.i),16&this.f)throw this.v;return this.v}});function tt(e,t){return new U(e,t)}function st(e){var t=e.u;if(e.u=void 0,typeof t=="function"){E++;var o=l;l=void 0;try{t()}catch(d){throw e.f&=-2,e.f|=8,X(e),d}finally{l=o,G()}}}function X(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 ft(e){if(l!==this)throw new Error("Out-of-order effect");rt(this),l=e,this.f&=-2,8&this.f&&X(this),G()}function K(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}K.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()}};K.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,st(this),ot(this),E++;var e=l;return l=this,ft.bind(this,e)};K.prototype.N=function(){2&this.f||(this.f|=2,this.o=A,A=this)};K.prototype.d=function(){this.f|=8,1&this.f||X(this)};K.prototype.dispose=function(){this.d()};function ut(e,t){var o=new K(e,t);try{o.c()}catch(O){throw o.d(),O}var d=o.d.bind(o);return d[Symbol.dispose]=d,d}function dt(e={}){const t=(e.namespace??"default").trim()||"default",o=e.plugins??[],d=new Map;function O(S,D){return function(){var Z;const j=`${t}:${S}`,R=d.get(S);if(R)return R;const N=D.state(),x=D.modules??{},z={...N};for(const[i,n]of Object.entries(x))z[i]=n.state();const y=ct(z),I=new Map,H=new Map,q=new Map,P=new Map,$=new Map,T=new Set,M=new Set(Object.keys(N)),W=new Set(Object.keys(D.getters??{})),C=new Set(Object.keys(D.actions??{})),r=i=>y.value[i]??{},a=(i,n)=>{y.value={...y.value,[i]:n}},h=new Proxy({},{get(i,n){var c,f;if(I.has(n))return(c=I.get(n))==null?void 0:c.value;if($.has(n))return(f=$.get(n))==null?void 0:f.value;if(M.has(n))return y.value[n];const p=P.get(n);return p?r(p.moduleName)[p.key]:y.value[n]}});for(const[i,n]of Object.entries(D.getters??{}))I.set(i,tt(()=>n(y.value,h)));const w=(i,n)=>{var c;for(const f of T)f(i);for(const f of o)(c=f.onMutation)==null||c.call(f,n,i)},J=(i,n,c,f)=>{const p=y.value[i];Object.is(p,n)||(y.value={...y.value,[i]:n},w({namespace:t,scopeId:j,storeId:S,key:i,oldValue:p,newValue:n,origin:c},f))},B=(i,n,c,f,p)=>{const m=r(i),s=m[n];Object.is(s,c)||(a(i,{...m,[n]:c}),w({namespace:t,scopeId:j,storeId:S,key:`${i}.${n}`,oldValue:s,newValue:c,origin:f},p))},g={$id:S,$namespace:t,$scopeId:j,$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,v);d.delete(S)},$patch(i,n="local"){L(()=>{for(const[c,f]of Object.entries(i)){const p=P.get(c);if(p){B(p.moduleName,p.key,f,n,v);continue}if(c.includes(".")){const[m,s]=c.split(".",2);if(x[m]){B(m,s,f,n,v);continue}}if(x[c]&&f&&typeof f=="object"&&!Array.isArray(f)){for(const[m,s]of Object.entries(f))B(c,m,s,n,v);continue}J(c,f,n,v)}})},$snapshot(){return{...y.value}}};for(const[i,n]of Object.entries(x)){const c=new Map,f=new Map,p=new Proxy({},{get(s,u){var k,V,Y;if(c.has(u))return(k=c.get(u))==null?void 0:k.value;if(I.has(u))return(V=I.get(u))==null?void 0:V.value;if($.has(u))return(Y=$.get(u))==null?void 0:Y.value;const _=r(i);if(u in _)return _[u];if(M.has(u))return y.value[u]}});for(const[s,u]of Object.entries(n.getters??{}))c.set(s,tt(()=>u(r(i),y.value,p)));let m;for(const[s,u]of Object.entries(n.actions??{}))f.set(s,(...k)=>L(()=>u.apply(m,k)));m=new Proxy({},{get(s,u){var k;if(f.has(u))return f.get(u);if(c.has(u))return(k=c.get(u))==null?void 0:k.value;const V=r(i);return u in V?V[u]:v[u]},set(s,u,k){if(f.has(u)||c.has(u))return!1;const V=r(i);return u in V?B(i,u,k,"local",v):v[u]=k,!0}}),q.set(i,m),H.set(i,c);for(const s of Object.keys(r(i)))!(M.has(s)||W.has(s)||C.has(s)||Reflect.has(g,s)||x[s]!==void 0)&&!P.has(s)&&P.set(s,{moduleName:i,key:s});for(const[s,u]of c.entries())!(M.has(s)||W.has(s)||C.has(s)||Reflect.has(g,s)||x[s]!==void 0)&&!$.has(s)&&$.set(s,u);for(const[s,u]of f.entries())!(M.has(s)||W.has(s)||C.has(s)||Reflect.has(g,s)||x[s]!==void 0)&&!Reflect.has(g,s)&&(g[s]=u)}const v=new Proxy(g,{get(i,n,c){var f,p;if(Reflect.has(i,n))return Reflect.get(i,n,c);if(q.has(n))return q.get(n);if(I.has(n))return(f=I.get(n))==null?void 0:f.value;if($.has(n))return(p=$.get(n))==null?void 0:p.value;if(M.has(n))return y.value[n];const m=P.get(n);return m?r(m.moduleName)[m.key]:y.value[n]},set(i,n,c,f){if(Reflect.has(i,n))return Reflect.set(i,n,c,f);const p=P.get(n);return p?(B(p.moduleName,p.key,c,"local",f),!0):(J(n,c,"local",f),!0)}});for(const[i,n]of Object.entries(D.actions??{}))g[i]=(...c)=>L(()=>n.apply(v,c));for(const i of o)(Z=i.onInit)==null||Z.call(i,v);return d.set(S,v),v}}return{defineStore:O,use(S){o.push(S)}}}dt();function lt(e={}){var W,C;const t=e.keyPrefix??"storix",o=new Set(e.include??[]),d=e.storage??"localStorage",O=e.serialize??JSON.stringify,S=e.deserialize??(r=>JSON.parse(r)),D=e.debounce??100,Z=((W=e.indexedDB)==null?void 0:W.dbName)??`${t}-persist`,j=((C=e.indexedDB)==null?void 0:C.storeName)??"stores",R=new Map,N=new Map;let x=null;const z=r=>{const a=[r.$id,r.$scopeId];return!(o.size>0&&!a.some(h=>o.has(h)))},y=r=>`${t}:${r.$namespace}:${r.$id}`,I=()=>x||(x=new Promise((r,a)=>{const h=window.indexedDB.open(Z,1);h.onupgradeneeded=()=>{const w=h.result;w.objectStoreNames.contains(j)||w.createObjectStore(j)},h.onsuccess=()=>r(h.result),h.onerror=()=>a(h.error??new Error("IndexedDB open failed"))}),x),H=async r=>{const a=await I(),h=y(r);return new Promise((w,J)=>{const g=a.transaction(j,"readonly").objectStore(j).get(h);g.onsuccess=()=>{const v=g.result;if(typeof v!="string"){w(null);return}try{w(S(v))}catch{w(null)}},g.onerror=()=>J(g.error??new Error("IndexedDB read failed"))})},q=async(r,a)=>{const h=await I(),w=y(r),J=O(a);return new Promise((B,g)=>{const v=h.transaction(j,"readwrite");v.objectStore(j).put(J,w),v.oncomplete=()=>B(),v.onerror=()=>g(v.error??new Error("IndexedDB write failed")),v.onabort=()=>g(v.error??new Error("IndexedDB write aborted"))})},P=async r=>{if(typeof window>"u")return null;if(d==="indexedDB"&&"indexedDB"in window)try{return await H(r)}catch{return null}try{const a=window.localStorage.getItem(y(r));return a?S(a):null}catch{return null}},$=r=>{if(typeof window>"u")return;const a=R.get(r.$scopeId);if(a){if(d==="indexedDB"&&"indexedDB"in window){q(r,a).catch(()=>{});return}try{window.localStorage.setItem(y(r),O(a))}catch{}}},T=r=>{const a=r.$scopeId,h=N.get(a);if(h&&(clearTimeout(h),N.delete(a)),D<=0){$(r);return}const w=setTimeout(()=>{N.delete(a),$(r)},D);N.set(a,w)},M=r=>{const a=r.$scopeId,h=N.get(a);h&&(clearTimeout(h),N.delete(a)),$(r)};return{name:"persist",onInit(r){const a=r;typeof window>"u"||z(a)&&(async()=>{const h=await P(a);h&&(R.set(a.$scopeId,h),a.$patch(h,"remote"))})()},onMutation(r,a){const h=r;if(typeof window>"u"||!z(h))return;const w=R.get(h.$scopeId)??{};w[a.key]=a.newValue,R.set(h.$scopeId,w),T(h)},onDispose(r){const a=r;z(a)&&M(a)}}}exports.persistPlugin=lt;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var fe=Symbol.for("preact-signals");function Y(){if(T>1)T--;else{var t,e=!1;for((function(){var g=Q;for(Q=void 0;g!==void 0;)g.S.v===g.v&&(g.S.i=g.i),g=g.o})();H!==void 0;){var o=H;for(H=void 0,L++;o!==void 0;){var f=o.u;if(o.u=void 0,o.f&=-3,!(8&o.f)&&ae(o))try{o.c()}catch(g){e||(t=g,e=!0)}o=f}}if(L=0,T--,e)throw t}}function F(t){if(T>0)return t();ee=++de,T++;try{return t()}finally{Y()}}var p=void 0;function te(t){var e=p;p=void 0;try{return t()}finally{p=e}}var H=void 0,T=0,L=0,de=0,ee=0,Q=void 0,X=0;function se(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 j(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}j.prototype.brand=fe;j.prototype.h=function(){return!0};j.prototype.S=function(t){var e=this,o=this.t;o!==t&&t.e===void 0&&(t.x=o,this.t=t,o!==void 0?o.e=t:te(function(){var f;(f=e.W)==null||f.call(e)}))};j.prototype.U=function(t){var e=this;if(this.t!==void 0){var o=t.e,f=t.x;o!==void 0&&(o.x=f,t.e=void 0),f!==void 0&&(f.e=o,t.x=void 0),t===this.t&&(this.t=f,f===void 0&&te(function(){var g;(g=e.Z)==null||g.call(e)}))}};j.prototype.subscribe=function(t){var e=this;return ve(function(){var o=e.value,f=p;p=void 0;try{t(o)}finally{p=f}},{name:"sub"})};j.prototype.valueOf=function(){return this.value};j.prototype.toString=function(){return this.value+""};j.prototype.toJSON=function(){return this.value};j.prototype.peek=function(){var t=this;return te(function(){return t.value})};Object.defineProperty(j.prototype,"value",{get:function(){var t=se(this);return t!==void 0&&(t.i=this.i),this.v},set:function(t){if(t!==this.v){if(L>100)throw new Error("Cycle detected");(function(o){T!==0&&L===0&&o.l!==ee&&(o.l=ee,Q={S:o,v:o.v,i:o.i,o:Q})})(this),this.v=t,this.i++,X++,T++;try{for(var e=this.t;e!==void 0;e=e.x)e.t.N()}finally{Y()}}}});function _(t,e){return new j(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 o=e.S.n;if(o!==void 0&&(e.r=o),e.S.n=e,e.i=-1,e.n===void 0){t.s=e;break}}}function ue(t){for(var e=t.s,o=void 0;e!==void 0;){var f=e.p;e.i===-1?(e.S.U(e),f!==void 0&&(f.n=e.n),e.n!==void 0&&(e.n.p=f)):o=e,e.S.n=e.r,e.r!==void 0&&(e.r=void 0),e=f}t.s=o}function A(t,e){j.call(this,void 0),this.x=t,this.s=void 0,this.g=X-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 j;A.prototype.h=function(){if(this.f&=-3,1&this.f)return!1;if((36&this.f)==32||(this.f&=-5,this.g===X))return!0;if(this.g=X,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(o){this.v=o,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)}j.prototype.S.call(this,t)};A.prototype.U=function(t){if(this.t!==void 0&&(j.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=se(this);if(this.h(),t!==void 0&&(t.i=this.i),16&this.f)throw this.v;return this.v}});function re(t,e){return new A(t,e)}function le(t){var e=t.m;if(t.m=void 0,typeof e=="function"){T++;var o=p;p=void 0;try{e()}catch(f){throw t.f&=-2,t.f|=8,ne(t),f}finally{p=o,Y()}}}function ne(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 he(t){if(p!==this)throw new Error("Out-of-order effect");ue(this),p=t,this.f&=-2,8&this.f&&ne(this),Y()}function q(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}q.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()}};q.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,le(this),ce(this),T++;var t=p;return p=this,he.bind(this,t)};q.prototype.N=function(){2&this.f||(this.f|=2,this.u=H,H=this)};q.prototype.d=function(){this.f|=8,1&this.f||ne(this)};q.prototype.dispose=function(){this.d()};function ve(t,e){var o=new q(t,e);try{o.c()}catch(g){throw o.d(),g}var f=o.d.bind(o);return f[Symbol.dispose]=f,f}function pe(t={}){const e=(t.namespace??"default").trim()||"default",o=t.plugins??[],f=new Map;function g($,N){return function(){var M;const P=`${e}:${$}`,R=f.get($);if(R)return R;const z=N.state(),S=N.modules??{},k={...z};for(const[s,i]of Object.entries(S))k[s]=i.state();const w=_(k),B=new Map,J=new Map,W=new Map,V=new Map,b=new Map,a=new Set,c=new Set(Object.keys(z)),y=new Set(Object.keys(N.getters??{})),n=new Set(Object.keys(N.actions??{})),r=s=>w.value[s]??{},d=(s,i)=>{w.value={...w.value,[s]:i}},x=new Proxy({},{get(s,i){var l,h;if(B.has(i))return(l=B.get(i))==null?void 0:l.value;if(b.has(i))return(h=b.get(i))==null?void 0:h.value;if(c.has(i))return w.value[i];const m=V.get(i);return m?r(m.moduleName)[m.key]:w.value[i]}});for(const[s,i]of Object.entries(N.getters??{}))B.set(s,re(()=>i(w.value,x)));const K=(s,i)=>{var l;for(const h of a)h(s);for(const h of o)(l=h.onMutation)==null||l.call(h,i,s)},C=(s,i,l,h)=>{const m=w.value[s];Object.is(m,i)||(w.value={...w.value,[s]:i},K({namespace:e,scopeId:P,storeId:$,key:s,oldValue:m,newValue:i,origin:l},h))},D=(s,i,l,h,m)=>{const O=r(s),u=O[i];Object.is(u,l)||(d(s,{...O,[i]:l}),K({namespace:e,scopeId:P,storeId:$,key:`${s}.${i}`,oldValue:u,newValue:l,origin:h},m))},I={$id:$,$namespace:e,$scopeId:P,$subscribe(s){return a.add(s),()=>a.delete(s)},$dispose(){var s;a.clear();for(const i of o)(s=i.onDispose)==null||s.call(i,E);f.delete($)},$patch(s,i="local"){F(()=>{for(const[l,h]of Object.entries(s)){const m=V.get(l);if(m){D(m.moduleName,m.key,h,i,E);continue}if(l.includes(".")){const[O,u]=l.split(".",2);if(S[O]){D(O,u,h,i,E);continue}}if(S[l]&&h&&typeof h=="object"&&!Array.isArray(h)){for(const[O,u]of Object.entries(h))D(l,O,u,i,E);continue}C(l,h,i,E)}})},$snapshot(){return{...w.value}}};for(const[s,i]of Object.entries(S)){const l=new Map,h=new Map,m=new Proxy({},{get(u,v){var U,Z,ie;if(l.has(v))return(U=l.get(v))==null?void 0:U.value;if(B.has(v))return(Z=B.get(v))==null?void 0:Z.value;if(b.has(v))return(ie=b.get(v))==null?void 0:ie.value;const oe=r(s);if(v in oe)return oe[v];if(c.has(v))return w.value[v]}});for(const[u,v]of Object.entries(i.getters??{}))l.set(u,re(()=>v(r(s),w.value,m)));let O;for(const[u,v]of Object.entries(i.actions??{}))h.set(u,(...U)=>F(()=>v.apply(O,U)));O=new Proxy({},{get(u,v){var U;if(h.has(v))return h.get(v);if(l.has(v))return(U=l.get(v))==null?void 0:U.value;const Z=r(s);return v in Z?Z[v]:E[v]},set(u,v,U){if(h.has(v)||l.has(v))return!1;const Z=r(s);return v in Z?D(s,v,U,"local",E):E[v]=U,!0}}),W.set(s,O),J.set(s,l);for(const u of Object.keys(r(s)))!(c.has(u)||y.has(u)||n.has(u)||Reflect.has(I,u)||S[u]!==void 0)&&!V.has(u)&&V.set(u,{moduleName:s,key:u});for(const[u,v]of l.entries())!(c.has(u)||y.has(u)||n.has(u)||Reflect.has(I,u)||S[u]!==void 0)&&!b.has(u)&&b.set(u,v);for(const[u,v]of h.entries())!(c.has(u)||y.has(u)||n.has(u)||Reflect.has(I,u)||S[u]!==void 0)&&!Reflect.has(I,u)&&(I[u]=v)}const E=new Proxy(I,{get(s,i,l){var h,m;if(Reflect.has(s,i))return Reflect.get(s,i,l);if(W.has(i))return W.get(i);if(B.has(i))return(h=B.get(i))==null?void 0:h.value;if(b.has(i))return(m=b.get(i))==null?void 0:m.value;if(c.has(i))return w.value[i];const O=V.get(i);return O?r(O.moduleName)[O.key]:w.value[i]},set(s,i,l,h){if(Reflect.has(s,i))return Reflect.set(s,i,l,h);const m=V.get(i);return m?(D(m.moduleName,m.key,l,"local",h),!0):(C(i,l,"local",h),!0)}});for(const[s,i]of Object.entries(N.actions??{}))I[s]=(...l)=>F(()=>i.apply(E,l));for(const s of o)(M=s.onInit)==null||M.call(s,E);return f.set($,E),E}}function G($,N){return function(){var M;const P=`\0machine:${$}`,R=f.get(P);if(R)return R;const z=`${e}:${$}`,S=_(N.initial),k=_((N.context??(()=>({})))()),w=new Set,B=(a,c)=>{var y;for(const n of w)n(a);for(const n of o)(y=n.onMutation)==null||y.call(n,c,a)},J=(a,c,y,n)=>{const r=k.value[a];Object.is(r,c)||(k.value={...k.value,[a]:c},B({namespace:e,scopeId:z,storeId:$,key:a,oldValue:r,newValue:c,origin:y},n))},W=(a,c,y)=>{const n=S.value;Object.is(n,a)||(S.value=a,B({namespace:e,scopeId:z,storeId:$,key:"$state",oldValue:n,newValue:a,origin:c},y))},V={$id:$,$namespace:e,$scopeId:z,$subscribe(a){return w.add(a),()=>w.delete(a)},$dispose(){var a;w.clear();for(const c of o)(a=c.onDispose)==null||a.call(c,b);f.delete(P)},$patch(a,c="local"){F(()=>{for(const[y,n]of Object.entries(a))y==="$state"?W(n,c,b):J(y,n,c,b)})},$snapshot(){return{$state:S.value,...k.value}},send(a,c){F(()=>{var y,n,r;const d=(y=N.actions)==null?void 0:y[a];d&&d.call(b,c);const x=(r=(n=N.states[S.value])==null?void 0:n.on)==null?void 0:r[a];x!==void 0&&W(x,"local",b)})},matches(a){return S.value===a},can(a){const c=N.states[S.value];return!!(c!=null&&c.on&&a in c.on)}},b=new Proxy(V,{get(a,c,y){return c==="state"?S.value:Reflect.has(a,c)?Reflect.get(a,c,y):k.value[c]},set(a,c,y,n){return c==="state"?!1:Reflect.has(a,c)?Reflect.set(a,c,y,n):(J(c,y,"local",n),!0)}});for(const a of o)(M=a.onInit)==null||M.call(a,b);return f.set(P,b),b}}return{defineStore:g,defineMachine:G,use($){o.push($)}}}pe();function ye(t={}){var c,y;const e=t.keyPrefix??"storix",o=new Set(t.include??[]),f=t.storage??"localStorage",g=t.serialize??JSON.stringify,G=t.deserialize??(n=>JSON.parse(n)),$=t.debounce??100,N=((c=t.indexedDB)==null?void 0:c.dbName)??`${e}-persist`,M=((y=t.indexedDB)==null?void 0:y.storeName)??"stores",P=new Map,R=new Map;let z=null;const S=n=>{const r=[n.$id,n.$scopeId];return!(o.size>0&&!r.some(d=>o.has(d)))},k=n=>`${e}:${n.$namespace}:${n.$id}`,w=()=>z||(z=new Promise((n,r)=>{const d=window.indexedDB.open(N,1);d.onupgradeneeded=()=>{const x=d.result;x.objectStoreNames.contains(M)||x.createObjectStore(M)},d.onsuccess=()=>n(d.result),d.onerror=()=>r(d.error??new Error("IndexedDB open failed"))}),z),B=async n=>{const r=await w(),d=k(n);return new Promise((x,K)=>{const D=r.transaction(M,"readonly").objectStore(M).get(d);D.onsuccess=()=>{const I=D.result;if(typeof I!="string"){x(null);return}try{x(G(I))}catch{x(null)}},D.onerror=()=>K(D.error??new Error("IndexedDB read failed"))})},J=async(n,r)=>{const d=await w(),x=k(n),K=g(r);return new Promise((C,D)=>{const I=d.transaction(M,"readwrite");I.objectStore(M).put(K,x),I.oncomplete=()=>C(),I.onerror=()=>D(I.error??new Error("IndexedDB write failed")),I.onabort=()=>D(I.error??new Error("IndexedDB write aborted"))})},W=async n=>{if(typeof window>"u")return null;if(f==="indexedDB"&&"indexedDB"in window)try{return await B(n)}catch{return null}try{const r=window.localStorage.getItem(k(n));return r?G(r):null}catch{return null}},V=n=>{if(typeof window>"u")return;const r=P.get(n.$scopeId);if(r){if(f==="indexedDB"&&"indexedDB"in window){J(n,r).catch(()=>{});return}try{window.localStorage.setItem(k(n),g(r))}catch{}}},b=n=>{const r=n.$scopeId,d=R.get(r);if(d&&(clearTimeout(d),R.delete(r)),$<=0){V(n);return}const x=setTimeout(()=>{R.delete(r),V(n)},$);R.set(r,x)},a=n=>{const r=n.$scopeId,d=R.get(r);d&&(clearTimeout(d),R.delete(r)),V(n)};return{name:"persist",onInit(n){const r=n;typeof window>"u"||S(r)&&(async()=>{const d=await W(r);d&&(P.set(r.$scopeId,d),r.$patch(d,"remote"))})()},onMutation(n,r){const d=n;if(typeof window>"u"||!S(d))return;const x=P.get(d.$scopeId)??{};x[r.key]=r.newValue,P.set(d.$scopeId,x),b(d)},onDispose(n){const r=n;S(r)&&a(r)}}}exports.persistPlugin=ye;