Comparing version 2.9.1 to 2.9.2
@@ -6,3 +6,3 @@ { | ||
"type": "commonjs", | ||
"version": "2.9.1", | ||
"version": "2.9.2", | ||
"main": "./index.js", | ||
@@ -9,0 +9,0 @@ "types": "./index.d.ts", |
@@ -53,2 +53,3 @@ System.register(['jotai/vanilla'], (function (exports) { | ||
const atoms = /* @__PURE__ */ new Map(); | ||
const listeners = /* @__PURE__ */ new Set(); | ||
const createAtom = (param) => { | ||
@@ -75,11 +76,28 @@ let item; | ||
atoms.set(param, [newAtom, Date.now()]); | ||
notifyListeners("CREATE", param, newAtom); | ||
return newAtom; | ||
}; | ||
function notifyListeners(type, param, atom) { | ||
for (const listener of listeners) { | ||
listener({ type, param, atom }); | ||
} | ||
} | ||
createAtom.unstable_listen = (callback) => { | ||
listeners.add(callback); | ||
return () => { | ||
listeners.delete(callback); | ||
}; | ||
}; | ||
createAtom.getParams = () => atoms.keys(); | ||
createAtom.remove = (param) => { | ||
if (areEqual === void 0) { | ||
if (!atoms.has(param)) return; | ||
const [atom] = atoms.get(param); | ||
atoms.delete(param); | ||
notifyListeners("REMOVE", param, atom); | ||
} else { | ||
for (const [key] of atoms) { | ||
for (const [key, [atom]] of atoms) { | ||
if (areEqual(key, param)) { | ||
atoms.delete(key); | ||
notifyListeners("REMOVE", key, atom); | ||
break; | ||
@@ -93,5 +111,6 @@ } | ||
if (!shouldRemove) return; | ||
for (const [key, value] of atoms) { | ||
if (shouldRemove(value[1], key)) { | ||
for (const [key, [atom, createdAt]] of atoms) { | ||
if (shouldRemove(createdAt, key)) { | ||
atoms.delete(key); | ||
notifyListeners("REMOVE", key, atom); | ||
} | ||
@@ -98,0 +117,0 @@ } |
@@ -1,1 +0,1 @@ | ||
System.register(["jotai/vanilla"],function(x){"use strict";var d;return{setters:[function(k){d=k.atom}],execute:function(){x({atomFamily:_,atomWithDefault:X,atomWithLazy:ft,atomWithObservable:et,atomWithReducer:$,atomWithRefresh:lt,atomWithReset:J,atomWithStorage:tt,createJSONStorage:D,freezeAtom:A,freezeAtomCreator:q,loadable:ot,selectAtom:K,splitAtom:U,unstable_withStorageValidator:Y,unwrap:ct});const k=x("RESET",Symbol(""));function J(t){const n=d(t,(r,c,a)=>{const s=typeof a=="function"?a(r(n)):a;c(n,s===k?t:s)});return n}function $(t,n){return d(t,function(r,c,a){c(this,n(r(this),a))})}function _(t,n){let r=null;const c=new Map,a=s=>{let e;if(n===void 0)e=c.get(s);else for(const[u,o]of c)if(n(u,s)){e=o;break}if(e!==void 0)if(r!=null&&r(e[1],s))a.remove(s);else return e[0];const i=t(s);return c.set(s,[i,Date.now()]),i};return a.remove=s=>{if(n===void 0)c.delete(s);else for(const[e]of c)if(n(e,s)){c.delete(e);break}},a.setShouldRemove=s=>{if(r=s,!!r)for(const[e,i]of c)r(i[1],e)&&c.delete(e)},a}const I=(t,n,r)=>(n.has(r)?n:n.set(r,t())).get(r),C=new WeakMap,F=(t,n,r,c)=>{const a=I(()=>new WeakMap,C,n),s=I(()=>new WeakMap,a,r);return I(t,s,c)};function K(t,n,r=Object.is){return F(()=>{const c=Symbol(),a=([e,i])=>{if(i===c)return n(e);const u=n(e,i);return r(i,u)?i:u},s=d(e=>{const i=e(s),u=e(t);return a([u,i])});return s.init=c,s},t,n,r)}const j=new WeakSet,M=t=>{if(typeof t!="object"||t===null)return;Object.freeze(t);const n=Object.getOwnPropertyNames(t);for(const r of n){const c=t[r];M(c)}return t};function A(t){if(j.has(t))return t;j.add(t);const n=t.read;if(t.read=function(r,c){return M(n.call(this,r,c))},"write"in t){const r=t.write;t.write=function(c,a,...s){return r.call(this,c,(...e)=>(e[0]===t&&(e[1]=M(e[1])),a(...e)),...s)}}return t}function q(t){return(...n)=>A(t(...n))}const R=(t,n,r)=>(n.has(r)?n:n.set(r,t())).get(r),B=new WeakMap,G=(t,n,r)=>{const c=R(()=>new WeakMap,B,n);return R(t,c,r)},H={},P=t=>!!t.write,Q=t=>typeof t=="function";function U(t,n){return G(()=>{const r=new WeakMap,c=(e,i)=>{let u=r.get(e);if(u)return u;const o=i&&r.get(i),l=[],f=[];return e.forEach((b,h)=>{const m=n?n(b):h;f[h]=m;const p=o&&o.atomList[o.keyList.indexOf(m)];if(p){l[h]=p;return}const O=g=>{const v=g(a),y=g(t),S=c(y,v==null?void 0:v.arr).keyList.indexOf(m);if(S<0||S>=y.length){const w=e[c(e).keyList.indexOf(m)];if(w)return w;throw new Error("splitAtom: index out of bounds for read")}return y[S]},L=(g,v,y)=>{const S=g(a),w=g(t),W=c(w,S==null?void 0:S.arr).keyList.indexOf(m);if(W<0||W>=w.length)throw new Error("splitAtom: index out of bounds for write");const V=Q(y)?y(w[W]):y;Object.is(w[W],V)||v(t,[...w.slice(0,W),V,...w.slice(W+1)])};l[h]=P(t)?d(O,L):d(O)}),o&&o.keyList.length===f.length&&o.keyList.every((b,h)=>b===f[h])?u=o:u={arr:e,atomList:l,keyList:f},r.set(e,u),u},a=d(e=>{const i=e(a),u=e(t);return c(u,i==null?void 0:i.arr)});a.init=void 0;const s=P(t)?d(e=>e(a).atomList,(e,i,u)=>{switch(u.type){case"remove":{const o=e(s).indexOf(u.atom);if(o>=0){const l=e(t);i(t,[...l.slice(0,o),...l.slice(o+1)])}break}case"insert":{const o=u.before?e(s).indexOf(u.before):e(s).length;if(o>=0){const l=e(t);i(t,[...l.slice(0,o),u.value,...l.slice(o)])}break}case"move":{const o=e(s).indexOf(u.atom),l=u.before?e(s).indexOf(u.before):e(s).length;if(o>=0&&l>=0){const f=e(t);o<l?i(t,[...f.slice(0,o),...f.slice(o+1,l),f[o],...f.slice(l)]):i(t,[...f.slice(0,l),f[o],...f.slice(l,o),...f.slice(o+1)])}break}}}):d(e=>e(a).atomList);return s},t,n||H)}function X(t){const n=Symbol(),r=d(n),c=d((a,s)=>{const e=a(r);return e!==n?e:t(a,s)},(a,s,e)=>{if(e===k)s(r,n);else if(typeof e=="function"){const i=a(c);s(r,e(i))}else s(r,e)});return c}const z=t=>typeof(t==null?void 0:t.then)=="function";function Y(t){return n=>({...n,getItem:(r,c)=>{const a=e=>t(e)?e:c,s=n.getItem(r,c);return z(s)?s.then(a):a(s)}})}function D(t=()=>{try{return window.localStorage}catch(r){return}},n){var r;let c,a;const s={getItem:(u,o)=>{var l,f;const b=m=>{if(m=m||"",c!==m){try{a=JSON.parse(m,n==null?void 0:n.reviver)}catch(p){return o}c=m}return a},h=(f=(l=t())==null?void 0:l.getItem(u))!=null?f:null;return z(h)?h.then(b):b(h)},setItem:(u,o)=>{var l;return(l=t())==null?void 0:l.setItem(u,JSON.stringify(o,n==null?void 0:n.replacer))},removeItem:u=>{var o;return(o=t())==null?void 0:o.removeItem(u)}},e=u=>(o,l,f)=>u(o,b=>{let h;try{h=JSON.parse(b||"")}catch(m){h=f}l(h)});let i;try{i=(r=t())==null?void 0:r.subscribe}catch(u){}return!i&&typeof window!="undefined"&&typeof window.addEventListener=="function"&&window.Storage&&(i=(u,o)=>{if(!(t()instanceof window.Storage))return()=>{};const l=f=>{f.storageArea===t()&&f.key===u&&o(f.newValue)};return window.addEventListener("storage",l),()=>{window.removeEventListener("storage",l)}}),i&&(s.subscribe=e(i)),s}const Z=D();function tt(t,n,r=Z,c){const a=c==null?void 0:c.getOnInit,s=d(a?r.getItem(t,n):n);return s.onMount=e=>{e(r.getItem(t,n));let i;return r.subscribe&&(i=r.subscribe(t,e,n)),i},d(e=>e(s),(e,i,u)=>{const o=typeof u=="function"?u(e(s)):u;return o===k?(i(s,n),r.removeItem(t)):o instanceof Promise?o.then(l=>(i(s,l),r.setItem(t,l))):(i(s,o),r.setItem(t,o))})}function et(t,n){const r=a=>{if("e"in a)throw a.e;return a.d},c=d(a=>{var s;let e=t(a);const i=(s=e[Symbol.observable])==null?void 0:s.call(e);i&&(e=i);let u;const o=()=>new Promise(v=>{u=v}),l=n&&"initialValue"in n?{d:typeof n.initialValue=="function"?n.initialValue():n.initialValue}:o();let f,b;const h=v=>{b=v,u==null||u(v),f==null||f(v)};let m,p;const O=()=>!f,L=()=>{m&&(clearTimeout(p),m.unsubscribe()),m=e.subscribe({next:v=>h({d:v}),error:v=>h({e:v}),complete:()=>{}}),O()&&n!=null&&n.unstable_timeout&&(p=setTimeout(()=>{m&&(m.unsubscribe(),m=void 0)},n.unstable_timeout))};L();const g=d(b||l);return g.onMount=v=>(f=v,b&&v(b),m?clearTimeout(p):L(),()=>{f=void 0,m&&(m.unsubscribe(),m=void 0)}),[g,e,o,L,O]});return d(a=>{const[s]=a(c),e=a(s);return e instanceof Promise?e.then(r):r(e)},(a,s,e)=>{const[i,u,o,l,f]=a(c);if("next"in u)f()&&(s(i,o()),l()),u.next(e);else throw new Error("observable is not subject")})}const E=new WeakMap,nt=(t,n)=>(E.has(n)?E:E.set(n,t())).get(n),rt=t=>t instanceof Promise,N={state:"loading"};function ot(t){return nt(()=>{const n=new WeakMap,r=d(0),c=d((a,{setSelf:s})=>{a(r);let e;try{e=a(t)}catch(l){return{state:"hasError",error:l}}if(!rt(e))return{state:"hasData",data:e};const i=e,u=n.get(i);return u||(i.status==="fulfilled"?n.set(i,{state:"hasData",data:i.value}):i.status==="rejected"?n.set(i,{state:"hasError",error:i.reason}):i.then(l=>{n.set(i,{state:"hasData",data:l})},l=>{n.set(i,{state:"hasError",error:l})}).finally(s),n.get(i)||(n.set(i,N),N))},(a,s)=>{s(r,e=>e+1)});return d(a=>a(c))},t)}const T=(t,n,r)=>(n.has(r)?n:n.set(r,t())).get(r),st=new WeakMap,it=(t,n,r)=>{const c=T(()=>new WeakMap,st,n);return T(t,c,r)},at=t=>t instanceof Promise,ut=()=>{};function ct(t,n=ut){return it(()=>{const r=new WeakMap,c=new WeakMap,a=d(0),s=d((e,{setSelf:i})=>{e(a);const u=e(s),o=e(t);if(!at(o))return{v:o};if(o!==(u==null?void 0:u.p)&&(o.status==="fulfilled"?c.set(o,o.value):o.status==="rejected"?r.set(o,o.reason):o.then(l=>c.set(o,l),l=>r.set(o,l)).finally(i)),r.has(o))throw r.get(o);return c.has(o)?{p:o,v:c.get(o)}:u&&"v"in u?{p:o,f:n(u.v),v:u.v}:{p:o,f:n()}},(e,i)=>{i(a,u=>u+1)});return s.init=void 0,d(e=>{const i=e(s);return"f"in i?i.f:i.v},(e,i,...u)=>i(t,...u))},t,n)}function lt(t,n){const r=d(0);return d((c,a)=>(c(r),t(c,a)),(c,a,...s)=>{if(s.length===0)a(r,e=>e+1);else if(n)return n(c,a,...s)})}function ft(t){const n=d(void 0);return delete n.init,Object.defineProperty(n,"init",{get(){return t()}}),n}}}}); | ||
System.register(["jotai/vanilla"],function(x){"use strict";var m;return{setters:[function(W){m=W.atom}],execute:function(){x({atomFamily:$,atomWithDefault:X,atomWithLazy:ft,atomWithObservable:et,atomWithReducer:_,atomWithRefresh:lt,atomWithReset:J,atomWithStorage:tt,createJSONStorage:z,freezeAtom:j,freezeAtomCreator:q,loadable:ot,selectAtom:K,splitAtom:U,unstable_withStorageValidator:Y,unwrap:ct});const W=x("RESET",Symbol(""));function J(t){const e=m(t,(i,u,c)=>{const a=typeof c=="function"?c(i(e)):c;u(e,a===W?t:a)});return e}function _(t,e){return m(t,function(i,u,c){u(this,e(i(this),c))})}function $(t,e){let i=null;const u=new Map,c=new Set,a=r=>{let s;if(e===void 0)s=u.get(r);else for(const[l,f]of u)if(e(l,r)){s=f;break}if(s!==void 0)if(i!=null&&i(s[1],r))a.remove(r);else return s[0];const o=t(r);return u.set(r,[o,Date.now()]),n("CREATE",r,o),o};function n(r,s,o){for(const l of c)l({type:r,param:s,atom:o})}return a.unstable_listen=r=>(c.add(r),()=>{c.delete(r)}),a.getParams=()=>u.keys(),a.remove=r=>{if(e===void 0){if(!u.has(r))return;const[s]=u.get(r);u.delete(r),n("REMOVE",r,s)}else for(const[s,[o]]of u)if(e(s,r)){u.delete(s),n("REMOVE",s,o);break}},a.setShouldRemove=r=>{if(i=r,!!i)for(const[s,[o,l]]of u)i(l,s)&&(u.delete(s),n("REMOVE",s,o))},a}const M=(t,e,i)=>(e.has(i)?e:e.set(i,t())).get(i),C=new WeakMap,F=(t,e,i,u)=>{const c=M(()=>new WeakMap,C,e),a=M(()=>new WeakMap,c,i);return M(t,a,u)};function K(t,e,i=Object.is){return F(()=>{const u=Symbol(),c=([n,r])=>{if(r===u)return e(n);const s=e(n,r);return i(r,s)?r:s},a=m(n=>{const r=n(a),s=n(t);return c([s,r])});return a.init=u,a},t,e,i)}const R=new WeakSet,L=t=>{if(typeof t!="object"||t===null)return;Object.freeze(t);const e=Object.getOwnPropertyNames(t);for(const i of e){const u=t[i];L(u)}return t};function j(t){if(R.has(t))return t;R.add(t);const e=t.read;if(t.read=function(i,u){return L(e.call(this,i,u))},"write"in t){const i=t.write;t.write=function(u,c,...a){return i.call(this,u,(...n)=>(n[0]===t&&(n[1]=L(n[1])),c(...n)),...a)}}return t}function q(t){return(...e)=>j(t(...e))}const A=(t,e,i)=>(e.has(i)?e:e.set(i,t())).get(i),B=new WeakMap,G=(t,e,i)=>{const u=A(()=>new WeakMap,B,e);return A(t,u,i)},H={},P=t=>!!t.write,Q=t=>typeof t=="function";function U(t,e){return G(()=>{const i=new WeakMap,u=(n,r)=>{let s=i.get(n);if(s)return s;const o=r&&i.get(r),l=[],f=[];return n.forEach((b,h)=>{const d=e?e(b):h;f[h]=d;const p=o&&o.atomList[o.keyList.indexOf(d)];if(p){l[h]=p;return}const O=g=>{const v=g(c),y=g(t),S=u(y,v==null?void 0:v.arr).keyList.indexOf(d);if(S<0||S>=y.length){const w=n[u(n).keyList.indexOf(d)];if(w)return w;throw new Error("splitAtom: index out of bounds for read")}return y[S]},E=(g,v,y)=>{const S=g(c),w=g(t),k=u(w,S==null?void 0:S.arr).keyList.indexOf(d);if(k<0||k>=w.length)throw new Error("splitAtom: index out of bounds for write");const N=Q(y)?y(w[k]):y;Object.is(w[k],N)||v(t,[...w.slice(0,k),N,...w.slice(k+1)])};l[h]=P(t)?m(O,E):m(O)}),o&&o.keyList.length===f.length&&o.keyList.every((b,h)=>b===f[h])?s=o:s={arr:n,atomList:l,keyList:f},i.set(n,s),s},c=m(n=>{const r=n(c),s=n(t);return u(s,r==null?void 0:r.arr)});c.init=void 0;const a=P(t)?m(n=>n(c).atomList,(n,r,s)=>{switch(s.type){case"remove":{const o=n(a).indexOf(s.atom);if(o>=0){const l=n(t);r(t,[...l.slice(0,o),...l.slice(o+1)])}break}case"insert":{const o=s.before?n(a).indexOf(s.before):n(a).length;if(o>=0){const l=n(t);r(t,[...l.slice(0,o),s.value,...l.slice(o)])}break}case"move":{const o=n(a).indexOf(s.atom),l=s.before?n(a).indexOf(s.before):n(a).length;if(o>=0&&l>=0){const f=n(t);o<l?r(t,[...f.slice(0,o),...f.slice(o+1,l),f[o],...f.slice(l)]):r(t,[...f.slice(0,l),f[o],...f.slice(l,o),...f.slice(o+1)])}break}}}):m(n=>n(c).atomList);return a},t,e||H)}function X(t){const e=Symbol(),i=m(e),u=m((c,a)=>{const n=c(i);return n!==e?n:t(c,a)},(c,a,n)=>{if(n===W)a(i,e);else if(typeof n=="function"){const r=c(u);a(i,n(r))}else a(i,n)});return u}const V=t=>typeof(t==null?void 0:t.then)=="function";function Y(t){return e=>({...e,getItem:(i,u)=>{const c=n=>t(n)?n:u,a=e.getItem(i,u);return V(a)?a.then(c):c(a)}})}function z(t=()=>{try{return window.localStorage}catch(i){return}},e){var i;let u,c;const a={getItem:(s,o)=>{var l,f;const b=d=>{if(d=d||"",u!==d){try{c=JSON.parse(d,e==null?void 0:e.reviver)}catch(p){return o}u=d}return c},h=(f=(l=t())==null?void 0:l.getItem(s))!=null?f:null;return V(h)?h.then(b):b(h)},setItem:(s,o)=>{var l;return(l=t())==null?void 0:l.setItem(s,JSON.stringify(o,e==null?void 0:e.replacer))},removeItem:s=>{var o;return(o=t())==null?void 0:o.removeItem(s)}},n=s=>(o,l,f)=>s(o,b=>{let h;try{h=JSON.parse(b||"")}catch(d){h=f}l(h)});let r;try{r=(i=t())==null?void 0:i.subscribe}catch(s){}return!r&&typeof window!="undefined"&&typeof window.addEventListener=="function"&&window.Storage&&(r=(s,o)=>{if(!(t()instanceof window.Storage))return()=>{};const l=f=>{f.storageArea===t()&&f.key===s&&o(f.newValue)};return window.addEventListener("storage",l),()=>{window.removeEventListener("storage",l)}}),r&&(a.subscribe=n(r)),a}const Z=z();function tt(t,e,i=Z,u){const c=u==null?void 0:u.getOnInit,a=m(c?i.getItem(t,e):e);return a.onMount=n=>{n(i.getItem(t,e));let r;return i.subscribe&&(r=i.subscribe(t,n,e)),r},m(n=>n(a),(n,r,s)=>{const o=typeof s=="function"?s(n(a)):s;return o===W?(r(a,e),i.removeItem(t)):o instanceof Promise?o.then(l=>(r(a,l),i.setItem(t,l))):(r(a,o),i.setItem(t,o))})}function et(t,e){const i=c=>{if("e"in c)throw c.e;return c.d},u=m(c=>{var a;let n=t(c);const r=(a=n[Symbol.observable])==null?void 0:a.call(n);r&&(n=r);let s;const o=()=>new Promise(v=>{s=v}),l=e&&"initialValue"in e?{d:typeof e.initialValue=="function"?e.initialValue():e.initialValue}:o();let f,b;const h=v=>{b=v,s==null||s(v),f==null||f(v)};let d,p;const O=()=>!f,E=()=>{d&&(clearTimeout(p),d.unsubscribe()),d=n.subscribe({next:v=>h({d:v}),error:v=>h({e:v}),complete:()=>{}}),O()&&e!=null&&e.unstable_timeout&&(p=setTimeout(()=>{d&&(d.unsubscribe(),d=void 0)},e.unstable_timeout))};E();const g=m(b||l);return g.onMount=v=>(f=v,b&&v(b),d?clearTimeout(p):E(),()=>{f=void 0,d&&(d.unsubscribe(),d=void 0)}),[g,n,o,E,O]});return m(c=>{const[a]=c(u),n=c(a);return n instanceof Promise?n.then(i):i(n)},(c,a,n)=>{const[r,s,o,l,f]=c(u);if("next"in s)f()&&(a(r,o()),l()),s.next(n);else throw new Error("observable is not subject")})}const I=new WeakMap,nt=(t,e)=>(I.has(e)?I:I.set(e,t())).get(e),rt=t=>t instanceof Promise,D={state:"loading"};function ot(t){return nt(()=>{const e=new WeakMap,i=m(0),u=m((c,{setSelf:a})=>{c(i);let n;try{n=c(t)}catch(l){return{state:"hasError",error:l}}if(!rt(n))return{state:"hasData",data:n};const r=n,s=e.get(r);return s||(r.status==="fulfilled"?e.set(r,{state:"hasData",data:r.value}):r.status==="rejected"?e.set(r,{state:"hasError",error:r.reason}):r.then(l=>{e.set(r,{state:"hasData",data:l})},l=>{e.set(r,{state:"hasError",error:l})}).finally(a),e.get(r)||(e.set(r,D),D))},(c,a)=>{a(i,n=>n+1)});return m(c=>c(u))},t)}const T=(t,e,i)=>(e.has(i)?e:e.set(i,t())).get(i),st=new WeakMap,it=(t,e,i)=>{const u=T(()=>new WeakMap,st,e);return T(t,u,i)},at=t=>t instanceof Promise,ut=()=>{};function ct(t,e=ut){return it(()=>{const i=new WeakMap,u=new WeakMap,c=m(0),a=m((n,{setSelf:r})=>{n(c);const s=n(a),o=n(t);if(!at(o))return{v:o};if(o!==(s==null?void 0:s.p)&&(o.status==="fulfilled"?u.set(o,o.value):o.status==="rejected"?i.set(o,o.reason):o.then(l=>u.set(o,l),l=>i.set(o,l)).finally(r)),i.has(o))throw i.get(o);return u.has(o)?{p:o,v:u.get(o)}:s&&"v"in s?{p:o,f:e(s.v),v:s.v}:{p:o,f:e()}},(n,r)=>{r(c,s=>s+1)});return a.init=void 0,m(n=>{const r=n(a);return"f"in r?r.f:r.v},(n,r,...s)=>r(t,...s))},t,e)}function lt(t,e){const i=m(0);return m((u,c)=>(u(i),t(u,c)),(u,c,...a)=>{if(a.length===0)c(i,n=>n+1);else if(e)return e(u,c,...a)})}function ft(t){const e=m(void 0);return delete e.init,Object.defineProperty(e,"init",{get(){return t()}}),e}}}}); |
import type { Atom } from 'jotai/vanilla'; | ||
type ShouldRemove<Param> = (createdAt: number, param: Param) => boolean; | ||
/** | ||
* in milliseconds | ||
*/ | ||
type CreatedAt = number; | ||
type ShouldRemove<Param> = (createdAt: CreatedAt, param: Param) => boolean; | ||
type Cleanup = () => void; | ||
type Callback<Param, AtomType> = (event: { | ||
type: 'CREATE' | 'REMOVE'; | ||
param: Param; | ||
atom: AtomType; | ||
}) => void; | ||
export interface AtomFamily<Param, AtomType> { | ||
(param: Param): AtomType; | ||
getParams(): Iterable<Param>; | ||
remove(param: Param): void; | ||
setShouldRemove(shouldRemove: ShouldRemove<Param> | null): void; | ||
/** | ||
* fires when a atom is created or removed | ||
* This API is for advanced use cases, and can change without notice. | ||
*/ | ||
unstable_listen(callback: Callback<Param, AtomType>): Cleanup; | ||
} | ||
@@ -8,0 +24,0 @@ export declare function atomFamily<Param, AtomType extends Atom<unknown>>(initializeAtom: (param: Param) => AtomType, areEqual?: (a: Param, b: Param) => boolean): AtomFamily<Param, AtomType>; |
import type { Atom } from 'jotai/vanilla'; | ||
type ShouldRemove<Param> = (createdAt: number, param: Param) => boolean; | ||
/** | ||
* in milliseconds | ||
*/ | ||
type CreatedAt = number; | ||
type ShouldRemove<Param> = (createdAt: CreatedAt, param: Param) => boolean; | ||
type Cleanup = () => void; | ||
type Callback<Param, AtomType> = (event: { | ||
type: 'CREATE' | 'REMOVE'; | ||
param: Param; | ||
atom: AtomType; | ||
}) => void; | ||
export interface AtomFamily<Param, AtomType> { | ||
(param: Param): AtomType; | ||
getParams(): Iterable<Param>; | ||
remove(param: Param): void; | ||
setShouldRemove(shouldRemove: ShouldRemove<Param> | null): void; | ||
/** | ||
* fires when a atom is created or removed | ||
* This API is for advanced use cases, and can change without notice. | ||
*/ | ||
unstable_listen(callback: Callback<Param, AtomType>): Cleanup; | ||
} | ||
@@ -8,0 +24,0 @@ export declare function atomFamily<Param, AtomType extends Atom<unknown>>(initializeAtom: (param: Param) => AtomType, areEqual?: (a: Param, b: Param) => boolean): AtomFamily<Param, AtomType>; |
@@ -267,3 +267,3 @@ (function (global, factory) { | ||
}; | ||
var readAtomState = function readAtomState(pending, atom, force) { | ||
var _readAtomState = function readAtomState(pending, atom, force) { | ||
var atomState = getAtomState(atom); | ||
@@ -277,3 +277,3 @@ if (!(force != null && force(atom)) && isAtomStateInitialized(atomState)) { | ||
n = _ref[1]; | ||
return readAtomState(pending, a, force).n === n; | ||
return _readAtomState(pending, a, force).n === n; | ||
})) { | ||
@@ -297,3 +297,3 @@ return atomState; | ||
} | ||
var aState = readAtomState(pending, a, force); | ||
var aState = _readAtomState(pending, a, force); | ||
if (isSync) { | ||
@@ -361,3 +361,3 @@ addDependency(pending, atom, a, aState); | ||
var readAtom = function readAtom(atom) { | ||
return returnAtomValue(readAtomState(undefined, atom)); | ||
return returnAtomValue(_readAtomState(undefined, atom)); | ||
}; | ||
@@ -380,3 +380,3 @@ var recomputeDependents = function recomputeDependents(pending, atom) { | ||
var markedAtoms = new Set(); | ||
var visit = function visit(n) { | ||
var _visit = function visit(n) { | ||
if (markedAtoms.has(n)) { | ||
@@ -389,3 +389,3 @@ return; | ||
if (n !== m) { | ||
visit(m); | ||
_visit(m); | ||
} | ||
@@ -395,3 +395,3 @@ } | ||
}; | ||
visit(atom); | ||
_visit(atom); | ||
var changedAtoms = new Set([atom]); | ||
@@ -414,3 +414,3 @@ var isMarked = function isMarked(a) { | ||
if (hasChangedDeps) { | ||
readAtomState(pending, a, isMarked); | ||
_readAtomState(pending, a, isMarked); | ||
mountDependencies(pending, a, aState); | ||
@@ -425,5 +425,5 @@ if (prevEpochNumber !== aState.n) { | ||
}; | ||
var writeAtomState = function writeAtomState(pending, atom) { | ||
var _writeAtomState = function writeAtomState(pending, atom) { | ||
var getter = function getter(a) { | ||
return returnAtomValue(readAtomState(pending, a)); | ||
return returnAtomValue(_readAtomState(pending, a)); | ||
}; | ||
@@ -450,3 +450,3 @@ var setter = function setter(a) { | ||
} else { | ||
r = writeAtomState.apply(void 0, [pending, a].concat(args)); | ||
r = _writeAtomState.apply(void 0, [pending, a].concat(args)); | ||
} | ||
@@ -467,3 +467,3 @@ flushPending(pending); | ||
} | ||
var result = writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
var result = _writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
flushPending(pending); | ||
@@ -477,3 +477,3 @@ return result; | ||
if (!atomState.m.d.has(a)) { | ||
var aMounted = mountAtom(pending, a); | ||
var aMounted = _mountAtom(pending, a); | ||
aMounted.t.add(atom); | ||
@@ -487,3 +487,3 @@ atomState.m.d.add(a); | ||
atomState.m.d.delete(_a); | ||
var _aMounted = unmountAtom(pending, _a); | ||
var _aMounted = _unmountAtom(pending, _a); | ||
_aMounted == null || _aMounted.t.delete(atom); | ||
@@ -494,9 +494,9 @@ } | ||
}; | ||
var mountAtom = function mountAtom(pending, atom) { | ||
var _mountAtom = function mountAtom(pending, atom) { | ||
var atomState = getAtomState(atom); | ||
if (!atomState.m) { | ||
readAtomState(pending, atom); | ||
_readAtomState(pending, atom); | ||
for (var _iterator7 = _createForOfIteratorHelperLoose(atomState.d.keys()), _step7; !(_step7 = _iterator7()).done;) { | ||
var a = _step7.value; | ||
var aMounted = mountAtom(pending, a); | ||
var aMounted = _mountAtom(pending, a); | ||
aMounted.t.add(atom); | ||
@@ -520,3 +520,3 @@ } | ||
} | ||
return writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
return _writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
}); | ||
@@ -531,3 +531,3 @@ if (onUnmount) { | ||
}; | ||
var unmountAtom = function unmountAtom(pending, atom) { | ||
var _unmountAtom = function unmountAtom(pending, atom) { | ||
var atomState = getAtomState(atom); | ||
@@ -548,3 +548,3 @@ if (atomState.m && !atomState.m.l.size && !Array.from(atomState.m.t).some(function (a) { | ||
var a = _step8.value; | ||
var aMounted = unmountAtom(pending, a); | ||
var aMounted = _unmountAtom(pending, a); | ||
aMounted == null || aMounted.t.delete(atom); | ||
@@ -562,3 +562,3 @@ } | ||
var pending = createPending(); | ||
var mounted = mountAtom(pending, atom); | ||
var mounted = _mountAtom(pending, atom); | ||
flushPending(pending); | ||
@@ -570,3 +570,3 @@ var listeners = mounted.l; | ||
var pending = createPending(); | ||
unmountAtom(pending, atom); | ||
_unmountAtom(pending, atom); | ||
flushPending(pending); | ||
@@ -573,0 +573,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).jotaiVanilla={})}(this,(function(n){"use strict";var e=0;function t(n){return n(this)}function r(n,e,t){return e(this,"function"==typeof t?t(n(this)):t)}function o(n,e){(null==e||e>n.length)&&(e=n.length);for(var t=0,r=Array(e);t<e;t++)r[t]=n[t];return r}function a(n,e){var t="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(t)return(t=t.call(n)).next.bind(t);if(Array.isArray(n)||(t=function(n,e){if(n){if("string"==typeof n)return o(n,e);var t={}.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(n,e):void 0}}(n))||e){t&&(n=t);var r=0;return function(){return r>=n.length?{done:!0}:{done:!1,value:n[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,u=function(n,e){return n.unstable_is?n.unstable_is(e):e===n},f=function(n){return"init"in n},l=function(n){return!!n.write},c=Symbol(""),v="pending",d=new WeakMap,s=function(n){return"v"in n||"e"in n},m=function(n){if("e"in n)throw n.e;return n.v},y=function(n){var e,t=n.v;return"object"==typeof(e=t)&&null!==e&&c in e&&t.status===v?t:null},h=function(n,e,t){t.p.has(n)||(t.p.add(n),e.then((function(){t.p.delete(n)}),(function(){t.p.delete(n)})))},p=function(){return[new Map,new Map,new Set]},w=function(n,e,t){n[0].has(e)||n[0].set(e,new Set),n[1].set(e,t)},g=function(n,e){n[2].add(e)},b=function(n){for(;n[1].size||n[2].size;){n[0].clear();var e=new Set(n[1].values());n[1].clear();var t=new Set(n[2]);n[2].clear(),e.forEach((function(n){var e;return null==(e=n.m)?void 0:e.l.forEach((function(n){return n()}))})),t.forEach((function(n){return n()}))}},S=function(){var n=new WeakMap,e=function(e){var t=n.get(e);return t||(t={d:new Map,p:new Set,n:0},n.set(e,t)),t},t=function(n,t,r,o,i){void 0===o&&(o=function(){}),void 0===i&&(i=function(){});var u,f="v"in t,l=t.v,s=y(t);if("function"==typeof(null==(u=r)?void 0:u.then))if(s)s!==r&&(s[c](r,o),++t.n);else{var m=function(n,e,t){if(!d.has(n)){var r,o=new Promise((function(a,i){var u=n,f=function(n){return function(e){u===n&&(o.status="fulfilled",o.value=e,a(e),t())}},l=function(n){return function(e){u===n&&(o.status="rejected",o.reason=e,i(e),t())}};n.then(f(n),l(n)),r=function(n,t){n&&(d.set(n,o),u=n,n.then(f(n),l(n)),e(),e=t)}}));o.status=v,o[c]=r,d.set(n,o)}return d.get(n)}(r,o,i);if(m.status===v)for(var p,w=a(t.d.keys());!(p=w()).done;){var g=p.value,b=e(g);h(n,m,b)}t.v=m,delete t.e}else s&&s[c](Promise.resolve(r),o),t.v=r,delete t.e;f&&Object.is(l,t.v)||++t.n},r=function(n,t,r,o){var a,i=e(t);i.d.set(r,o.n);var u=y(i);u&&h(t,u,o),null==(a=o.m)||a.t.add(t),n&&function(n,e,t){var r=n[0].get(e);r&&r.add(t)}(n,r,t)},o=function n(o,a,i){var c=e(a);if((null==i||!i(a))&&s(c)){if(c.m)return c;if(Array.from(c.d).every((function(e){var t=e[0],r=e[1];return n(o,t,i).n===r})))return c}c.d.clear();var v,d,y=!0,h={get signal(){return v||(v=new AbortController),v.signal},get setSelf(){return!d&&l(a)&&(d=function(){if(!y){for(var n=arguments.length,e=new Array(n),t=0;t<n;t++)e[t]=arguments[t];return A.apply(void 0,[a].concat(e))}}),d}};try{var w=a.read((function(l){if(u(a,l)){var v=e(l);if(!s(v)){if(!f(l))throw new Error("no atom init");t(l,v,l.init)}return m(v)}var d=n(o,l,i);if(y)r(o,a,l,d);else{var h=p();r(h,a,l,d),k(h,a,c),b(h)}return m(d)}),h);return t(a,c,w,(function(){var n;return null==(n=v)?void 0:n.abort()}),(function(){if(c.m){var n=p();k(n,a,c),b(n)}})),c}catch(n){return delete c.v,c.e=n,++c.n,c}finally{y=!1}},i=function(n,t){var r=[],i=new Set;!function t(o){if(!i.has(o)){i.add(o);for(var u,f=a(function(t){for(var r,o,i,u=e(t),f=new Set(null==(r=u.m)?void 0:r.t),l=a(u.p);!(i=l()).done;){var c=i.value;f.add(c)}return null==(o=function(n,e){return n[0].get(e)}(n,t))||o.forEach((function(n){f.add(n)})),f}(o));!(u=f()).done;){var l=u.value;o!==l&&t(l)}r.push(o)}}(t);for(var u=new Set([t]),f=function(n){return i.has(n)},l=r.length-1;l>=0;--l){for(var c,v=r[l],d=e(v),s=d.n,m=!1,y=a(d.d.keys());!(c=y()).done;){var h=c.value;if(h!==v&&u.has(h)){m=!0;break}}m&&(o(n,v,f),k(n,v,d),s!==d.n&&(w(n,v,d),u.add(v))),i.delete(v)}},S=function n(r,a){for(var l=arguments.length,c=new Array(l>2?l-2:0),v=2;v<l;v++)c[v-2]=arguments[v];var d=a.write.apply(a,[function(n){return m(o(r,n))},function(o){for(var l,c=arguments.length,v=new Array(c>1?c-1:0),d=1;d<c;d++)v[d-1]=arguments[d];if(u(a,o)){if(!f(o))throw new Error("atom not writable");var s=e(o),m="v"in s,y=s.v,h=v[0];t(o,s,h),k(r,o,s),m&&Object.is(y,s.v)||(w(r,o,s),i(r,o))}else l=n.apply(void 0,[r,o].concat(v));return b(r),l}].concat(c));return d},A=function(n){for(var e=p(),t=arguments.length,r=new Array(t>1?t-1:0),o=1;o<t;o++)r[o-1]=arguments[o];var a=S.apply(void 0,[e,n].concat(r));return b(e),a},k=function(n,e,t){if(t.m&&!y(t)){for(var r,o=a(t.d.keys());!(r=o()).done;){var i=r.value;if(!t.m.d.has(i))j(n,i).t.add(e),t.m.d.add(i)}for(var u,f=a(t.m.d||[]);!(u=f()).done;){var l=u.value;if(!t.d.has(l)){t.m.d.delete(l);var c=M(n,l);null==c||c.t.delete(e)}}}},j=function n(t,r){var i=e(r);if(!i.m){o(t,r);for(var u,f=a(i.d.keys());!(u=f()).done;){var c=u.value;n(t,c).t.add(r)}if(i.m={l:new Set,d:new Set(i.d.keys()),t:new Set},l(r)&&r.onMount){var v=i.m,d=r.onMount;g(t,(function(){var n=d((function(){for(var n=arguments.length,e=new Array(n),o=0;o<n;o++)e[o]=arguments[o];return S.apply(void 0,[t,r].concat(e))}));n&&(v.u=n)}))}}return i.m},M=function n(t,r){var o=e(r);if(!o.m||o.m.l.size||Array.from(o.m.t).some((function(n){var t;return null==(t=e(n).m)?void 0:t.d.has(r)})))return o.m;var i=o.m.u;i&&g(t,i),delete o.m;for(var u,f=a(o.d.keys());!(u=f()).done;){var l=n(t,u.value);null==l||l.t.delete(r)}var v=y(o);v&&v[c](void 0,(function(){}))},E={get:function(n){return m(o(void 0,n))},set:A,sub:function(n,e){var t=p(),r=j(t,n);b(t);var o=r.l;return o.add(e),function(){o.delete(e);var t=p();M(t,n),b(t)}}};return E};n.atom=function(n,o){var a="atom"+ ++e,i={toString:function(){return a}};return"function"==typeof n?i.read=n:(i.init=n,i.read=t,i.write=r),o&&(i.write=o),i},n.createStore=S,n.getDefaultStore=function(){return i||(i=S()),i}})); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).jotaiVanilla={})}(this,(function(n){"use strict";var e=0;function t(n){return n(this)}function r(n,e,t){return e(this,"function"==typeof t?t(n(this)):t)}function o(n,e){(null==e||e>n.length)&&(e=n.length);for(var t=0,r=Array(e);t<e;t++)r[t]=n[t];return r}function a(n,e){var t="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(t)return(t=t.call(n)).next.bind(t);if(Array.isArray(n)||(t=function(n,e){if(n){if("string"==typeof n)return o(n,e);var t={}.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(n,e):void 0}}(n))||e){t&&(n=t);var r=0;return function(){return r>=n.length?{done:!0}:{done:!1,value:n[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,u=function(n,e){return n.unstable_is?n.unstable_is(e):e===n},f=function(n){return"init"in n},l=function(n){return!!n.write},c=Symbol(""),v="pending",d=new WeakMap,s=function(n){return"v"in n||"e"in n},m=function(n){if("e"in n)throw n.e;return n.v},y=function(n){var e,t=n.v;return"object"==typeof(e=t)&&null!==e&&c in e&&t.status===v?t:null},h=function(n,e,t){t.p.has(n)||(t.p.add(n),e.then((function(){t.p.delete(n)}),(function(){t.p.delete(n)})))},p=function(){return[new Map,new Map,new Set]},w=function(n,e,t){n[0].has(e)||n[0].set(e,new Set),n[1].set(e,t)},g=function(n,e){n[2].add(e)},b=function(n){for(;n[1].size||n[2].size;){n[0].clear();var e=new Set(n[1].values());n[1].clear();var t=new Set(n[2]);n[2].clear(),e.forEach((function(n){var e;return null==(e=n.m)?void 0:e.l.forEach((function(n){return n()}))})),t.forEach((function(n){return n()}))}},S=function(){var n=new WeakMap,e=function(e){var t=n.get(e);return t||(t={d:new Map,p:new Set,n:0},n.set(e,t)),t},t=function(n,t,r,o,i){void 0===o&&(o=function(){}),void 0===i&&(i=function(){});var u,f="v"in t,l=t.v,s=y(t);if("function"==typeof(null==(u=r)?void 0:u.then))if(s)s!==r&&(s[c](r,o),++t.n);else{var m=function(n,e,t){if(!d.has(n)){var r,o=new Promise((function(a,i){var u=n,f=function(n){return function(e){u===n&&(o.status="fulfilled",o.value=e,a(e),t())}},l=function(n){return function(e){u===n&&(o.status="rejected",o.reason=e,i(e),t())}};n.then(f(n),l(n)),r=function(n,t){n&&(d.set(n,o),u=n,n.then(f(n),l(n)),e(),e=t)}}));o.status=v,o[c]=r,d.set(n,o)}return d.get(n)}(r,o,i);if(m.status===v)for(var p,w=a(t.d.keys());!(p=w()).done;){var g=p.value,b=e(g);h(n,m,b)}t.v=m,delete t.e}else s&&s[c](Promise.resolve(r),o),t.v=r,delete t.e;f&&Object.is(l,t.v)||++t.n},r=function(n,t,r,o){var a,i=e(t);i.d.set(r,o.n);var u=y(i);u&&h(t,u,o),null==(a=o.m)||a.t.add(t),n&&function(n,e,t){var r=n[0].get(e);r&&r.add(t)}(n,r,t)},o=function(n,a,i){var c=e(a);if((null==i||!i(a))&&s(c)){if(c.m)return c;if(Array.from(c.d).every((function(e){var t=e[0],r=e[1];return o(n,t,i).n===r})))return c}c.d.clear();var v,d,y=!0,h={get signal(){return v||(v=new AbortController),v.signal},get setSelf(){return!d&&l(a)&&(d=function(){if(!y){for(var n=arguments.length,e=new Array(n),t=0;t<n;t++)e[t]=arguments[t];return A.apply(void 0,[a].concat(e))}}),d}};try{var w=a.read((function(l){if(u(a,l)){var v=e(l);if(!s(v)){if(!f(l))throw new Error("no atom init");t(l,v,l.init)}return m(v)}var d=o(n,l,i);if(y)r(n,a,l,d);else{var h=p();r(h,a,l,d),k(h,a,c),b(h)}return m(d)}),h);return t(a,c,w,(function(){var n;return null==(n=v)?void 0:n.abort()}),(function(){if(c.m){var n=p();k(n,a,c),b(n)}})),c}catch(n){return delete c.v,c.e=n,++c.n,c}finally{y=!1}},i=function(n,t){var r=[],i=new Set,u=function(t){if(!i.has(t)){i.add(t);for(var o,f=a(function(t){for(var r,o,i,u=e(t),f=new Set(null==(r=u.m)?void 0:r.t),l=a(u.p);!(i=l()).done;){var c=i.value;f.add(c)}return null==(o=function(n,e){return n[0].get(e)}(n,t))||o.forEach((function(n){f.add(n)})),f}(t));!(o=f()).done;){var l=o.value;t!==l&&u(l)}r.push(t)}};u(t);for(var f=new Set([t]),l=function(n){return i.has(n)},c=r.length-1;c>=0;--c){for(var v,d=r[c],s=e(d),m=s.n,y=!1,h=a(s.d.keys());!(v=h()).done;){var p=v.value;if(p!==d&&f.has(p)){y=!0;break}}y&&(o(n,d,l),k(n,d,s),m!==s.n&&(w(n,d,s),f.add(d))),i.delete(d)}},S=function(n,r){for(var a=arguments.length,l=new Array(a>2?a-2:0),c=2;c<a;c++)l[c-2]=arguments[c];var v=r.write.apply(r,[function(e){return m(o(n,e))},function(o){for(var a,l=arguments.length,c=new Array(l>1?l-1:0),v=1;v<l;v++)c[v-1]=arguments[v];if(u(r,o)){if(!f(o))throw new Error("atom not writable");var d=e(o),s="v"in d,m=d.v,y=c[0];t(o,d,y),k(n,o,d),s&&Object.is(m,d.v)||(w(n,o,d),i(n,o))}else a=S.apply(void 0,[n,o].concat(c));return b(n),a}].concat(l));return v},A=function(n){for(var e=p(),t=arguments.length,r=new Array(t>1?t-1:0),o=1;o<t;o++)r[o-1]=arguments[o];var a=S.apply(void 0,[e,n].concat(r));return b(e),a},k=function(n,e,t){if(t.m&&!y(t)){for(var r,o=a(t.d.keys());!(r=o()).done;){var i=r.value;if(!t.m.d.has(i))j(n,i).t.add(e),t.m.d.add(i)}for(var u,f=a(t.m.d||[]);!(u=f()).done;){var l=u.value;if(!t.d.has(l)){t.m.d.delete(l);var c=M(n,l);null==c||c.t.delete(e)}}}},j=function(n,t){var r=e(t);if(!r.m){o(n,t);for(var i,u=a(r.d.keys());!(i=u()).done;){var f=i.value;j(n,f).t.add(t)}if(r.m={l:new Set,d:new Set(r.d.keys()),t:new Set},l(t)&&t.onMount){var c=r.m,v=t.onMount;g(n,(function(){var e=v((function(){for(var e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return S.apply(void 0,[n,t].concat(r))}));e&&(c.u=e)}))}}return r.m},M=function(n,t){var r=e(t);if(!r.m||r.m.l.size||Array.from(r.m.t).some((function(n){var r;return null==(r=e(n).m)?void 0:r.d.has(t)})))return r.m;var o=r.m.u;o&&g(n,o),delete r.m;for(var i,u=a(r.d.keys());!(i=u()).done;){var f=i.value,l=M(n,f);null==l||l.t.delete(t)}var v=y(r);v&&v[c](void 0,(function(){}))},E={get:function(n){return m(o(void 0,n))},set:A,sub:function(n,e){var t=p(),r=j(t,n);b(t);var o=r.l;return o.add(e),function(){o.delete(e);var t=p();M(t,n),b(t)}}};return E};n.atom=function(n,o){var a="atom"+ ++e,i={toString:function(){return a}};return"function"==typeof n?i.read=n:(i.init=n,i.read=t,i.write=r),o&&(i.write=o),i},n.createStore=S,n.getDefaultStore=function(){return i||(i=S()),i}})); |
@@ -65,3 +65,4 @@ (function (global, factory) { | ||
var atoms = new Map(); | ||
var createAtom = function createAtom(param) { | ||
var listeners = new Set(); | ||
var _createAtom = function createAtom(param) { | ||
var item; | ||
@@ -83,3 +84,3 @@ if (areEqual === undefined) { | ||
if (shouldRemove != null && shouldRemove(item[1], param)) { | ||
createAtom.remove(param); | ||
_createAtom.remove(param); | ||
} else { | ||
@@ -91,13 +92,40 @@ return item[0]; | ||
atoms.set(param, [newAtom, Date.now()]); | ||
notifyListeners('CREATE', param, newAtom); | ||
return newAtom; | ||
}; | ||
createAtom.remove = function (param) { | ||
function notifyListeners(type, param, atom) { | ||
for (var _iterator2 = _createForOfIteratorHelperLoose(listeners), _step2; !(_step2 = _iterator2()).done;) { | ||
var listener = _step2.value; | ||
listener({ | ||
type: type, | ||
param: param, | ||
atom: atom | ||
}); | ||
} | ||
} | ||
_createAtom.unstable_listen = function (callback) { | ||
listeners.add(callback); | ||
return function () { | ||
listeners.delete(callback); | ||
}; | ||
}; | ||
_createAtom.getParams = function () { | ||
return atoms.keys(); | ||
}; | ||
_createAtom.remove = function (param) { | ||
if (areEqual === undefined) { | ||
if (!atoms.has(param)) return; | ||
var _ref = atoms.get(param), | ||
atom = _ref[0]; | ||
atoms.delete(param); | ||
notifyListeners('REMOVE', param, atom); | ||
} else { | ||
for (var _iterator2 = _createForOfIteratorHelperLoose(atoms), _step2; !(_step2 = _iterator2()).done;) { | ||
var _step2$value = _step2.value, | ||
key = _step2$value[0]; | ||
for (var _iterator3 = _createForOfIteratorHelperLoose(atoms), _step3; !(_step3 = _iterator3()).done;) { | ||
var _step3$value = _step3.value, | ||
key = _step3$value[0], | ||
_step3$value$ = _step3$value[1], | ||
_atom = _step3$value$[0]; | ||
if (areEqual(key, param)) { | ||
atoms.delete(key); | ||
notifyListeners('REMOVE', key, _atom); | ||
break; | ||
@@ -108,15 +136,18 @@ } | ||
}; | ||
createAtom.setShouldRemove = function (fn) { | ||
_createAtom.setShouldRemove = function (fn) { | ||
shouldRemove = fn; | ||
if (!shouldRemove) return; | ||
for (var _iterator3 = _createForOfIteratorHelperLoose(atoms), _step3; !(_step3 = _iterator3()).done;) { | ||
var _step3$value = _step3.value, | ||
key = _step3$value[0], | ||
value = _step3$value[1]; | ||
if (shouldRemove(value[1], key)) { | ||
for (var _iterator4 = _createForOfIteratorHelperLoose(atoms), _step4; !(_step4 = _iterator4()).done;) { | ||
var _step4$value = _step4.value, | ||
key = _step4$value[0], | ||
_step4$value$ = _step4$value[1], | ||
atom = _step4$value$[0], | ||
_createdAt = _step4$value$[1]; | ||
if (shouldRemove(_createdAt, key)) { | ||
atoms.delete(key); | ||
notifyListeners('REMOVE', key, atom); | ||
} | ||
} | ||
}; | ||
return createAtom; | ||
return _createAtom; | ||
} | ||
@@ -163,3 +194,3 @@ | ||
var frozenAtoms = new WeakSet(); | ||
var deepFreeze = function deepFreeze(obj) { | ||
var _deepFreeze = function deepFreeze(obj) { | ||
if (typeof obj !== 'object' || obj === null) return; | ||
@@ -171,3 +202,3 @@ Object.freeze(obj); | ||
var value = obj[name]; | ||
deepFreeze(value); | ||
_deepFreeze(value); | ||
} | ||
@@ -183,3 +214,3 @@ return obj; | ||
anAtom.read = function (get, options) { | ||
return deepFreeze(origRead.call(this, get, options)); | ||
return _deepFreeze(origRead.call(this, get, options)); | ||
}; | ||
@@ -197,3 +228,3 @@ if ('write' in anAtom) { | ||
if (setArgs[0] === anAtom) { | ||
setArgs[1] = deepFreeze(setArgs[1]); | ||
setArgs[1] = _deepFreeze(setArgs[1]); | ||
} | ||
@@ -235,3 +266,3 @@ return set.apply(void 0, setArgs); | ||
var mappingCache = new WeakMap(); | ||
var getMapping = function getMapping(arr, prev) { | ||
var _getMapping = function getMapping(arr, prev) { | ||
var mapping = mappingCache.get(arr); | ||
@@ -255,6 +286,6 @@ if (mapping) { | ||
var currArr = get(arrAtom); | ||
var mapping = getMapping(currArr, prev == null ? void 0 : prev.arr); | ||
var mapping = _getMapping(currArr, prev == null ? void 0 : prev.arr); | ||
var index = mapping.keyList.indexOf(key); | ||
if (index < 0 || index >= currArr.length) { | ||
var prevItem = arr[getMapping(arr).keyList.indexOf(key)]; | ||
var prevItem = arr[_getMapping(arr).keyList.indexOf(key)]; | ||
if (prevItem) { | ||
@@ -270,3 +301,3 @@ return prevItem; | ||
var arr = get(arrAtom); | ||
var mapping = getMapping(arr, prev == null ? void 0 : prev.arr); | ||
var mapping = _getMapping(arr, prev == null ? void 0 : prev.arr); | ||
var index = mapping.keyList.indexOf(key); | ||
@@ -300,3 +331,3 @@ if (index < 0 || index >= arr.length) { | ||
var arr = get(arrAtom); | ||
var mapping = getMapping(arr, prev == null ? void 0 : prev.arr); | ||
var mapping = _getMapping(arr, prev == null ? void 0 : prev.arr); | ||
return mapping; | ||
@@ -303,0 +334,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("jotai/vanilla")):"function"==typeof define&&define.amd?define(["exports","jotai/vanilla"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).jotaiVanillaUtils={},t.jotaiVanilla)}(this,(function(t,n){"use strict";var e=Symbol("");function r(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=Array(n);e<n;e++)r[e]=t[e];return r}function o(t,n){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return(e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,n){if(t){if("string"==typeof t)return r(t,n);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?r(t,n):void 0}}(t))||n){e&&(t=e);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function i(){return i=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var r in e)({}).hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},i.apply(null,arguments)}var a=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},u=new WeakMap;var f=new WeakSet,c=function t(n){if("object"==typeof n&&null!==n){Object.freeze(n);for(var e,r=o(Object.getOwnPropertyNames(n));!(e=r()).done;){t(n[e.value])}return n}};function l(t){if(f.has(t))return t;f.add(t);var n=t.read;if(t.read=function(t,e){return c(n.call(this,t,e))},"write"in t){var e=t.write;t.write=function(n,r){for(var o=arguments.length,i=new Array(o>2?o-2:0),a=2;a<o;a++)i[a-2]=arguments[a];return e.call.apply(e,[this,n,function(){for(var n=arguments.length,e=new Array(n),o=0;o<n;o++)e[o]=arguments[o];return e[0]===t&&(e[1]=c(e[1])),r.apply(void 0,e)}].concat(i))}}return t}var s=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},v=new WeakMap,d={},m=function(t){return!!t.write};var h=function(t){return"function"==typeof(null==t?void 0:t.then)};function p(t,n){var e,r;void 0===t&&(t=function(){try{return window.localStorage}catch(t){return}});var o,i={getItem:function(o,i){var a,u,f=function(t){if(e!==(t=t||"")){try{r=JSON.parse(t,null==n?void 0:n.reviver)}catch(t){return i}e=t}return r},c=null!=(a=null==(u=t())?void 0:u.getItem(o))?a:null;return h(c)?c.then(f):f(c)},setItem:function(e,r){var o;return null==(o=t())?void 0:o.setItem(e,JSON.stringify(r,null==n?void 0:n.replacer))},removeItem:function(n){var e;return null==(e=t())?void 0:e.removeItem(n)}};try{var a;o=null==(a=t())?void 0:a.subscribe}catch(t){}return!o&&"undefined"!=typeof window&&"function"==typeof window.addEventListener&&window.Storage&&(o=function(n,e){if(!(t()instanceof window.Storage))return function(){};var r=function(r){r.storageArea===t()&&r.key===n&&e(r.newValue)};return window.addEventListener("storage",r),function(){window.removeEventListener("storage",r)}}),o&&(i.subscribe=function(t){return function(n,e,r){return t(n,(function(t){var n;try{n=JSON.parse(t||"")}catch(t){n=r}e(n)}))}}(o)),i}var b=p();var y=new WeakMap,w={state:"loading"};var g=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},k=new WeakMap,O=function(){};t.RESET=e,t.atomFamily=function(t,n){var e=null,r=new Map,i=function i(a){var u;if(void 0===n)u=r.get(a);else for(var f,c=o(r);!(f=c()).done;){var l=f.value,s=l[0],v=l[1];if(n(s,a)){u=v;break}}if(void 0!==u){if(null==e||!e(u[1],a))return u[0];i.remove(a)}var d=t(a);return r.set(a,[d,Date.now()]),d};return i.remove=function(t){if(void 0===n)r.delete(t);else for(var e,i=o(r);!(e=i()).done;){var a=e.value[0];if(n(a,t)){r.delete(a);break}}},i.setShouldRemove=function(t){if(e=t)for(var n,i=o(r);!(n=i()).done;){var a=n.value,u=a[0],f=a[1];e(f[1],u)&&r.delete(u)}},i},t.atomWithDefault=function(t){var r=Symbol(),o=n.atom(r),i=n.atom((function(n,e){var i=n(o);return i!==r?i:t(n,e)}),(function(t,n,a){if(a===e)n(o,r);else if("function"==typeof a){var u=t(i);n(o,a(u))}else n(o,a)}));return i},t.atomWithLazy=function(t){var e=n.atom(void 0);return delete e.init,Object.defineProperty(e,"init",{get:function(){return t()}}),e},t.atomWithObservable=function(t,e){var r=function(t){if("e"in t)throw t.e;return t.d},o=n.atom((function(r){var o,i,a,u=t(r),f=null==(o=(i=u)[Symbol.observable])?void 0:o.call(i);f&&(u=f);var c,l,s,v,d=function(){return new Promise((function(t){a=t}))},m=e&&"initialValue"in e?{d:"function"==typeof e.initialValue?e.initialValue():e.initialValue}:d(),h=function(t){l=t,null==a||a(t),null==c||c(t)},p=function(){return!c},b=function(){s&&(clearTimeout(v),s.unsubscribe()),s=u.subscribe({next:function(t){return h({d:t})},error:function(t){return h({e:t})},complete:function(){}}),p()&&null!=e&&e.unstable_timeout&&(v=setTimeout((function(){s&&(s.unsubscribe(),s=void 0)}),e.unstable_timeout))};b();var y=n.atom(l||m);return y.onMount=function(t){return c=t,l&&t(l),s?clearTimeout(v):b(),function(){c=void 0,s&&(s.unsubscribe(),s=void 0)}},[y,u,d,b,p]}));return n.atom((function(t){var n=t(o),e=t(n[0]);return e instanceof Promise?e.then(r):r(e)}),(function(t,n,e){var r=t(o),i=r[0],a=r[1],u=r[2],f=r[3],c=r[4];if(!("next"in a))throw new Error("observable is not subject");c()&&(n(i,u()),f()),a.next(e)}))},t.atomWithReducer=function(t,e){return n.atom(t,(function(t,n,r){n(this,e(t(this),r))}))},t.atomWithRefresh=function(t,e){var r=n.atom(0);return n.atom((function(n,e){return n(r),t(n,e)}),(function(t,n){for(var o=arguments.length,i=new Array(o>2?o-2:0),a=2;a<o;a++)i[a-2]=arguments[a];if(0===i.length)n(r,(function(t){return t+1}));else if(e)return e.apply(void 0,[t,n].concat(i))}))},t.atomWithReset=function(t){var r=n.atom(t,(function(n,o,i){var a="function"==typeof i?i(n(r)):i;o(r,a===e?t:a)}));return r},t.atomWithStorage=function(t,r,o,i){void 0===o&&(o=b);var a=null==i?void 0:i.getOnInit,u=n.atom(a?o.getItem(t,r):r);return u.onMount=function(n){var e;return n(o.getItem(t,r)),o.subscribe&&(e=o.subscribe(t,n,r)),e},n.atom((function(t){return t(u)}),(function(n,i,a){var f="function"==typeof a?a(n(u)):a;return f===e?(i(u,r),o.removeItem(t)):f instanceof Promise?f.then((function(n){return i(u,n),o.setItem(t,n)})):(i(u,f),o.setItem(t,f))}))},t.createJSONStorage=p,t.freezeAtom=l,t.freezeAtomCreator=function(t){return function(){return l(t.apply(void 0,arguments))}},t.loadable=function(t){return e=function(){var e=new WeakMap,r=n.atom(0),o=n.atom((function(n,o){var i,a=o.setSelf;n(r);try{i=n(t)}catch(t){return{state:"hasError",error:t}}if(!(i instanceof Promise))return{state:"hasData",data:i};var u=i,f=e.get(u);if(f)return f;"fulfilled"===u.status?e.set(u,{state:"hasData",data:u.value}):"rejected"===u.status?e.set(u,{state:"hasError",error:u.reason}):u.then((function(t){e.set(u,{state:"hasData",data:t})}),(function(t){e.set(u,{state:"hasError",error:t})})).finally(a);var c=e.get(u);return c||(e.set(u,w),w)}),(function(t,n){n(r,(function(t){return t+1}))}));return n.atom((function(t){return t(o)}))},r=t,(y.has(r)?y:y.set(r,e())).get(r);var e,r},t.selectAtom=function(t,e,r){return void 0===r&&(r=Object.is),o=function(){var o=Symbol(),i=n.atom((function(n){var a=n(i);return function(t){var n=t[0],i=t[1];if(i===o)return e(n);var a=e(n,i);return r(i,a)?i:a}([n(t),a])}));return i.init=o,i},i=e,f=r,c=a((function(){return new WeakMap}),u,t),l=a((function(){return new WeakMap}),c,i),a(o,l,f);var o,i,f,c,l},t.splitAtom=function(t,e){return r=function(){var r=new WeakMap,o=function o(a,u){var f=r.get(a);if(f)return f;var c=u&&r.get(u),l=[],s=[];return a.forEach((function(r,u){var f=e?e(r):u;s[u]=f;var v=c&&c.atomList[c.keyList.indexOf(f)];if(v)l[u]=v;else{var d=function(n){var e=n(i),r=n(t),u=o(r,null==e?void 0:e.arr).keyList.indexOf(f);if(u<0||u>=r.length){var c=a[o(a).keyList.indexOf(f)];if(c)return c;throw new Error("splitAtom: index out of bounds for read")}return r[u]};l[u]=m(t)?n.atom(d,(function(n,e,r){var a=n(i),u=n(t),c=o(u,null==a?void 0:a.arr).keyList.indexOf(f);if(c<0||c>=u.length)throw new Error("splitAtom: index out of bounds for write");var l="function"==typeof r?r(u[c]):r;Object.is(u[c],l)||e(t,[].concat(u.slice(0,c),[l],u.slice(c+1)))})):n.atom(d)}})),f=c&&c.keyList.length===s.length&&c.keyList.every((function(t,n){return t===s[n]}))?c:{arr:a,atomList:l,keyList:s},r.set(a,f),f},i=n.atom((function(n){var e=n(i),r=n(t);return o(r,null==e?void 0:e.arr)}));i.init=void 0;var a=m(t)?n.atom((function(t){return t(i).atomList}),(function(n,e,r){switch(r.type){case"remove":var o=n(a).indexOf(r.atom);if(o>=0){var i=n(t);e(t,[].concat(i.slice(0,o),i.slice(o+1)))}break;case"insert":var u=r.before?n(a).indexOf(r.before):n(a).length;if(u>=0){var f=n(t);e(t,[].concat(f.slice(0,u),[r.value],f.slice(u)))}break;case"move":var c=n(a).indexOf(r.atom),l=r.before?n(a).indexOf(r.before):n(a).length;if(c>=0&&l>=0){var s=n(t);e(t,c<l?[].concat(s.slice(0,c),s.slice(c+1,l),[s[c]],s.slice(l)):[].concat(s.slice(0,l),[s[c]],s.slice(l,c),s.slice(c+1)))}}})):n.atom((function(t){return t(i).atomList}));return a},o=e||d,i=s((function(){return new WeakMap}),v,t),s(r,i,o);var r,o,i},t.unstable_withStorageValidator=function(t){return function(n){return i({},n,{getItem:function(e,r){var o=function(n){return t(n)?n:r},i=n.getItem(e,r);return h(i)?i.then(o):o(i)}})}},t.unwrap=function(t,e){return void 0===e&&(e=O),r=function(){var r=new WeakMap,o=new WeakMap,i=n.atom(0),a=n.atom((function(n,u){var f=u.setSelf;n(i);var c=n(a),l=n(t);if(!(l instanceof Promise))return{v:l};if(l!==(null==c?void 0:c.p)&&("fulfilled"===l.status?o.set(l,l.value):"rejected"===l.status?r.set(l,l.reason):l.then((function(t){return o.set(l,t)}),(function(t){return r.set(l,t)})).finally(f)),r.has(l))throw r.get(l);return o.has(l)?{p:l,v:o.get(l)}:c&&"v"in c?{p:l,f:e(c.v),v:c.v}:{p:l,f:e()}}),(function(t,n){n(i,(function(t){return t+1}))}));return a.init=void 0,n.atom((function(t){var n=t(a);return"f"in n?n.f:n.v}),(function(n,e){for(var r=arguments.length,o=new Array(r>2?r-2:0),i=2;i<r;i++)o[i-2]=arguments[i];return e.apply(void 0,[t].concat(o))}))},o=e,i=g((function(){return new WeakMap}),k,t),g(r,i,o);var r,o,i}})); | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("jotai/vanilla")):"function"==typeof define&&define.amd?define(["exports","jotai/vanilla"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).jotaiVanillaUtils={},t.jotaiVanilla)}(this,(function(t,n){"use strict";var e=Symbol("");function r(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=Array(n);e<n;e++)r[e]=t[e];return r}function o(t,n){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return(e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,n){if(t){if("string"==typeof t)return r(t,n);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?r(t,n):void 0}}(t))||n){e&&(t=e);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function i(){return i=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var r in e)({}).hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},i.apply(null,arguments)}var a=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},u=new WeakMap;var f=new WeakSet,c=function(t){if("object"==typeof t&&null!==t){Object.freeze(t);for(var n,e=o(Object.getOwnPropertyNames(t));!(n=e()).done;){var r=t[n.value];c(r)}return t}};function l(t){if(f.has(t))return t;f.add(t);var n=t.read;if(t.read=function(t,e){return c(n.call(this,t,e))},"write"in t){var e=t.write;t.write=function(n,r){for(var o=arguments.length,i=new Array(o>2?o-2:0),a=2;a<o;a++)i[a-2]=arguments[a];return e.call.apply(e,[this,n,function(){for(var n=arguments.length,e=new Array(n),o=0;o<n;o++)e[o]=arguments[o];return e[0]===t&&(e[1]=c(e[1])),r.apply(void 0,e)}].concat(i))}}return t}var s=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},v=new WeakMap,d={},m=function(t){return!!t.write};var h=function(t){return"function"==typeof(null==t?void 0:t.then)};function p(t,n){var e,r;void 0===t&&(t=function(){try{return window.localStorage}catch(t){return}});var o,i={getItem:function(o,i){var a,u,f=function(t){if(e!==(t=t||"")){try{r=JSON.parse(t,null==n?void 0:n.reviver)}catch(t){return i}e=t}return r},c=null!=(a=null==(u=t())?void 0:u.getItem(o))?a:null;return h(c)?c.then(f):f(c)},setItem:function(e,r){var o;return null==(o=t())?void 0:o.setItem(e,JSON.stringify(r,null==n?void 0:n.replacer))},removeItem:function(n){var e;return null==(e=t())?void 0:e.removeItem(n)}};try{var a;o=null==(a=t())?void 0:a.subscribe}catch(t){}return!o&&"undefined"!=typeof window&&"function"==typeof window.addEventListener&&window.Storage&&(o=function(n,e){if(!(t()instanceof window.Storage))return function(){};var r=function(r){r.storageArea===t()&&r.key===n&&e(r.newValue)};return window.addEventListener("storage",r),function(){window.removeEventListener("storage",r)}}),o&&(i.subscribe=function(t){return function(n,e,r){return t(n,(function(t){var n;try{n=JSON.parse(t||"")}catch(t){n=r}e(n)}))}}(o)),i}var y=p();var b=new WeakMap,w={state:"loading"};var g=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},O=new WeakMap,k=function(){};t.RESET=e,t.atomFamily=function(t,n){var e=null,r=new Map,i=new Set,a=function(i){var f;if(void 0===n)f=r.get(i);else for(var c,l=o(r);!(c=l()).done;){var s=c.value,v=s[0],d=s[1];if(n(v,i)){f=d;break}}if(void 0!==f){if(null==e||!e(f[1],i))return f[0];a.remove(i)}var m=t(i);return r.set(i,[m,Date.now()]),u("CREATE",i,m),m};function u(t,n,e){for(var r,a=o(i);!(r=a()).done;){(0,r.value)({type:t,param:n,atom:e})}}return a.unstable_listen=function(t){return i.add(t),function(){i.delete(t)}},a.getParams=function(){return r.keys()},a.remove=function(t){if(void 0===n){if(!r.has(t))return;var e=r.get(t)[0];r.delete(t),u("REMOVE",t,e)}else for(var i,a=o(r);!(i=a()).done;){var f=i.value,c=f[0],l=f[1][0];if(n(c,t)){r.delete(c),u("REMOVE",c,l);break}}},a.setShouldRemove=function(t){if(e=t)for(var n,i=o(r);!(n=i()).done;){var a=n.value,f=a[0],c=a[1],l=c[0],s=c[1];e(s,f)&&(r.delete(f),u("REMOVE",f,l))}},a},t.atomWithDefault=function(t){var r=Symbol(),o=n.atom(r),i=n.atom((function(n,e){var i=n(o);return i!==r?i:t(n,e)}),(function(t,n,a){if(a===e)n(o,r);else if("function"==typeof a){var u=t(i);n(o,a(u))}else n(o,a)}));return i},t.atomWithLazy=function(t){var e=n.atom(void 0);return delete e.init,Object.defineProperty(e,"init",{get:function(){return t()}}),e},t.atomWithObservable=function(t,e){var r=function(t){if("e"in t)throw t.e;return t.d},o=n.atom((function(r){var o,i,a,u=t(r),f=null==(o=(i=u)[Symbol.observable])?void 0:o.call(i);f&&(u=f);var c,l,s,v,d=function(){return new Promise((function(t){a=t}))},m=e&&"initialValue"in e?{d:"function"==typeof e.initialValue?e.initialValue():e.initialValue}:d(),h=function(t){l=t,null==a||a(t),null==c||c(t)},p=function(){return!c},y=function(){s&&(clearTimeout(v),s.unsubscribe()),s=u.subscribe({next:function(t){return h({d:t})},error:function(t){return h({e:t})},complete:function(){}}),p()&&null!=e&&e.unstable_timeout&&(v=setTimeout((function(){s&&(s.unsubscribe(),s=void 0)}),e.unstable_timeout))};y();var b=n.atom(l||m);return b.onMount=function(t){return c=t,l&&t(l),s?clearTimeout(v):y(),function(){c=void 0,s&&(s.unsubscribe(),s=void 0)}},[b,u,d,y,p]}));return n.atom((function(t){var n=t(o),e=t(n[0]);return e instanceof Promise?e.then(r):r(e)}),(function(t,n,e){var r=t(o),i=r[0],a=r[1],u=r[2],f=r[3],c=r[4];if(!("next"in a))throw new Error("observable is not subject");c()&&(n(i,u()),f()),a.next(e)}))},t.atomWithReducer=function(t,e){return n.atom(t,(function(t,n,r){n(this,e(t(this),r))}))},t.atomWithRefresh=function(t,e){var r=n.atom(0);return n.atom((function(n,e){return n(r),t(n,e)}),(function(t,n){for(var o=arguments.length,i=new Array(o>2?o-2:0),a=2;a<o;a++)i[a-2]=arguments[a];if(0===i.length)n(r,(function(t){return t+1}));else if(e)return e.apply(void 0,[t,n].concat(i))}))},t.atomWithReset=function(t){var r=n.atom(t,(function(n,o,i){var a="function"==typeof i?i(n(r)):i;o(r,a===e?t:a)}));return r},t.atomWithStorage=function(t,r,o,i){void 0===o&&(o=y);var a=null==i?void 0:i.getOnInit,u=n.atom(a?o.getItem(t,r):r);return u.onMount=function(n){var e;return n(o.getItem(t,r)),o.subscribe&&(e=o.subscribe(t,n,r)),e},n.atom((function(t){return t(u)}),(function(n,i,a){var f="function"==typeof a?a(n(u)):a;return f===e?(i(u,r),o.removeItem(t)):f instanceof Promise?f.then((function(n){return i(u,n),o.setItem(t,n)})):(i(u,f),o.setItem(t,f))}))},t.createJSONStorage=p,t.freezeAtom=l,t.freezeAtomCreator=function(t){return function(){return l(t.apply(void 0,arguments))}},t.loadable=function(t){return e=function(){var e=new WeakMap,r=n.atom(0),o=n.atom((function(n,o){var i,a=o.setSelf;n(r);try{i=n(t)}catch(t){return{state:"hasError",error:t}}if(!(i instanceof Promise))return{state:"hasData",data:i};var u=i,f=e.get(u);if(f)return f;"fulfilled"===u.status?e.set(u,{state:"hasData",data:u.value}):"rejected"===u.status?e.set(u,{state:"hasError",error:u.reason}):u.then((function(t){e.set(u,{state:"hasData",data:t})}),(function(t){e.set(u,{state:"hasError",error:t})})).finally(a);var c=e.get(u);return c||(e.set(u,w),w)}),(function(t,n){n(r,(function(t){return t+1}))}));return n.atom((function(t){return t(o)}))},r=t,(b.has(r)?b:b.set(r,e())).get(r);var e,r},t.selectAtom=function(t,e,r){return void 0===r&&(r=Object.is),o=function(){var o=Symbol(),i=n.atom((function(n){var a=n(i);return function(t){var n=t[0],i=t[1];if(i===o)return e(n);var a=e(n,i);return r(i,a)?i:a}([n(t),a])}));return i.init=o,i},i=e,f=r,c=a((function(){return new WeakMap}),u,t),l=a((function(){return new WeakMap}),c,i),a(o,l,f);var o,i,f,c,l},t.splitAtom=function(t,e){return r=function(){var r=new WeakMap,o=function(a,u){var f=r.get(a);if(f)return f;var c=u&&r.get(u),l=[],s=[];return a.forEach((function(r,u){var f=e?e(r):u;s[u]=f;var v=c&&c.atomList[c.keyList.indexOf(f)];if(v)l[u]=v;else{var d=function(n){var e=n(i),r=n(t),u=o(r,null==e?void 0:e.arr).keyList.indexOf(f);if(u<0||u>=r.length){var c=a[o(a).keyList.indexOf(f)];if(c)return c;throw new Error("splitAtom: index out of bounds for read")}return r[u]};l[u]=m(t)?n.atom(d,(function(n,e,r){var a=n(i),u=n(t),c=o(u,null==a?void 0:a.arr).keyList.indexOf(f);if(c<0||c>=u.length)throw new Error("splitAtom: index out of bounds for write");var l="function"==typeof r?r(u[c]):r;Object.is(u[c],l)||e(t,[].concat(u.slice(0,c),[l],u.slice(c+1)))})):n.atom(d)}})),f=c&&c.keyList.length===s.length&&c.keyList.every((function(t,n){return t===s[n]}))?c:{arr:a,atomList:l,keyList:s},r.set(a,f),f},i=n.atom((function(n){var e=n(i),r=n(t);return o(r,null==e?void 0:e.arr)}));i.init=void 0;var a=m(t)?n.atom((function(t){return t(i).atomList}),(function(n,e,r){switch(r.type){case"remove":var o=n(a).indexOf(r.atom);if(o>=0){var i=n(t);e(t,[].concat(i.slice(0,o),i.slice(o+1)))}break;case"insert":var u=r.before?n(a).indexOf(r.before):n(a).length;if(u>=0){var f=n(t);e(t,[].concat(f.slice(0,u),[r.value],f.slice(u)))}break;case"move":var c=n(a).indexOf(r.atom),l=r.before?n(a).indexOf(r.before):n(a).length;if(c>=0&&l>=0){var s=n(t);e(t,c<l?[].concat(s.slice(0,c),s.slice(c+1,l),[s[c]],s.slice(l)):[].concat(s.slice(0,l),[s[c]],s.slice(l,c),s.slice(c+1)))}}})):n.atom((function(t){return t(i).atomList}));return a},o=e||d,i=s((function(){return new WeakMap}),v,t),s(r,i,o);var r,o,i},t.unstable_withStorageValidator=function(t){return function(n){return i({},n,{getItem:function(e,r){var o=function(n){return t(n)?n:r},i=n.getItem(e,r);return h(i)?i.then(o):o(i)}})}},t.unwrap=function(t,e){return void 0===e&&(e=k),r=function(){var r=new WeakMap,o=new WeakMap,i=n.atom(0),a=n.atom((function(n,u){var f=u.setSelf;n(i);var c=n(a),l=n(t);if(!(l instanceof Promise))return{v:l};if(l!==(null==c?void 0:c.p)&&("fulfilled"===l.status?o.set(l,l.value):"rejected"===l.status?r.set(l,l.reason):l.then((function(t){return o.set(l,t)}),(function(t){return r.set(l,t)})).finally(f)),r.has(l))throw r.get(l);return o.has(l)?{p:l,v:o.get(l)}:c&&"v"in c?{p:l,f:e(c.v),v:c.v}:{p:l,f:e()}}),(function(t,n){n(i,(function(t){return t+1}))}));return a.init=void 0,n.atom((function(t){var n=t(a);return"f"in n?n.f:n.v}),(function(n,e){for(var r=arguments.length,o=new Array(r>2?r-2:0),i=2;i<r;i++)o[i-2]=arguments[i];return e.apply(void 0,[t].concat(o))}))},o=e,i=g((function(){return new WeakMap}),O,t),g(r,i,o);var r,o,i}})); |
@@ -263,3 +263,3 @@ 'use strict'; | ||
}; | ||
var readAtomState = function readAtomState(pending, atom, force) { | ||
var _readAtomState = function readAtomState(pending, atom, force) { | ||
var atomState = getAtomState(atom); | ||
@@ -273,3 +273,3 @@ if (!(force != null && force(atom)) && isAtomStateInitialized(atomState)) { | ||
n = _ref[1]; | ||
return readAtomState(pending, a, force).n === n; | ||
return _readAtomState(pending, a, force).n === n; | ||
})) { | ||
@@ -293,3 +293,3 @@ return atomState; | ||
} | ||
var aState = readAtomState(pending, a, force); | ||
var aState = _readAtomState(pending, a, force); | ||
if (isSync) { | ||
@@ -357,3 +357,3 @@ addDependency(pending, atom, a, aState); | ||
var readAtom = function readAtom(atom) { | ||
return returnAtomValue(readAtomState(undefined, atom)); | ||
return returnAtomValue(_readAtomState(undefined, atom)); | ||
}; | ||
@@ -376,3 +376,3 @@ var recomputeDependents = function recomputeDependents(pending, atom) { | ||
var markedAtoms = new Set(); | ||
var visit = function visit(n) { | ||
var _visit = function visit(n) { | ||
if (markedAtoms.has(n)) { | ||
@@ -385,3 +385,3 @@ return; | ||
if (n !== m) { | ||
visit(m); | ||
_visit(m); | ||
} | ||
@@ -391,3 +391,3 @@ } | ||
}; | ||
visit(atom); | ||
_visit(atom); | ||
var changedAtoms = new Set([atom]); | ||
@@ -410,3 +410,3 @@ var isMarked = function isMarked(a) { | ||
if (hasChangedDeps) { | ||
readAtomState(pending, a, isMarked); | ||
_readAtomState(pending, a, isMarked); | ||
mountDependencies(pending, a, aState); | ||
@@ -421,5 +421,5 @@ if (prevEpochNumber !== aState.n) { | ||
}; | ||
var writeAtomState = function writeAtomState(pending, atom) { | ||
var _writeAtomState = function writeAtomState(pending, atom) { | ||
var getter = function getter(a) { | ||
return returnAtomValue(readAtomState(pending, a)); | ||
return returnAtomValue(_readAtomState(pending, a)); | ||
}; | ||
@@ -446,3 +446,3 @@ var setter = function setter(a) { | ||
} else { | ||
r = writeAtomState.apply(void 0, [pending, a].concat(args)); | ||
r = _writeAtomState.apply(void 0, [pending, a].concat(args)); | ||
} | ||
@@ -463,3 +463,3 @@ flushPending(pending); | ||
} | ||
var result = writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
var result = _writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
flushPending(pending); | ||
@@ -473,3 +473,3 @@ return result; | ||
if (!atomState.m.d.has(a)) { | ||
var aMounted = mountAtom(pending, a); | ||
var aMounted = _mountAtom(pending, a); | ||
aMounted.t.add(atom); | ||
@@ -483,3 +483,3 @@ atomState.m.d.add(a); | ||
atomState.m.d.delete(_a); | ||
var _aMounted = unmountAtom(pending, _a); | ||
var _aMounted = _unmountAtom(pending, _a); | ||
_aMounted == null || _aMounted.t.delete(atom); | ||
@@ -490,9 +490,9 @@ } | ||
}; | ||
var mountAtom = function mountAtom(pending, atom) { | ||
var _mountAtom = function mountAtom(pending, atom) { | ||
var atomState = getAtomState(atom); | ||
if (!atomState.m) { | ||
readAtomState(pending, atom); | ||
_readAtomState(pending, atom); | ||
for (var _iterator7 = _createForOfIteratorHelperLoose(atomState.d.keys()), _step7; !(_step7 = _iterator7()).done;) { | ||
var a = _step7.value; | ||
var aMounted = mountAtom(pending, a); | ||
var aMounted = _mountAtom(pending, a); | ||
aMounted.t.add(atom); | ||
@@ -516,3 +516,3 @@ } | ||
} | ||
return writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
return _writeAtomState.apply(void 0, [pending, atom].concat(args)); | ||
}); | ||
@@ -527,3 +527,3 @@ if (onUnmount) { | ||
}; | ||
var unmountAtom = function unmountAtom(pending, atom) { | ||
var _unmountAtom = function unmountAtom(pending, atom) { | ||
var atomState = getAtomState(atom); | ||
@@ -544,3 +544,3 @@ if (atomState.m && !atomState.m.l.size && !Array.from(atomState.m.t).some(function (a) { | ||
var a = _step8.value; | ||
var aMounted = unmountAtom(pending, a); | ||
var aMounted = _unmountAtom(pending, a); | ||
aMounted == null || aMounted.t.delete(atom); | ||
@@ -558,3 +558,3 @@ } | ||
var pending = createPending(); | ||
var mounted = mountAtom(pending, atom); | ||
var mounted = _mountAtom(pending, atom); | ||
flushPending(pending); | ||
@@ -566,3 +566,3 @@ var listeners = mounted.l; | ||
var pending = createPending(); | ||
unmountAtom(pending, atom); | ||
_unmountAtom(pending, atom); | ||
flushPending(pending); | ||
@@ -569,0 +569,0 @@ }; |
@@ -63,3 +63,4 @@ 'use strict'; | ||
var atoms = new Map(); | ||
var createAtom = function createAtom(param) { | ||
var listeners = new Set(); | ||
var _createAtom = function createAtom(param) { | ||
var item; | ||
@@ -81,3 +82,3 @@ if (areEqual === undefined) { | ||
if (shouldRemove != null && shouldRemove(item[1], param)) { | ||
createAtom.remove(param); | ||
_createAtom.remove(param); | ||
} else { | ||
@@ -89,13 +90,40 @@ return item[0]; | ||
atoms.set(param, [newAtom, Date.now()]); | ||
notifyListeners('CREATE', param, newAtom); | ||
return newAtom; | ||
}; | ||
createAtom.remove = function (param) { | ||
function notifyListeners(type, param, atom) { | ||
for (var _iterator2 = _createForOfIteratorHelperLoose(listeners), _step2; !(_step2 = _iterator2()).done;) { | ||
var listener = _step2.value; | ||
listener({ | ||
type: type, | ||
param: param, | ||
atom: atom | ||
}); | ||
} | ||
} | ||
_createAtom.unstable_listen = function (callback) { | ||
listeners.add(callback); | ||
return function () { | ||
listeners.delete(callback); | ||
}; | ||
}; | ||
_createAtom.getParams = function () { | ||
return atoms.keys(); | ||
}; | ||
_createAtom.remove = function (param) { | ||
if (areEqual === undefined) { | ||
if (!atoms.has(param)) return; | ||
var _ref = atoms.get(param), | ||
atom = _ref[0]; | ||
atoms.delete(param); | ||
notifyListeners('REMOVE', param, atom); | ||
} else { | ||
for (var _iterator2 = _createForOfIteratorHelperLoose(atoms), _step2; !(_step2 = _iterator2()).done;) { | ||
var _step2$value = _step2.value, | ||
key = _step2$value[0]; | ||
for (var _iterator3 = _createForOfIteratorHelperLoose(atoms), _step3; !(_step3 = _iterator3()).done;) { | ||
var _step3$value = _step3.value, | ||
key = _step3$value[0], | ||
_step3$value$ = _step3$value[1], | ||
_atom = _step3$value$[0]; | ||
if (areEqual(key, param)) { | ||
atoms.delete(key); | ||
notifyListeners('REMOVE', key, _atom); | ||
break; | ||
@@ -106,15 +134,18 @@ } | ||
}; | ||
createAtom.setShouldRemove = function (fn) { | ||
_createAtom.setShouldRemove = function (fn) { | ||
shouldRemove = fn; | ||
if (!shouldRemove) return; | ||
for (var _iterator3 = _createForOfIteratorHelperLoose(atoms), _step3; !(_step3 = _iterator3()).done;) { | ||
var _step3$value = _step3.value, | ||
key = _step3$value[0], | ||
value = _step3$value[1]; | ||
if (shouldRemove(value[1], key)) { | ||
for (var _iterator4 = _createForOfIteratorHelperLoose(atoms), _step4; !(_step4 = _iterator4()).done;) { | ||
var _step4$value = _step4.value, | ||
key = _step4$value[0], | ||
_step4$value$ = _step4$value[1], | ||
atom = _step4$value$[0], | ||
_createdAt = _step4$value$[1]; | ||
if (shouldRemove(_createdAt, key)) { | ||
atoms.delete(key); | ||
notifyListeners('REMOVE', key, atom); | ||
} | ||
} | ||
}; | ||
return createAtom; | ||
return _createAtom; | ||
} | ||
@@ -161,3 +192,3 @@ | ||
var frozenAtoms = new WeakSet(); | ||
var deepFreeze = function deepFreeze(obj) { | ||
var _deepFreeze = function deepFreeze(obj) { | ||
if (typeof obj !== 'object' || obj === null) return; | ||
@@ -169,3 +200,3 @@ Object.freeze(obj); | ||
var value = obj[name]; | ||
deepFreeze(value); | ||
_deepFreeze(value); | ||
} | ||
@@ -181,3 +212,3 @@ return obj; | ||
anAtom.read = function (get, options) { | ||
return deepFreeze(origRead.call(this, get, options)); | ||
return _deepFreeze(origRead.call(this, get, options)); | ||
}; | ||
@@ -195,3 +226,3 @@ if ('write' in anAtom) { | ||
if (setArgs[0] === anAtom) { | ||
setArgs[1] = deepFreeze(setArgs[1]); | ||
setArgs[1] = _deepFreeze(setArgs[1]); | ||
} | ||
@@ -233,3 +264,3 @@ return set.apply(void 0, setArgs); | ||
var mappingCache = new WeakMap(); | ||
var getMapping = function getMapping(arr, prev) { | ||
var _getMapping = function getMapping(arr, prev) { | ||
var mapping = mappingCache.get(arr); | ||
@@ -253,6 +284,6 @@ if (mapping) { | ||
var currArr = get(arrAtom); | ||
var mapping = getMapping(currArr, prev == null ? void 0 : prev.arr); | ||
var mapping = _getMapping(currArr, prev == null ? void 0 : prev.arr); | ||
var index = mapping.keyList.indexOf(key); | ||
if (index < 0 || index >= currArr.length) { | ||
var prevItem = arr[getMapping(arr).keyList.indexOf(key)]; | ||
var prevItem = arr[_getMapping(arr).keyList.indexOf(key)]; | ||
if (prevItem) { | ||
@@ -268,3 +299,3 @@ return prevItem; | ||
var arr = get(arrAtom); | ||
var mapping = getMapping(arr, prev == null ? void 0 : prev.arr); | ||
var mapping = _getMapping(arr, prev == null ? void 0 : prev.arr); | ||
var index = mapping.keyList.indexOf(key); | ||
@@ -298,3 +329,3 @@ if (index < 0 || index >= arr.length) { | ||
var arr = get(arrAtom); | ||
var mapping = getMapping(arr, prev == null ? void 0 : prev.arr); | ||
var mapping = _getMapping(arr, prev == null ? void 0 : prev.arr); | ||
return mapping; | ||
@@ -301,0 +332,0 @@ }); |
@@ -1,9 +0,25 @@ | ||
import type { Atom } from 'jotai/vanilla'; | ||
type ShouldRemove<Param> = (createdAt: number, param: Param) => boolean; | ||
import { type Atom } from 'jotai/vanilla'; | ||
/** | ||
* in milliseconds | ||
*/ | ||
type CreatedAt = number; | ||
type ShouldRemove<Param> = (createdAt: CreatedAt, param: Param) => boolean; | ||
type Cleanup = () => void; | ||
type Callback<Param, AtomType> = (event: { | ||
type: 'CREATE' | 'REMOVE'; | ||
param: Param; | ||
atom: AtomType; | ||
}) => void; | ||
export interface AtomFamily<Param, AtomType> { | ||
(param: Param): AtomType; | ||
getParams(): Iterable<Param>; | ||
remove(param: Param): void; | ||
setShouldRemove(shouldRemove: ShouldRemove<Param> | null): void; | ||
/** | ||
* fires when a atom is created or removed | ||
* This API is for advanced use cases, and can change without notice. | ||
*/ | ||
unstable_listen(callback: Callback<Param, AtomType>): Cleanup; | ||
} | ||
export declare function atomFamily<Param, AtomType extends Atom<unknown>>(initializeAtom: (param: Param) => AtomType, areEqual?: (a: Param, b: Param) => boolean): AtomFamily<Param, AtomType>; | ||
export {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
422176
9232