@storix-js/persist 0.2.0 → 0.2.1

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 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
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=require("@storix-js/core");function j(s={}){var D,I;const m=s.keyPrefix??"storix",y=new Set(s.include??[]),g=s.storage??"localStorage",b=s.serialize??JSON.stringify,x=s.deserialize??(e=>JSON.parse(e)),S=s.debounce??100,B=((D=s.indexedDB)==null?void 0:D.dbName)??`${m}-persist`,c=((I=s.indexedDB)==null?void 0:I.storeName)??"stores",a=new Map,d=new Map;let u=null;const w=e=>{const t=[e.$id,e.$scopeId];return!(y.size>0&&!t.some(n=>y.has(n)))},l=e=>`${m}:${e.$namespace}:${e.$id}`,h=()=>u||(u=new Promise((e,t)=>{const n=window.indexedDB.open(B,1);n.onupgradeneeded=()=>{const r=n.result;r.objectStoreNames.contains(c)||r.createObjectStore(c)},n.onsuccess=()=>e(n.result),n.onerror=()=>t(n.error??new Error("IndexedDB open failed"))}),u),P=async e=>{const t=await h(),n=l(e);return new Promise((r,p)=>{const i=t.transaction(c,"readonly").objectStore(c).get(n);i.onsuccess=()=>{const o=i.result;if(typeof o!="string"){r(null);return}try{r(x(o))}catch{r(null)}},i.onerror=()=>p(i.error??new Error("IndexedDB read failed"))})},N=async(e,t)=>{const n=await h(),r=l(e),p=b(t);return new Promise(($,i)=>{const o=n.transaction(c,"readwrite");o.objectStore(c).put(p,r),o.oncomplete=()=>$(),o.onerror=()=>i(o.error??new Error("IndexedDB write failed")),o.onabort=()=>i(o.error??new Error("IndexedDB write aborted"))})},k=async e=>{if(typeof window>"u")return null;if(g==="indexedDB"&&"indexedDB"in window)try{return await P(e)}catch{return null}try{const t=window.localStorage.getItem(l(e));return t?x(t):null}catch{return null}},f=e=>{if(typeof window>"u")return;const t=a.get(e.$scopeId);if(t){if(g==="indexedDB"&&"indexedDB"in window){N(e,t).catch(()=>{});return}try{window.localStorage.setItem(l(e),b(t))}catch{}}},z=e=>{const t=e.$scopeId,n=d.get(t);if(n&&(clearTimeout(n),d.delete(t)),S<=0){f(e);return}const r=setTimeout(()=>{d.delete(t),f(e)},S);d.set(t,r)},M=e=>{const t=e.$scopeId,n=d.get(t);n&&(clearTimeout(n),d.delete(t)),f(e)};return T.definePlugin({name:"persist",onInit(e){const t=e;typeof window>"u"||w(t)&&(async()=>{const n=await k(t);n&&(a.set(t.$scopeId,n),t.$patch(n,"remote"))})()},onMutation(e,t){const n=e;if(typeof window>"u"||!w(n))return;const r=a.get(n.$scopeId)??{};r[t.key]=t.newValue,a.set(n.$scopeId,r),z(n)},onDispose(e){const t=e;w(t)&&M(t)}})}exports.persistPlugin=j;
package/dist/index.js CHANGED
@@ -1,620 +1,103 @@
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);
1
+ import { definePlugin as M } from "@storix-js/core";
2
+ function j(s = {}) {
3
+ var D, I;
4
+ const m = s.keyPrefix ?? "storix", y = new Set(s.include ?? []), x = s.storage ?? "localStorage", b = s.serialize ?? JSON.stringify, g = s.deserialize ?? ((e) => JSON.parse(e)), S = s.debounce ?? 100, B = ((D = s.indexedDB) == null ? void 0 : D.dbName) ?? `${m}-persist`, c = ((I = s.indexedDB) == null ? void 0 : I.storeName) ?? "stores", a = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map();
5
+ let u = null;
6
+ const w = (e) => {
7
+ const t = [e.$id, e.$scopeId];
8
+ return !(y.size > 0 && !t.some((n) => y.has(n)));
9
+ }, l = (e) => `${m}:${e.$namespace}:${e.$id}`, h = () => u || (u = new Promise((e, t) => {
10
+ const n = window.indexedDB.open(B, 1);
11
+ n.onupgradeneeded = () => {
12
+ const r = n.result;
13
+ r.objectStoreNames.contains(c) || r.createObjectStore(c);
14
+ }, n.onsuccess = () => e(n.result), n.onerror = () => t(n.error ?? new Error("IndexedDB open failed"));
15
+ }), u), P = async (e) => {
16
+ const t = await h(), n = l(e);
17
+ return new Promise((r, p) => {
18
+ const i = t.transaction(c, "readonly").objectStore(c).get(n);
19
+ i.onsuccess = () => {
20
+ const o = i.result;
21
+ if (typeof o != "string") {
22
+ r(null);
540
23
  return;
541
24
  }
542
25
  try {
543
- $(G(I));
26
+ r(g(o));
544
27
  } catch {
545
- $(null);
28
+ r(null);
546
29
  }
547
- }, D.onerror = () => T(D.error ?? new Error("IndexedDB read failed"));
30
+ }, i.onerror = () => p(i.error ?? new Error("IndexedDB read failed"));
548
31
  });
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"));
32
+ }, N = async (e, t) => {
33
+ const n = await h(), r = l(e), p = b(t);
34
+ return new Promise(($, i) => {
35
+ const o = n.transaction(c, "readwrite");
36
+ o.objectStore(c).put(p, r), o.oncomplete = () => $(), o.onerror = () => i(o.error ?? new Error("IndexedDB write failed")), o.onabort = () => i(o.error ?? new Error("IndexedDB write aborted"));
554
37
  });
555
- }, Z = async (n) => {
38
+ }, k = async (e) => {
556
39
  if (typeof window > "u") return null;
557
- if (f === "indexedDB" && "indexedDB" in window)
40
+ if (x === "indexedDB" && "indexedDB" in window)
558
41
  try {
559
- return await B(n);
42
+ return await P(e);
560
43
  } catch {
561
44
  return null;
562
45
  }
563
46
  try {
564
- const r = window.localStorage.getItem(k(n));
565
- return r ? G(r) : null;
47
+ const t = window.localStorage.getItem(l(e));
48
+ return t ? g(t) : null;
566
49
  } catch {
567
50
  return null;
568
51
  }
569
- }, V = (n) => {
52
+ }, f = (e) => {
570
53
  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(() => {
54
+ const t = a.get(e.$scopeId);
55
+ if (t) {
56
+ if (x === "indexedDB" && "indexedDB" in window) {
57
+ N(e, t).catch(() => {
575
58
  });
576
59
  return;
577
60
  }
578
61
  try {
579
- window.localStorage.setItem(k(n), g(r));
62
+ window.localStorage.setItem(l(e), b(t));
580
63
  } catch {
581
64
  }
582
65
  }
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);
66
+ }, z = (e) => {
67
+ const t = e.$scopeId, n = d.get(t);
68
+ if (n && (clearTimeout(n), d.delete(t)), S <= 0) {
69
+ f(e);
587
70
  return;
588
71
  }
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);
72
+ const r = setTimeout(() => {
73
+ d.delete(t), f(e);
74
+ }, S);
75
+ d.set(t, r);
76
+ }, E = (e) => {
77
+ const t = e.$scopeId, n = d.get(t);
78
+ n && (clearTimeout(n), d.delete(t)), f(e);
596
79
  };
597
- return {
80
+ return M({
598
81
  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"));
82
+ onInit(e) {
83
+ const t = e;
84
+ typeof window > "u" || w(t) && (async () => {
85
+ const n = await k(t);
86
+ n && (a.set(t.$scopeId, n), t.$patch(n, "remote"));
604
87
  })();
605
88
  },
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);
89
+ onMutation(e, t) {
90
+ const n = e;
91
+ if (typeof window > "u" || !w(n)) return;
92
+ const r = a.get(n.$scopeId) ?? {};
93
+ r[t.key] = t.newValue, a.set(n.$scopeId, r), z(n);
611
94
  },
612
- onDispose(n) {
613
- const r = n;
614
- S(r) && a(r);
95
+ onDispose(e) {
96
+ const t = e;
97
+ w(t) && E(t);
615
98
  }
616
- };
99
+ });
617
100
  }
618
101
  export {
619
- ye as persistPlugin
102
+ j as persistPlugin
620
103
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storix-js/persist",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",