@storix-js/persist 0.2.0 → 0.3.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 CHANGED
@@ -22,3 +22,5 @@ const storix = createStorix({
22
22
  ```
23
23
 
24
24
  Use `storage: 'indexedDB'` for larger or more durable client-side state.
25
+
26
+ The plugin writes complete namespace-aware snapshots, including nested modules. If a local mutation occurs while asynchronous hydration is pending, the local state wins; disposed stores are not hydrated later. IndexedDB dispose flush remains best effort because plugin disposal is synchronous.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
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;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@storix-js/core");function t(t={}){let n=t.keyPrefix??`storix`,r=new Set(t.include??[]),i=t.storage??`localStorage`,a=t.serialize??JSON.stringify,o=t.deserialize??(e=>JSON.parse(e)),s=t.debounce??100,c=t.indexedDB?.dbName??`${n}-persist`,l=t.indexedDB?.storeName??`stores`,u=new Map,d=new Map,f=new Set,p=new Set,m=new Set,h=new Set,g=null,_=e=>{let t=[e.$id,e.$scopeId];return!(r.size>0&&!t.some(e=>r.has(e)))},v=e=>`${n}:${e.$namespace}:${e.$id}`,y=()=>g||(g=new Promise((e,t)=>{let n=window.indexedDB.open(c,1);n.onupgradeneeded=()=>{let e=n.result;e.objectStoreNames.contains(l)||e.createObjectStore(l)},n.onsuccess=()=>e(n.result),n.onerror=()=>t(n.error??Error(`IndexedDB open failed`))}),g),b=async e=>{let t=await y(),n=v(e);return new Promise((e,r)=>{let i=t.transaction(l,`readonly`).objectStore(l).get(n);i.onsuccess=()=>{let t=i.result;if(typeof t!=`string`){e(null);return}try{e(o(t))}catch{e(null)}},i.onerror=()=>r(i.error??Error(`IndexedDB read failed`))})},x=async(e,t)=>{let n=await y(),r=v(e),i=a(t);return new Promise((e,t)=>{let a=n.transaction(l,`readwrite`);a.objectStore(l).put(i,r),a.oncomplete=()=>e(),a.onerror=()=>t(a.error??Error(`IndexedDB write failed`)),a.onabort=()=>t(a.error??Error(`IndexedDB write aborted`))})},S=async e=>{if(typeof window>`u`)return null;if(i===`indexedDB`&&`indexedDB`in window)try{return await b(e)}catch{return null}try{let t=window.localStorage.getItem(v(e));return t?o(t):null}catch{return null}},C=e=>{if(typeof window>`u`)return;let t=u.get(e.$scopeId);if(t){if(i===`indexedDB`&&`indexedDB`in window){x(e,t).catch(()=>{});return}try{window.localStorage.setItem(v(e),a(t))}catch{}}},w=e=>{let t=e.$scopeId,n=d.get(t);if(n&&(clearTimeout(n),d.delete(t)),s<=0){C(e);return}let r=setTimeout(()=>{d.delete(t),C(e)},s);d.set(t,r)},T=e=>{let t=e.$scopeId,n=d.get(t);n&&(clearTimeout(n),d.delete(t)),C(e)};return(0,e.definePlugin)({name:`persist`,onInit(e){let t=e;typeof window>`u`||_(t)&&(h.delete(t.$scopeId),f.add(t.$scopeId),(async()=>{let e=await S(t);if(!h.has(t.$scopeId)){if(f.delete(t.$scopeId),p.delete(t.$scopeId)){u.set(t.$scopeId,t.$snapshot());return}if(!e){u.set(t.$scopeId,t.$snapshot());return}m.add(t.$scopeId);try{t.$patch(e,`remote`),u.set(t.$scopeId,t.$snapshot())}finally{m.delete(t.$scopeId)}}})())},onMutation(e){let t=e;typeof window>`u`||_(t)&&(m.has(t.$scopeId)||(f.has(t.$scopeId)&&p.add(t.$scopeId),u.set(t.$scopeId,t.$snapshot()),w(t)))},onDispose(e){let t=e;_(t)&&(h.add(t.$scopeId),f.delete(t.$scopeId),p.delete(t.$scopeId),m.delete(t.$scopeId),T(t),u.delete(t.$scopeId))}})}exports.persistPlugin=t;
package/dist/index.d.ts CHANGED
@@ -12,6 +12,11 @@ export interface PersistPluginOptions {
12
12
  debounce?: number;
13
13
  indexedDB?: IndexedDBPersistOptions;
14
14
  }
15
- export declare function persistPlugin(options?: PersistPluginOptions): import("@storix-js/core").StorePlugin;
15
+ export declare function persistPlugin(options?: PersistPluginOptions): {
16
+ name: string;
17
+ onInit(store: import("@storix-js/core").StoreInstance<{}, {}, {}, {}>): void;
18
+ onMutation(store: import("@storix-js/core").StoreInstance<{}, {}, {}, {}>): void;
19
+ onDispose(store: import("@storix-js/core").StoreInstance<{}, {}, {}, {}>): void;
20
+ };
16
21
  export {};
17
22
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,KAAK,cAAc,GAAG,cAAc,GAAG,WAAW,CAAC;AAEnD,MAAM,WAAW,uBAAuB;IACtC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;IACvD,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,uBAAuB,CAAC;CACrC;AASD,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB,yCAuL/D"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,KAAK,cAAc,GAAG,cAAc,GAAG,WAAW,CAAC;AAEnD,MAAM,WAAW,uBAAuB;IACtC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;IACvD,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,uBAAuB,CAAC;CACrC;AAUD,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB;;;;;EAwN/D"}
package/dist/index.js CHANGED
@@ -1,620 +1,110 @@
1
- var fe = Symbol.for("preact-signals");
2
- function Y() {
3
- if (W > 1)
4
- W--;
5
- else {
6
- var t, e = !1;
7
- for ((function() {
8
- var g = Q;
9
- for (Q = void 0; g !== void 0; )
10
- g.S.v === g.v && (g.S.i = g.i), g = g.o;
11
- })(); H !== void 0; ) {
12
- var i = H;
13
- for (H = void 0, L++; i !== void 0; ) {
14
- var f = i.u;
15
- if (i.u = void 0, i.f &= -3, !(8 & i.f) && ae(i)) try {
16
- i.c();
17
- } catch (g) {
18
- e || (t = g, e = !0);
19
- }
20
- i = f;
21
- }
22
- }
23
- if (L = 0, W--, e) throw t;
24
- }
25
- }
26
- function F(t) {
27
- if (W > 0) return t();
28
- ee = ++de, W++;
29
- try {
30
- return t();
31
- } finally {
32
- Y();
33
- }
34
- }
35
- var p = void 0;
36
- function te(t) {
37
- var e = p;
38
- p = void 0;
39
- try {
40
- return t();
41
- } finally {
42
- p = e;
43
- }
44
- }
45
- var H = void 0, W = 0, L = 0, de = 0, ee = 0, Q = void 0, X = 0;
46
- function se(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;
53
- }
54
- }
55
- function j(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;
57
- }
58
- j.prototype.brand = fe;
59
- j.prototype.h = function() {
60
- return !0;
61
- };
62
- j.prototype.S = function(t) {
63
- var e = this, i = this.t;
64
- i !== t && t.e === void 0 && (t.x = i, this.t = t, i !== void 0 ? i.e = t : te(function() {
65
- var f;
66
- (f = e.W) == null || f.call(e);
67
- }));
68
- };
69
- j.prototype.U = function(t) {
70
- var e = this;
71
- if (this.t !== void 0) {
72
- var i = t.e, f = t.x;
73
- i !== void 0 && (i.x = f, t.e = void 0), f !== void 0 && (f.e = i, t.x = void 0), t === this.t && (this.t = f, f === void 0 && te(function() {
74
- var g;
75
- (g = e.Z) == null || g.call(e);
76
- }));
77
- }
78
- };
79
- j.prototype.subscribe = function(t) {
80
- var e = this;
81
- return ve(function() {
82
- var i = e.value, f = p;
83
- p = void 0;
84
- try {
85
- t(i);
86
- } finally {
87
- p = f;
88
- }
89
- }, { name: "sub" });
90
- };
91
- j.prototype.valueOf = function() {
92
- return this.value;
93
- };
94
- j.prototype.toString = function() {
95
- return this.value + "";
96
- };
97
- j.prototype.toJSON = function() {
98
- return this.value;
99
- };
100
- j.prototype.peek = function() {
101
- var t = this;
102
- return te(function() {
103
- return t.value;
104
- });
105
- };
106
- Object.defineProperty(j.prototype, "value", { get: function() {
107
- var t = se(this);
108
- return t !== void 0 && (t.i = this.i), this.v;
109
- }, set: function(t) {
110
- if (t !== this.v) {
111
- if (L > 100) throw new Error("Cycle detected");
112
- (function(i) {
113
- W !== 0 && L === 0 && i.l !== ee && (i.l = ee, Q = { S: i, v: i.v, i: i.i, o: Q });
114
- })(this), this.v = t, this.i++, X++, W++;
115
- try {
116
- for (var e = this.t; e !== void 0; e = e.x) e.t.N();
117
- } finally {
118
- Y();
119
- }
120
- }
121
- } });
122
- function _(t, e) {
123
- return new j(t, e);
124
- }
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;
127
- return !1;
128
- }
129
- function ce(t) {
130
- for (var e = t.s; e !== void 0; e = e.n) {
131
- var i = e.S.n;
132
- if (i !== void 0 && (e.r = i), e.S.n = e, e.i = -1, e.n === void 0) {
133
- t.s = e;
134
- break;
135
- }
136
- }
137
- }
138
- function ue(t) {
139
- for (var e = t.s, i = void 0; e !== void 0; ) {
140
- var f = e.p;
141
- e.i === -1 ? (e.S.U(e), f !== void 0 && (f.n = e.n), e.n !== void 0 && (e.n.p = f)) : i = e, e.S.n = e.r, e.r !== void 0 && (e.r = void 0), e = f;
142
- }
143
- t.s = i;
144
- }
145
- function J(t, e) {
146
- 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;
147
- }
148
- J.prototype = new j();
149
- J.prototype.h = function() {
150
- if (this.f &= -3, 1 & this.f) return !1;
151
- if ((36 & this.f) == 32 || (this.f &= -5, this.g === X)) return !0;
152
- if (this.g = X, this.f |= 1, this.i > 0 && !ae(this))
153
- return this.f &= -2, !0;
154
- var t = p;
155
- try {
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 (i) {
160
- this.v = i, this.f |= 16, this.i++;
161
- }
162
- return p = t, ue(this), this.f &= -2, !0;
163
- };
164
- J.prototype.S = function(t) {
165
- if (this.t === void 0) {
166
- this.f |= 36;
167
- for (var e = this.s; e !== void 0; e = e.n) e.S.S(e);
168
- }
169
- j.prototype.S.call(this, t);
170
- };
171
- J.prototype.U = function(t) {
172
- if (this.t !== void 0 && (j.prototype.U.call(this, t), this.t === void 0)) {
173
- this.f &= -33;
174
- for (var e = this.s; e !== void 0; e = e.n) e.S.U(e);
175
- }
176
- };
177
- J.prototype.N = function() {
178
- if (!(2 & this.f)) {
179
- this.f |= 6;
180
- for (var t = this.t; t !== void 0; t = t.x) t.t.N();
181
- }
182
- };
183
- Object.defineProperty(J.prototype, "value", { get: function() {
184
- if (1 & this.f) throw new Error("Cycle detected");
185
- var t = se(this);
186
- if (this.h(), t !== void 0 && (t.i = this.i), 16 & this.f) throw this.v;
187
- return this.v;
188
- } });
189
- function re(t, e) {
190
- return new J(t, e);
191
- }
192
- function le(t) {
193
- var e = t.m;
194
- if (t.m = void 0, typeof e == "function") {
195
- W++;
196
- var i = p;
197
- p = void 0;
198
- try {
199
- e();
200
- } catch (f) {
201
- throw t.f &= -2, t.f |= 8, ne(t), f;
202
- } finally {
203
- p = i, Y();
204
- }
205
- }
206
- }
207
- function ne(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);
210
- }
211
- function he(t) {
212
- if (p !== this) throw new Error("Out-of-order effect");
213
- ue(this), p = t, this.f &= -2, 8 & this.f && ne(this), Y();
214
- }
215
- function q(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;
217
- }
218
- q.prototype.c = function() {
219
- var t = this.S();
220
- try {
221
- if (8 & this.f || this.x === void 0) return;
222
- var e = this.x();
223
- typeof e == "function" && (this.m = e);
224
- } finally {
225
- t();
226
- }
227
- };
228
- q.prototype.S = function() {
229
- if (1 & this.f) throw new Error("Cycle detected");
230
- this.f |= 1, this.f &= -9, le(this), ce(this), W++;
231
- var t = p;
232
- return p = this, he.bind(this, t);
233
- };
234
- q.prototype.N = function() {
235
- 2 & this.f || (this.f |= 2, this.u = H, H = this);
236
- };
237
- q.prototype.d = function() {
238
- this.f |= 8, 1 & this.f || ne(this);
239
- };
240
- q.prototype.dispose = function() {
241
- this.d();
242
- };
243
- function ve(t, e) {
244
- var i = new q(t, e);
245
- try {
246
- i.c();
247
- } catch (g) {
248
- throw i.d(), g;
249
- }
250
- var f = i.d.bind(i);
251
- return f[Symbol.dispose] = f, f;
252
- }
253
- function pe(t = {}) {
254
- const e = (t.namespace ?? "default").trim() || "default", i = t.plugins ?? [], f = /* @__PURE__ */ new Map();
255
- function g(x, N) {
256
- return function() {
257
- var M;
258
- const P = `${e}:${x}`, R = f.get(x);
259
- if (R) return R;
260
- const z = N.state(), S = N.modules ?? {}, k = { ...z };
261
- for (const [s, o] of Object.entries(S))
262
- k[s] = o.state();
263
- const w = _(k), B = /* @__PURE__ */ new Map(), K = /* @__PURE__ */ new Map(), Z = /* @__PURE__ */ new Map(), V = /* @__PURE__ */ new Map(), b = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ 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, o) => {
264
- w.value = {
265
- ...w.value,
266
- [s]: o
267
- };
268
- }, $ = new Proxy(
269
- {},
270
- {
271
- get(s, o) {
272
- var l, h;
273
- if (B.has(o)) return (l = B.get(o)) == null ? void 0 : l.value;
274
- if (b.has(o)) return (h = b.get(o)) == null ? void 0 : h.value;
275
- if (c.has(o)) return w.value[o];
276
- const m = V.get(o);
277
- return m ? r(m.moduleName)[m.key] : w.value[o];
278
- }
279
- }
280
- );
281
- for (const [s, o] of Object.entries(N.getters ?? {}))
282
- B.set(
283
- s,
284
- re(() => o(w.value, $))
285
- );
286
- const T = (s, o) => {
287
- var l;
288
- for (const h of a) h(s);
289
- for (const h of i)
290
- (l = h.onMutation) == null || l.call(h, o, s);
291
- }, C = (s, o, l, h) => {
292
- const m = w.value[s];
293
- Object.is(m, o) || (w.value = {
294
- ...w.value,
295
- [s]: o
296
- }, T(
297
- {
298
- namespace: e,
299
- scopeId: P,
300
- storeId: x,
301
- key: s,
302
- oldValue: m,
303
- newValue: o,
304
- origin: l
305
- },
306
- h
307
- ));
308
- }, D = (s, o, l, h, m) => {
309
- const O = r(s), u = O[o];
310
- Object.is(u, l) || (d(s, {
311
- ...O,
312
- [o]: l
313
- }), T(
314
- {
315
- namespace: e,
316
- scopeId: P,
317
- storeId: x,
318
- key: `${s}.${o}`,
319
- oldValue: u,
320
- newValue: l,
321
- origin: h
322
- },
323
- m
324
- ));
325
- }, I = {
326
- $id: x,
327
- $namespace: e,
328
- $scopeId: P,
329
- $subscribe(s) {
330
- return a.add(s), () => a.delete(s);
331
- },
332
- $dispose() {
333
- var s;
334
- a.clear();
335
- for (const o of i)
336
- (s = o.onDispose) == null || s.call(o, E);
337
- f.delete(x);
338
- },
339
- $patch(s, o = "local") {
340
- F(() => {
341
- for (const [l, h] of Object.entries(s)) {
342
- const m = V.get(l);
343
- if (m) {
344
- D(m.moduleName, m.key, h, o, E);
345
- continue;
346
- }
347
- if (l.includes(".")) {
348
- const [O, u] = l.split(".", 2);
349
- if (S[O]) {
350
- D(O, u, h, o, E);
351
- continue;
352
- }
353
- }
354
- if (S[l] && h && typeof h == "object" && !Array.isArray(h)) {
355
- for (const [O, u] of Object.entries(h))
356
- D(l, O, u, o, E);
357
- continue;
358
- }
359
- C(l, h, o, E);
360
- }
361
- });
362
- },
363
- $snapshot() {
364
- return { ...w.value };
365
- }
366
- };
367
- for (const [s, o] of Object.entries(S)) {
368
- const l = /* @__PURE__ */ new Map(), h = /* @__PURE__ */ new Map(), m = new Proxy(
369
- {},
370
- {
371
- get(u, v) {
372
- var U, A, oe;
373
- if (l.has(v)) return (U = l.get(v)) == null ? void 0 : U.value;
374
- if (B.has(v)) return (A = B.get(v)) == null ? void 0 : A.value;
375
- if (b.has(v)) return (oe = b.get(v)) == null ? void 0 : oe.value;
376
- const ie = r(s);
377
- if (v in ie) return ie[v];
378
- if (c.has(v)) return w.value[v];
379
- }
380
- }
381
- );
382
- for (const [u, v] of Object.entries(o.getters ?? {}))
383
- l.set(
384
- u,
385
- re(() => v(r(s), w.value, m))
386
- );
387
- let O;
388
- for (const [u, v] of Object.entries(o.actions ?? {}))
389
- h.set(u, (...U) => F(() => v.apply(O, U)));
390
- O = new Proxy(
391
- {},
392
- {
393
- get(u, v) {
394
- var U;
395
- if (h.has(v)) return h.get(v);
396
- if (l.has(v)) return (U = l.get(v)) == null ? void 0 : U.value;
397
- const A = r(s);
398
- return v in A ? A[v] : E[v];
399
- },
400
- set(u, v, U) {
401
- if (h.has(v) || l.has(v)) return !1;
402
- const A = r(s);
403
- return v in A ? D(s, v, U, "local", E) : E[v] = U, !0;
404
- }
405
- }
406
- ), Z.set(s, O), K.set(s, l);
407
- for (const u of Object.keys(r(s)))
408
- !(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 });
409
- for (const [u, v] of l.entries())
410
- !(c.has(u) || y.has(u) || n.has(u) || Reflect.has(I, u) || S[u] !== void 0) && !b.has(u) && b.set(u, v);
411
- for (const [u, v] of h.entries())
412
- !(c.has(u) || y.has(u) || n.has(u) || Reflect.has(I, u) || S[u] !== void 0) && !Reflect.has(I, u) && (I[u] = v);
413
- }
414
- const E = new Proxy(I, {
415
- get(s, o, l) {
416
- var h, m;
417
- if (Reflect.has(s, o)) return Reflect.get(s, o, l);
418
- if (Z.has(o)) return Z.get(o);
419
- if (B.has(o)) return (h = B.get(o)) == null ? void 0 : h.value;
420
- if (b.has(o)) return (m = b.get(o)) == null ? void 0 : m.value;
421
- if (c.has(o)) return w.value[o];
422
- const O = V.get(o);
423
- return O ? r(O.moduleName)[O.key] : w.value[o];
424
- },
425
- set(s, o, l, h) {
426
- if (Reflect.has(s, o))
427
- return Reflect.set(s, o, l, h);
428
- const m = V.get(o);
429
- return m ? (D(m.moduleName, m.key, l, "local", h), !0) : (C(o, l, "local", h), !0);
430
- }
431
- });
432
- for (const [s, o] of Object.entries(N.actions ?? {}))
433
- I[s] = (...l) => F(() => o.apply(E, l));
434
- for (const s of i)
435
- (M = s.onInit) == null || M.call(s, E);
436
- return f.set(x, E), E;
437
- };
438
- }
439
- function G(x, N) {
440
- return function() {
441
- var M;
442
- const P = `\0machine:${x}`, R = f.get(P);
443
- if (R) return R;
444
- const z = `${e}:${x}`, S = _(N.initial), k = _((N.context ?? (() => ({})))()), w = /* @__PURE__ */ new Set(), B = (a, c) => {
445
- var y;
446
- for (const n of w) n(a);
447
- for (const n of i)
448
- (y = n.onMutation) == null || y.call(n, c, a);
449
- }, K = (a, c, y, n) => {
450
- const r = k.value[a];
451
- Object.is(r, c) || (k.value = { ...k.value, [a]: c }, B({ namespace: e, scopeId: z, storeId: x, key: a, oldValue: r, newValue: c, origin: y }, n));
452
- }, Z = (a, c, y) => {
453
- const n = S.value;
454
- Object.is(n, a) || (S.value = a, B(
455
- { namespace: e, scopeId: z, storeId: x, key: "$state", oldValue: n, newValue: a, origin: c },
456
- y
457
- ));
458
- }, V = {
459
- $id: x,
460
- $namespace: e,
461
- $scopeId: z,
462
- $subscribe(a) {
463
- return w.add(a), () => w.delete(a);
464
- },
465
- $dispose() {
466
- var a;
467
- w.clear();
468
- for (const c of i)
469
- (a = c.onDispose) == null || a.call(c, b);
470
- f.delete(P);
471
- },
472
- $patch(a, c = "local") {
473
- F(() => {
474
- for (const [y, n] of Object.entries(a))
475
- y === "$state" ? Z(n, c, b) : K(y, n, c, b);
476
- });
477
- },
478
- $snapshot() {
479
- return { $state: S.value, ...k.value };
480
- },
481
- send(a, c) {
482
- F(() => {
483
- var y, n, r;
484
- const d = (y = N.actions) == null ? void 0 : y[a];
485
- d && d.call(b, c);
486
- const $ = (r = (n = N.states[S.value]) == null ? void 0 : n.on) == null ? void 0 : r[a];
487
- $ !== void 0 && Z($, "local", b);
488
- });
489
- },
490
- matches(a) {
491
- return S.value === a;
492
- },
493
- can(a) {
494
- const c = N.states[S.value];
495
- return !!(c != null && c.on && a in c.on);
496
- }
497
- }, b = new Proxy(V, {
498
- get(a, c, y) {
499
- return c === "state" ? S.value : Reflect.has(a, c) ? Reflect.get(a, c, y) : k.value[c];
500
- },
501
- set(a, c, y, n) {
502
- return c === "state" ? !1 : Reflect.has(a, c) ? Reflect.set(a, c, y, n) : (K(c, y, "local", n), !0);
503
- }
504
- });
505
- for (const a of i)
506
- (M = a.onInit) == null || M.call(a, b);
507
- return f.set(P, b), b;
508
- };
509
- }
510
- return {
511
- defineStore: g,
512
- defineMachine: G,
513
- use(x) {
514
- i.push(x);
515
- }
516
- };
517
- }
518
- pe();
519
- function ye(t = {}) {
520
- var c, y;
521
- const e = t.keyPrefix ?? "storix", i = new Set(t.include ?? []), f = t.storage ?? "localStorage", g = t.serialize ?? JSON.stringify, G = t.deserialize ?? ((n) => JSON.parse(n)), x = 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 = /* @__PURE__ */ new Map(), R = /* @__PURE__ */ new Map();
522
- let z = null;
523
- const S = (n) => {
524
- const r = [n.$id, n.$scopeId];
525
- return !(i.size > 0 && !r.some((d) => i.has(d)));
526
- }, k = (n) => `${e}:${n.$namespace}:${n.$id}`, w = () => z || (z = new Promise((n, r) => {
527
- const d = window.indexedDB.open(N, 1);
528
- d.onupgradeneeded = () => {
529
- const $ = d.result;
530
- $.objectStoreNames.contains(M) || $.createObjectStore(M);
531
- }, d.onsuccess = () => n(d.result), d.onerror = () => r(d.error ?? new Error("IndexedDB open failed"));
532
- }), z), B = async (n) => {
533
- const r = await w(), d = k(n);
534
- return new Promise(($, T) => {
535
- const D = r.transaction(M, "readonly").objectStore(M).get(d);
536
- D.onsuccess = () => {
537
- const I = D.result;
538
- if (typeof I != "string") {
539
- $(null);
540
- return;
541
- }
542
- try {
543
- $(G(I));
544
- } catch {
545
- $(null);
546
- }
547
- }, D.onerror = () => T(D.error ?? new Error("IndexedDB read failed"));
548
- });
549
- }, K = async (n, r) => {
550
- const d = await w(), $ = k(n), T = g(r);
551
- return new Promise((C, D) => {
552
- const I = d.transaction(M, "readwrite");
553
- I.objectStore(M).put(T, $), I.oncomplete = () => C(), I.onerror = () => D(I.error ?? new Error("IndexedDB write failed")), I.onabort = () => D(I.error ?? new Error("IndexedDB write aborted"));
554
- });
555
- }, Z = async (n) => {
556
- if (typeof window > "u") return null;
557
- if (f === "indexedDB" && "indexedDB" in window)
558
- try {
559
- return await B(n);
560
- } catch {
561
- return null;
562
- }
563
- try {
564
- const r = window.localStorage.getItem(k(n));
565
- return r ? G(r) : null;
566
- } catch {
567
- return null;
568
- }
569
- }, V = (n) => {
570
- if (typeof window > "u") return;
571
- const r = P.get(n.$scopeId);
572
- if (r) {
573
- if (f === "indexedDB" && "indexedDB" in window) {
574
- K(n, r).catch(() => {
575
- });
576
- return;
577
- }
578
- try {
579
- window.localStorage.setItem(k(n), g(r));
580
- } catch {
581
- }
582
- }
583
- }, b = (n) => {
584
- const r = n.$scopeId, d = R.get(r);
585
- if (d && (clearTimeout(d), R.delete(r)), x <= 0) {
586
- V(n);
587
- return;
588
- }
589
- const $ = setTimeout(() => {
590
- R.delete(r), V(n);
591
- }, x);
592
- R.set(r, $);
593
- }, a = (n) => {
594
- const r = n.$scopeId, d = R.get(r);
595
- d && (clearTimeout(d), R.delete(r)), V(n);
596
- };
597
- return {
598
- name: "persist",
599
- onInit(n) {
600
- const r = n;
601
- typeof window > "u" || S(r) && (async () => {
602
- const d = await Z(r);
603
- d && (P.set(r.$scopeId, d), r.$patch(d, "remote"));
604
- })();
605
- },
606
- onMutation(n, r) {
607
- const d = n;
608
- if (typeof window > "u" || !S(d)) return;
609
- const $ = P.get(d.$scopeId) ?? {};
610
- $[r.key] = r.newValue, P.set(d.$scopeId, $), b(d);
611
- },
612
- onDispose(n) {
613
- const r = n;
614
- S(r) && a(r);
615
- }
616
- };
617
- }
618
- export {
619
- ye as persistPlugin
620
- };
1
+ import { definePlugin as e } from "@storix-js/core";
2
+ //#region src/index.ts
3
+ function t(t = {}) {
4
+ let n = t.keyPrefix ?? "storix", r = new Set(t.include ?? []), i = t.storage ?? "localStorage", a = t.serialize ?? JSON.stringify, o = t.deserialize ?? ((e) => JSON.parse(e)), s = t.debounce ?? 100, c = t.indexedDB?.dbName ?? `${n}-persist`, l = t.indexedDB?.storeName ?? "stores", u = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Set(), p = /* @__PURE__ */ new Set(), m = /* @__PURE__ */ new Set(), h = /* @__PURE__ */ new Set(), g = null, _ = (e) => {
5
+ let t = [e.$id, e.$scopeId];
6
+ return !(r.size > 0 && !t.some((e) => r.has(e)));
7
+ }, v = (e) => `${n}:${e.$namespace}:${e.$id}`, y = () => g || (g = new Promise((e, t) => {
8
+ let n = window.indexedDB.open(c, 1);
9
+ n.onupgradeneeded = () => {
10
+ let e = n.result;
11
+ e.objectStoreNames.contains(l) || e.createObjectStore(l);
12
+ }, n.onsuccess = () => e(n.result), n.onerror = () => t(n.error ?? /* @__PURE__ */ Error("IndexedDB open failed"));
13
+ }), g), b = async (e) => {
14
+ let t = await y(), n = v(e);
15
+ return new Promise((e, r) => {
16
+ let i = t.transaction(l, "readonly").objectStore(l).get(n);
17
+ i.onsuccess = () => {
18
+ let t = i.result;
19
+ if (typeof t != "string") {
20
+ e(null);
21
+ return;
22
+ }
23
+ try {
24
+ e(o(t));
25
+ } catch {
26
+ e(null);
27
+ }
28
+ }, i.onerror = () => r(i.error ?? /* @__PURE__ */ Error("IndexedDB read failed"));
29
+ });
30
+ }, x = async (e, t) => {
31
+ let n = await y(), r = v(e), i = a(t);
32
+ return new Promise((e, t) => {
33
+ let a = n.transaction(l, "readwrite");
34
+ a.objectStore(l).put(i, r), a.oncomplete = () => e(), a.onerror = () => t(a.error ?? /* @__PURE__ */ Error("IndexedDB write failed")), a.onabort = () => t(a.error ?? /* @__PURE__ */ Error("IndexedDB write aborted"));
35
+ });
36
+ }, S = async (e) => {
37
+ if (typeof window > "u") return null;
38
+ if (i === "indexedDB" && "indexedDB" in window) try {
39
+ return await b(e);
40
+ } catch {
41
+ return null;
42
+ }
43
+ try {
44
+ let t = window.localStorage.getItem(v(e));
45
+ return t ? o(t) : null;
46
+ } catch {
47
+ return null;
48
+ }
49
+ }, C = (e) => {
50
+ if (typeof window > "u") return;
51
+ let t = u.get(e.$scopeId);
52
+ if (t) {
53
+ if (i === "indexedDB" && "indexedDB" in window) {
54
+ x(e, t).catch(() => {});
55
+ return;
56
+ }
57
+ try {
58
+ window.localStorage.setItem(v(e), a(t));
59
+ } catch {}
60
+ }
61
+ }, w = (e) => {
62
+ let t = e.$scopeId, n = d.get(t);
63
+ if (n && (clearTimeout(n), d.delete(t)), s <= 0) {
64
+ C(e);
65
+ return;
66
+ }
67
+ let r = setTimeout(() => {
68
+ d.delete(t), C(e);
69
+ }, s);
70
+ d.set(t, r);
71
+ }, T = (e) => {
72
+ let t = e.$scopeId, n = d.get(t);
73
+ n && (clearTimeout(n), d.delete(t)), C(e);
74
+ };
75
+ return e({
76
+ name: "persist",
77
+ onInit(e) {
78
+ let t = e;
79
+ typeof window > "u" || _(t) && (h.delete(t.$scopeId), f.add(t.$scopeId), (async () => {
80
+ let e = await S(t);
81
+ if (!h.has(t.$scopeId)) {
82
+ if (f.delete(t.$scopeId), p.delete(t.$scopeId)) {
83
+ u.set(t.$scopeId, t.$snapshot());
84
+ return;
85
+ }
86
+ if (!e) {
87
+ u.set(t.$scopeId, t.$snapshot());
88
+ return;
89
+ }
90
+ m.add(t.$scopeId);
91
+ try {
92
+ t.$patch(e, "remote"), u.set(t.$scopeId, t.$snapshot());
93
+ } finally {
94
+ m.delete(t.$scopeId);
95
+ }
96
+ }
97
+ })());
98
+ },
99
+ onMutation(e) {
100
+ let t = e;
101
+ typeof window > "u" || _(t) && (m.has(t.$scopeId) || (f.has(t.$scopeId) && p.add(t.$scopeId), u.set(t.$scopeId, t.$snapshot()), w(t)));
102
+ },
103
+ onDispose(e) {
104
+ let t = e;
105
+ _(t) && (h.add(t.$scopeId), f.delete(t.$scopeId), p.delete(t.$scopeId), m.delete(t.$scopeId), T(t), u.delete(t.$scopeId));
106
+ }
107
+ });
108
+ }
109
+ //#endregion
110
+ export { t as persistPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storix-js/persist",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -19,13 +19,13 @@
19
19
  "require": "./dist/index.cjs"
20
20
  }
21
21
  },
22
+ "dependencies": {
23
+ "@storix-js/core": "0.3.0"
24
+ },
22
25
  "scripts": {
23
26
  "build": "vite build && tsc -p tsconfig.build.json --emitDeclarationOnly",
24
27
  "dev": "vite build --watch",
25
28
  "test": "vitest run --passWithNoTests",
26
29
  "typecheck": "tsc -p tsconfig.json --noEmit"
27
- },
28
- "dependencies": {
29
- "@storix-js/core": "workspace:*"
30
30
  }
31
- }
31
+ }