UNPKG

atomaric

Version:

Manage your project state

2 lines (1 loc) 7.57 kB
(function(g,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(g=typeof globalThis<"u"?globalThis:g||self,j(g.atomaric={}))})(this,function(g){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const T={},ue=e=>{T.useSyncExternalStore=j=e.useSyncExternalStore,T.keyPathSeparator=e.keyPathSeparator,T.securifyKeyLevel=e.securifyKeyLevel,T.securifyValueLevel=e.securifyValueLevel},X=e=>j(e.subscribe,e.get),Y=e=>e.set,le=e=>e.setDeferred,fe=e=>e.get,ae=e=>e.do,de=e=>[X(e),Y(e)],ge=(e,t)=>new ee(e,t),ye=(e,t)=>{const c=(l,h,A)=>new Proxy(l,{get:(s,n,i)=>{const u=A===0&&t.get!=null?t.get(s,n,i):s[n];return typeof u=="object"&&u!==null?c(Array.isArray(u)?u.slice(0):{...u},h.concat(Array.isArray(s)?+n:n),A+1):u},set:(s,n,i)=>(t.onSet(s,h,n,i,s[n])&&(s[n]=i),!0)});return c(e,[],0)},he=(e,t,c)=>{let l=null;if(typeof e=="number")l=E(t,s=>({increment:n=>{s.set(+s.get()+(n??0))}}));else if(typeof e=="boolean")l=E(t,s=>({toggle:()=>{s.set(!s.get())}}));else if(Array.isArray(e))l=E(t,s=>({push:(...n)=>{s.set(s.get().concat(n))},unshift:(...n)=>{s.set(n.concat(s.get()))},update:n=>{const i=s.get(),u=Z(i,n);u!==i&&s.set(u)},filter:n=>{s.set(s.get().filter(n??pe))},add:n=>{s.get().includes(n)||s.set(s.get().concat([n]))},remove:n=>{const i=s.get().indexOf(n);if(i<0)return;const u=s.get().slice(0);u.splice(i,1),s.set(u)},toggle:(n,i)=>{const u=s.get().slice(),d=u.indexOf(n);d<0?i?u.unshift(n):u.push(n):u.splice(d,1),s.set(u)}}));else if(e instanceof Set)l=E(t,s=>({add:n=>{s.set(new Set(s.get()).add(n))},delete:n=>{const i=new Set(s.get());i.delete(n),s.set(i)},toggle:n=>{const i=new Set(s.get());i.has(n)?i.delete(n):i.add(n),s.set(i)},clear:()=>{s.set(new Set)}}));else if(e instanceof Object){const s=T.keyPathSeparator||".";l=E(t,n=>({setPartial:i=>n.set(u=>({...u,...typeof i=="function"?i(n.get()):i})),update:i=>{const u=n.get(),d=Z(u,i);d!==u&&n.set(d)},setDeepPartial:(i,u,d,m=s)=>{if(!m)return;if(i.includes(m)){let V=i.split(m);const w=V[V.length-1];V=V.slice(0,-1);const U={...n.get()};let y=U,P=d;for(const x of V){P=P==null?void 0:P[Array.isArray(P)?"0":x];const D=y[K(y,x)]??(Array.isArray(P)?[]:{});if(D==null||typeof D!="object"){if(d==null)throw"Incorrect path for setDeepPartial";const I=typeof u=="function"?u(void 0):u;n.get()[i]!==I&&n.do.setPartial({[i]:I});return}y=y[K(y,x)]=Array.isArray(D)?[...D]:{...D}}const S=y[w];y[w]=typeof u=="function"?u(y[w]):u,S!==y[w]&&n.set(U);return}const p=n.get()[i],b=typeof u=="function"?u(p):u;b!==p&&n.do.setPartial({[i]:b})}}))}const h=typeof c=="object"&&c!=null&&"do"in c?c.do((s,n)=>t.set(s,n),()=>t.get(),t,(s,n,i)=>t.setDeferred(s,n,i)):null,A={};return h&&Object.keys(h).forEach(s=>Object.defineProperty(A,s,{get:()=>h[s]})),l&&Object.keys(l).forEach(s=>Object.defineProperty(A,s,{get:()=>l[s]})),A},pe=e=>e,E=(e,t,c)=>t(e),Z=(e,t)=>{const c=Array.isArray(e)?e.slice(0):{...e};let l=!1;const h=ye(e,{onSet:(A,s,n,i,u)=>{if(i===u)return!0;let d=c;l=!0;for(const m of s){const p=d[m];d=d[m]=Array.isArray(p)?p.slice(0):{...p}}return d[n]=i,!0}});return t(h),l?c:e},K=(e,t)=>Array.isArray(e)?`${+t}`:t;var O=(e=>(e[e.None=0]="None",e[e.Simple=1]="Simple",e[e.Middle=2]="Middle",e[e.Strong=3]="Strong",e))(O||{});class ee{constructor(t,c){const l=r=>u=r,h=()=>u,A=new Set,s=r=>r(p());let n=!0,i=!1,u=t,d,m=()=>{},p=()=>h(),b=null,V=()=>{const r=he(t,w,c);return V=()=>r,r};const w=new Proxy(this,{get:(r,a)=>a==="do"?V():r[a],set:we}),U=()=>{n=!0,i!==!0&&m(p()),i=!1;try{W.postMessage({key:f,value:h()})}catch{}},y=(r,a)=>{const $=typeof r=="function"?r(p()):r;$!==p()&&(l($),i=a,n&&(n=!1,A.forEach(s),queueMicrotask(U)))};this.set=(r,a)=>y(r,a),this.get=()=>p(),this.initialValue=t,this.isInitialValue=()=>t===h(),this.subscribe=r=>(A.add(r),()=>{A.delete(r)}),this.reset=()=>{y(t,!0)};const P=(r,a)=>{y(r,a),d=void 0};if(this.setDeferred=(r,a=500,$,v=!0)=>{v&&d===void 0&&y(r,$),clearTimeout(d),d=setTimeout(P,a,r,$)},c==null)return w;let S=null,x=null,D=!0,I=!0,Q=!1,F=0,_=0,se=-1,R=t instanceof Set?r=>new Set(r):r=>r,L=t instanceof Set?r=>{if(r instanceof Set)return Array.from(r);throw console.error(r),"The value is not Set instance"}:r=>r;if(typeof c=="string")S=c;else if("storeKey"in c)D=c.warnOnDuplicateStoreKey??D,I=c.listenStorageChanges??I,S=c.storeKey,R=c.unzipValue??R,L=c.zipValue??L,Q=c.unchangable??Q,F=c.securifyKeyLevel??T.securifyKeyLevel??F,_=c.securifyValueLevel??T.securifyValueLevel??_,x=c.exp??x;else return w;const k=F?M(S,3):S,f=`${_?G:z}${k}`;if(F){const r=`${z}${S}`;r in o&&(o[f]=o[r],delete o[r])}else{const r=`${z}${M(S,3)}`;r in o&&(o[f]=o[r],delete o[r])}if(_){delete o[`${z}${k}`];const r=L,a=R;R=$=>{try{return a(B($,_)[0])}catch{return delete o[f],""}},L=$=>{try{return M([r($)],_)}catch{return delete o[f],""}}}else delete o[`${G}${k}`];const ce=x===null||!(x(w,f in o)instanceof Date)?r=>M([L(r)],0):r=>(b??(b={}),b.exp=x(w,f in o).getTime(),b.exp-Date.now()<24*60*60*1e3&&(clearTimeout(se),clearTimeout(te[f]),se=setTimeout(()=>this.reset(),b.exp-Date.now())),b.exp=Math.trunc(b.exp/1e3),M([L(r),b],0)),ie=r=>{const a=B(r,0);return b=a[1],R(a[0])};let oe=!0;if(H[f]=w,o[`atom/${S}`]&&(o[f]||(o[f]=`[${o[`atom/${S}`]}]`),delete o[`atom/${S}`]),p=()=>{if(p=h,oe){oe=!1;try{l(f in o?ie(o[f]):t)}catch{console.warn("Invalid json value",o[f])}}return h()},m=r=>{if(r===t){this.reset();return}o[f]=ce(r)},this.reset=()=>{delete o[f],y(t,!0)},D&&C[f]!==void 0&&console.warn("Duplicate Atom key",S),I)if(Q){let r=!1,a;q[f]=this,C[f]=()=>{clearTimeout(a),a=setTimeout(()=>r=!1,10),!r&&(r=!0,o[f]=ce(h()))}}else C[f]=r=>{if(r.newValue===null){this.reset();return}try{y(ie(r.newValue))}catch{console.warn("Invalid json value",r.newValue)}};return w}}let W;try{W=new BroadcastChannel("updateHere"),W.addEventListener("message",e=>{var t;(t=q[e.data.key])==null||t.set(e.data.value,!0)})}catch{}const o=localStorage,C={},q={},we=(e,t)=>{throw`${t} is readonly property`};window.addEventListener("storage",e=>{var t;e.key===null||e.newValue===e.oldValue||(t=C[e.key])==null||t.call(C,e)});const Se=o.setItem.bind(o),Ae=o.removeItem.bind(o);o.setItem=(e,t)=>{q[e]===void 0&&Se.call(o,e,t)},o.removeItem=e=>{q[e]===void 0&&Ae.call(o,e)};const be=/"exp":\s*(\d+)/,z="atom\\",G="atom`s\\",H={},te={},re=/[a-z]/gi,N=5,J={};for(let e=54;e<80;e++){if(e===68||e===72||e===55)continue;const t=String.fromCharCode(e+43).toUpperCase(),c=String.fromCharCode(e+43).toLowerCase();J[t]=c,J[c]=t}const ne=e=>J[e]??e,M=(()=>{const e={0:t=>JSON.stringify(t),1:t=>btoa(encodeURI(e[0](t))),2:t=>{const c=e[1](t);return`${c.slice(0,N)}${c.slice(N).replace(re,ne)}`},3:t=>btoa(e[2](t))};return(t,c)=>{try{return e[c](t)}catch(l){if(c===0)throw l;return e[0](t)}}})(),B=(()=>{const e={0:t=>JSON.parse(t),1:t=>e[0](decodeURI(atob(t))),2:t=>e[1](`${t.slice(0,N)}${t.slice(N).replace(re,ne)}`),3:t=>e[2](atob(t))};return(t,c)=>{try{return e[c](t)}catch(l){if(c===0)throw l;return e[0](t)}}})();setTimeout(()=>{Object.keys(o).forEach(e=>{var l;if(typeof o[e]!="string"||!e.startsWith(z)&&!e.startsWith(G))return;const t=+((l=o[e].match(be))==null?void 0:l[1]);if(!t||t*1e3-Date.now()>24*60*60*1e3)return;const c=B(o[e],0);!Array.isArray(c)||c[1]==null||!("exp"in c[1])||c[1].exp!==t||(te[e]=setTimeout(()=>{H[e]?H[e].reset():delete o[e]},t*1e3-Date.now()))})},1e3),g.Atom=ee,g.AtomSecureLevel=O,g.atom=ge,g.configureAtomaric=ue,g.configuredOptions=T,g.useAtom=de,g.useAtomDo=ae,g.useAtomGet=fe,g.useAtomSet=Y,g.useAtomSetDeferred=le,g.useAtomValue=X,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});