UNPKG

tinybase

Version:

A reactive data store and sync engine.

2 lines (1 loc) 3.77 kB
const t=globalThis.window,e=t=>null==t,a=(t,a,n)=>e(t)?n?.():a(t),n=t=>Array.isArray(t),s=t=>t.length,r=t=>{throw Error(t)},o=async(t,e,a)=>{try{return await t()}catch(t){e?.(t)}},i=(t,e)=>t.forEach(e),c=(t,...e)=>t.push(...e),u=t=>t.shift(),g=Object,l=t=>g.getPrototypeOf(t),y=g.keys,d=g.freeze,w=t=>(t=>!e(t)&&a(l(t),(t=>t==g.prototype||e(l(t))),(()=>!0)))(t)&&0==(t=>s(y(t)))(t),v=JSON.stringify,p=JSON.parse,h=t=>e(t)||0==(t=>t?.size??0)(t),S=(t,e)=>t?.forEach(e),A=(t,e)=>t?.delete(e),C=t=>new Map(t),f=(t,e)=>t?.get(e),L=(t,a,n)=>e(n)?(A(t,a),t):t?.set(a,n),b=(t,e,a,n)=>{var s,r;return s=t,r=e,s?.has(r)?n?.(f(t,e)):L(t,e,a()),f(t,e)},M=(t,e,n,r,o=0)=>a((n?b:f)(t,e[o],o>s(e)-2?n:C),(a=>{if(o>s(e)-2)return r?.(a)&&L(t,e[o]),a;const i=M(a,e,n,r,o+1);return h(a)&&L(t,e[o]),i})),E=/^\d+$/,m=t=>new Set(n(t)||e(t)?t:[t]),O=C(),P=C(),T=(t,g,l,y,v,p,T,N={},k=0,z=[])=>{let D,I,J,j=0,x=0,F=0;b(O,z,(()=>0)),b(P,z,(()=>[]));const V=C(),[$,q,B,G,H]=((t=1,e,a)=>1!=t&&e.isMergeable()?[1,e.getMergeableContent,()=>e.getTransactionMergeableChanges(!a),([[t],[e]])=>!w(t)||!w(e),e.setDefaultContent]:2!=t?[0,e.getContent,e.getTransactionChanges,([t,e])=>!w(t)||!w(e),e.setContent]:r("Store type not supported by this Persister"))(T,t,k),[K,Q,R]=(()=>{let t;const[n,r]=(()=>{const t=[];let e=0;return[a=>(a?u(t):null)??""+e++,e=>{E.test(e)&&s(t)<1e3&&c(t,e)}]})(),o=C();return[(e,a,s,r=[],i=()=>[])=>{t??=st;const c=n(1);var u,g;return L(o,c,[e,a,s,r,i]),u=M(a,s??[""],m),g=c,u?.add(g),c},(e,a,...n)=>i(((t,e=[""])=>{const a=[],n=(t,r)=>r==s(e)?c(a,t):null===e[r]?S(t,(t=>n(t,r+1))):i([e[r],null],(e=>n(f(t,e),r+1)));return n(t,0),a})(e,a),(e=>S(e,(e=>f(o,e)[0](t,...a??[],...n))))),t=>a(f(o,t),(([,e,a])=>(M(e,a??[""],void 0,(e=>(A(e,t),h(e)?1:0))),L(o,t),r(t),a))),n=>a(f(o,n),(([a,,n=[],r,o])=>{const c=(...u)=>{const g=s(u);g==s(n)?a(t,...u,...o(u)):e(n[g])?i(r[g]?.(...u)??[],(t=>c(...u,t))):c(...u,n[g])};c()}))]})(),U=t=>{t!=j&&(j=t,Q(V,void 0,j))},W=e=>{($&&n(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},X=async t=>(2!=j&&(U(1),x++,await nt((async()=>{await o((async()=>{const e=await g();n(e)?W(e):t?H(t):r("Content is not an array: "+e)}),(()=>{t&&H(t)})),U(0)}))),st),Y=async t=>(Z(),await X(t),await o((async()=>I=await y((async(t,e)=>{e||t?2!=j&&(U(1),x++,W(e??t),U(0)):await X()}))),p),st),Z=async()=>(I&&(await o((()=>v(I)),p),I=void 0),st),_=async t=>(1!=j&&(U(2),F++,await nt((async()=>{await o((()=>l(q,t)),p),U(0)}))),st),tt=async()=>(et(),await _(),J=t.addDidFinishTransactionListener((()=>{const t=B();G(t)&&_(t)})),st),et=async()=>(J&&(t.delListener(J),J=void 0),st),at=async(t=!1)=>{const[e,a]=t?[et,Z]:[Z,et];return await e(),await a(),st},nt=async(...t)=>(c(f(P,z),...t),await(async()=>{if(!f(O,z)){for(L(O,z,1);!e(D=u(f(P,z)));)await o(D,p);L(O,z,0)}})(),st),st={load:X,startAutoLoad:Y,stopAutoLoad:Z,isAutoLoading:()=>!e(I),save:_,startAutoSave:tt,stopAutoSave:et,isAutoSaving:()=>!e(J),startAutoPersisting:async(t,e=!1)=>{const[a,n]=e?[tt,Y]:[Y,tt];return await a(t),await n(t),st},stopAutoPersisting:at,getStatus:()=>j,addStatusListener:t=>K(t,V),delListener:e=>(R(e),t),schedule:nt,getStore:()=>t,destroy:()=>(f(P,z).splice(0,void 0),at()),getStats:()=>({loads:x,saves:F}),...N};return d(st)},N="storage",k=(e,a,n,s)=>T(e,(async()=>{return t=n.getItem(a),p(t,((t,e)=>""===e?void 0:e));var t}),(async t=>{return n.setItem(a,(e=t(),v(e,((t,e)=>void 0===e?"":e))));var e}),(e=>{const s=t=>{t.storageArea===n&&t.key===a&&o((()=>e(p(t.newValue))),e)};return t.addEventListener(N,s),s}),(e=>t.removeEventListener(N,e)),s,3,{getStorageName:()=>a}),z=(t,e,a)=>k(t,e,localStorage,a),D=(t,e,a)=>k(t,e,sessionStorage,a);export{z as createLocalPersister,D as createSessionPersister};