1 | !function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).jotaiVanilla={})}(this,(function(n){"use strict";var t=0;var e,r=function(n){return"init"in n},i=function(n){return!!n.write},o=new WeakMap,u=function(n,t){var e=o.get(n);e&&(o.delete(n),e(t))},a=function(n,t){n.status="fulfilled",n.value=t},f=function(n,t){n.status="rejected",n.reason=t},c=function(n,t){return"v"in n&&"v"in t&&Object.is(n.v,t.v)},v=function(n,t){return"e"in n&&"e"in t&&Object.is(n.e,t.e)},l=function(n){return"v"in n&&n.v instanceof Promise},d=function(n){if("e"in n)throw n.e;return n.v},s=function(){var n=new WeakMap,t=new WeakMap,e=new Map,s=function(t){return n.get(t)},g=function(t,r){var i=n.get(t);if(n.set(t,r),e.has(t)||e.set(t,i),i&&l(i)){var o="v"in r?r.v instanceof Promise?r.v:Promise.resolve(r.v):Promise.reject(r.e);u(i.v,o)}},h=function(n,t,e){var r=new Map,i=!1;e.forEach((function(e,o){e||o!==n||(e=t),e&&(r.set(o,e),t.d.get(o)!==e&&(i=!0))})),(i||t.d.size!==r.size)&&(t.d=r)},p=function(n,t,e){var r,i,o=s(n),u={d:(null==o?void 0:o.d)||new Map,v:t};if(e&&h(n,u,e),o&&c(o,u)&&o.d===u.d)return o;if(o&&l(o)&&l(u)&&(i=u,"v"in(r=o)&&"v"in i&&r.v.orig&&r.v.orig===i.v.orig)){if(o.d===u.d)return o;u.v=o.v}return g(n,u),u},w=function(n,t,e,r){if("function"==typeof(null==(c=t)?void 0:c.then)){var i,u=new Promise((function(r,o){var c=!1;t.then((function(t){if(!c){c=!0;var i=s(n),o=p(n,u,e);a(u,t),r(t),(null==i?void 0:i.d)!==o.d&&S(n,o,null==i?void 0:i.d)}}),(function(t){if(!c){c=!0;var r=s(n),i=p(n,u,e);f(u,t),o(t),(null==r?void 0:r.d)!==i.d&&S(n,i,null==r?void 0:r.d)}})),i=function(n){c||(c=!0,n.then((function(n){return a(u,n)}),(function(n){return f(u,n)})),r(n))}}));return u.orig=t,u.status="pending",function(n,t){o.set(n,t),n.catch((function(){})).finally((function(){return o.delete(n)}))}(u,(function(n){n&&i(n),null==r||r()})),p(n,u,e)}var c;return p(n,t,e)},y=function n(e){var o=s(e);if(o&&(o.d.forEach((function(r,i){i===e||t.has(i)||n(i)})),Array.from(o.d).every((function(n){var t=n[0],r=n[1];return t===e||s(t)===r}))))return o;var u,a,f=new Map,c=!0,l={get signal(){return u||(u=new AbortController),u.signal},get setSelf(){return!a&&i(e)&&(a=function(){if(!c){for(var n=arguments.length,t=new Array(n),r=0;r<n;r++)t[r]=arguments[r];return M.apply(void 0,[e].concat(t))}}),a}};try{var p=e.read((function(t){if(t===e){var i=s(t);if(i)return f.set(t,i),d(i);if(r(t))return f.set(t,void 0),t.init;throw new Error("no atom init")}var o=n(t);return f.set(t,o),d(o)}),l);return w(e,p,f,(function(){var n;return null==(n=u)?void 0:n.abort()}))}catch(n){return function(n,t,e){var r=s(n),i={d:(null==r?void 0:r.d)||new Map,e:t};return e&&h(n,i,e),r&&v(r,i)&&r.d===i.d?r:(g(n,i),i)}(e,n,f)}finally{c=!1}},m=function(n,t){return!t.l.size&&(!t.t.size||1===t.t.size&&t.t.has(n))},E=function n(e){for(var i=!0,o=arguments.length,u=new Array(o>1?o-1:0),a=1;a<o;a++)u[a-1]=arguments[a];var f=e.write.apply(e,[function(n){return d(y(n))},function(o){for(var u,a=arguments.length,f=new Array(a>1?a-1:0),v=1;v<a;v++)f[v-1]=arguments[v];if(o===e){if(!r(o))throw new Error("atom not writable");var l=s(o),d=w(o,f[0]);l&&c(l,d)||function(n){var e=new Map,r=new WeakMap;!function n(i){var o=t.get(i);null==o||o.t.forEach((function(t){t!==i&&(e.set(t,(e.get(t)||new Set).add(i)),r.set(t,(r.get(t)||0)+1),n(t))}))}(n),function n(i){var o=t.get(i);null==o||o.t.forEach((function(t){if(t!==i){var o=r.get(t);if(o&&r.set(t,--o),!o){var u,a=!(null==(u=e.get(t))||!u.size);if(a){var f=s(t),v=y(t);a=!f||!c(f,v)}a||e.forEach((function(n){return n.delete(t)}))}n(t)}}))}(n)}(o)}else u=n.apply(void 0,[o].concat(f));return i||z(),u}].concat(u));return i=!1,f},M=function(n){for(var t=arguments.length,e=new Array(t>1?t-1:0),r=1;r<t;r++)e[r-1]=arguments[r];var i=E.apply(void 0,[n].concat(e));return z(),i},b=function n(e,r){var o={t:new Set(r&&[r]),l:new Set};if(t.set(e,o),y(e).d.forEach((function(r,i){var o=t.get(i);o?o.t.add(e):i!==e&&n(i,e)})),y(e),i(e)&&e.onMount){var u=e.onMount((function(){for(var n=arguments.length,t=new Array(n),r=0;r<n;r++)t[r]=arguments[r];return M.apply(void 0,[e].concat(t))}));u&&(o.u=u)}return o},A=function n(e){var r,i=null==(r=t.get(e))?void 0:r.u;i&&i(),t.delete(e);var o=s(e);o&&(l(o)&&u(o.v),o.d.forEach((function(r,i){if(i!==e){var o=t.get(i);o&&(o.t.delete(e),m(i,o)&&n(i))}})))},S=function(n,e,r){var i=new Set(e.d.keys());null==r||r.forEach((function(e,r){if(i.has(r))i.delete(r);else{var o=t.get(r);o&&(o.t.delete(n),m(r,o)&&A(r))}})),i.forEach((function(e){var r=t.get(e);r?r.t.add(n):t.has(n)&&b(e,n)}))},z=function(){for(;e.size;){var n=Array.from(e);e.clear(),n.forEach((function(n){var e=n[0],r=n[1],i=s(e);if(i){i.d!==(null==r?void 0:r.d)&&S(e,i,null==r?void 0:r.d);var o=t.get(e);!o||r&&!l(r)&&(c(r,i)||v(r,i))||o.l.forEach((function(n){return n()}))}}))}};return{get:function(n){return d(y(n))},set:M,sub:function(n,e){var r=function(n){var e=t.get(n);return e||(e=b(n)),e}(n);z();var i=r.l;return i.add(e),function(){i.delete(e),function(n){var e=t.get(n);e&&m(n,e)&&A(n)}(n)}}}};n.atom=function(n,e){var r="atom"+ ++t,i={toString:function(){return r}};return"function"==typeof n?i.read=n:(i.init=n,i.read=function(n){return n(i)},i.write=function(n,t,e){return t(i,"function"==typeof e?e(n(i)):e)}),e&&(i.write=e),i},n.createStore=s,n.getDefaultStore=function(){return e||(e=s()),e}}));
|