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