UNPKG

6.86 kBJavaScriptView Raw
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;function r(n){return n(this)}function e(n,t,r){return t(this,"function"==typeof r?r(n(this)):r)}function i(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function o(n,t){var r="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(r)return(r=r.call(n)).next.bind(r);if(Array.isArray(n)||(r=function(n,t){if(n){if("string"==typeof n)return i(n,t);var r=Object.prototype.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(n,t):void 0}}(n))||t&&n&&"number"==typeof n.length){r&&(n=r);var e=0;return function(){return e>=n.length?{done:!0}:{done:!1,value:n[e++]}}}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 a,u=function(n,t){return n.unstable_is?n.unstable_is(t):t===n},f=function(n){return"init"in n},c=function(n){return!!n.write},v=new WeakMap,l=function(n,t){var r=v.get(n);r&&(v.delete(n),r(t))},d=function(n,t){n.status="fulfilled",n.value=t},s=function(n,t){n.status="rejected",n.reason=t},h=function(n,t){return!!n&&"v"in n&&"v"in t&&Object.is(n.v,t.v)},p=function(n,t){return!!n&&"e"in n&&"e"in t&&Object.is(n.e,t.e)},g=function(n){return!!n&&"v"in n&&n.v instanceof Promise},w=function(n){if("e"in n)throw n.e;return n.v},y=function(){var n=new WeakMap,t=new WeakMap,r=[],e=new WeakMap,i=function(t){return n.get(t)},a=function n(t,o){o.d.forEach((function(o,a){if(!e.has(a)){var u,f=i(a);null==(u=r[r.length-1])||u.add(a),e.set(a,[f,new Set]),f&&n(a,f)}e.get(a)[1].add(t)}))},y=function(t,o){var u,f=i(t);(n.set(t,o),e.has(t))||(null==(u=r[r.length-1])||u.add(t),e.set(t,[f,new Set]),a(t,o));if(g(f)){var c="v"in o?o.v instanceof Promise?o.v:Promise.resolve(o.v):Promise.reject(o.e);f.v!==c&&l(f.v,c)}},b=function(n,t,r,e){var i=new Map(e?t.d:null),o=!1;r.forEach((function(r,e){!r&&u(n,e)&&(r=t),r&&(i.set(e,r),t.d.get(e)!==r&&(o=!0))})),(o||t.d.size!==i.size)&&(t.d=i)},m=function(n,t,r,e){var o,a,u=i(n),f={d:(null==u?void 0:u.d)||new Map,v:t};if(r&&b(n,f,r,e),h(u,f)&&u.d===f.d)return u;if(g(u)&&g(f)&&(a=f,"v"in(o=u)&&"v"in a&&o.v.orig&&o.v.orig===a.v.orig)){if(u.d===f.d)return u;f.v=u.v}return y(n,f),f},S=function(n,r,e,o){if("function"==typeof(null==(c=r)?void 0:c.then)){var a,u=function(){var r=i(n);if(g(r)&&r.v===f){var o=m(n,f,e);t.has(n)&&r.d!==o.d&&P(n,o,r.d)}},f=new Promise((function(n,t){var e=!1;r.then((function(t){e||(e=!0,d(f,t),n(t),u())}),(function(n){e||(e=!0,s(f,n),t(n),u())})),a=function(t){e||(e=!0,t.then((function(n){return d(f,n)}),(function(n){return s(f,n)})),n(t))}}));return f.orig=r,f.status="pending",function(n,t){v.set(n,t),n.catch((function(){})).finally((function(){return v.delete(n)}))}(f,(function(n){n&&a(n),null==o||o()})),m(n,f,e,!0)}var c;return m(n,r,e)},E=function n(r,e){var o=i(r);if(!e&&o){if(t.has(r))return o;if(Array.from(o.d).every((function(t){var e=t[0],i=t[1];if(e===r)return!0;var o=n(e);return o===i||h(o,i)})))return o}var a,v,l=new Map,d=!0,s={get signal(){return a||(a=new AbortController),a.signal},get setSelf(){return!v&&c(r)&&(v=function(){if(!d){for(var n=arguments.length,t=new Array(n),e=0;e<n;e++)t[e]=arguments[e];return j.apply(void 0,[r].concat(t))}}),v}};try{var g=r.read((function(t){if(u(r,t)){var e=i(t);if(e)return l.set(t,e),w(e);if(f(t))return l.set(t,void 0),t.init;throw new Error("no atom init")}var o=n(t);return l.set(t,o),w(o)}),s);return S(r,g,l,(function(){var n;return null==(n=a)?void 0:n.abort()}))}catch(n){return function(n,t,r){var e=i(n),o={d:(null==e?void 0:e.d)||new Map,e:t};return r&&b(n,o,r),p(e,o)&&e.d===o.d?e:(y(n,o),o)}(r,n,l)}finally{d=!1}},A=function(n){var r=new Array,a=new Set;!function n(i){if(!a.has(i)){a.add(i);for(var u,f=o(function(n){var r,i,o=new Set(null==(r=t.get(n))?void 0:r.t);return null==(i=e.get(n))||i[1].forEach((function(n){o.add(n)})),o}(i));!(u=f()).done;){var c=u.value;i!==c&&n(c)}r.push(i)}}(n);for(var u=new Set([n]),f=r.length-1;f>=0;--f){var c=r[f],v=i(c);if(v){for(var l,d=!1,s=o(v.d.keys());!(l=s()).done;){var p=l.value;if(p!==c&&u.has(p)){d=!0;break}}if(d){var g=E(c,!0);h(v,g)||u.add(c)}}}},M=function n(t){for(var e=arguments.length,o=new Array(e>1?e-1:0),a=1;a<e;a++)o[a-1]=arguments[a];var c=t.write.apply(t,[function(n){return w(E(n))},function(e){var o,a=r.length>0;a||r.push(new Set([e]));for(var c=arguments.length,v=new Array(c>1?c-1:0),l=1;l<c;l++)v[l-1]=arguments[l];if(u(t,e)){if(!f(e))throw new Error("atom not writable");var d=i(e),s=S(e,v[0]);h(d,s)||A(e)}else o=n.apply(void 0,[e].concat(v));return a||x(r.pop()),o}].concat(o));return c},j=function(n){r.push(new Set([n]));for(var t=arguments.length,e=new Array(t>1?t-1:0),i=1;i<t;i++)e[i-1]=arguments[i];var o=M.apply(void 0,[n].concat(e));return x(r.pop()),o},k=function n(r,e,o){var a,u=t.get(r);if(u)return e&&u.t.add(e),u;var f=o||[];null==(a=i(r))||a.d.forEach((function(t,e){e!==r&&n(e,r,f)})),E(r);var v={t:new Set(e&&[e]),l:new Set};if(t.set(r,v),c(r)&&r.onMount){var l=r.onMount;f.push((function(){var n=l((function(){for(var n=arguments.length,t=new Array(n),e=0;e<n;e++)t[e]=arguments[e];return j.apply(void 0,[r].concat(t))}));n&&(v.u=n)}))}return o||f.forEach((function(n){return n()})),v},z=function n(r,e){if(function(n,t){return!t.l.size&&(!t.t.size||1===t.t.size&&t.t.has(n))}(r,e)){var o=e.u;o&&o(),t.delete(r);var a=i(r);a&&(g(a)&&l(a.v),a.d.forEach((function(e,i){if(i!==r){var o=t.get(i);o&&(o.t.delete(r),n(i,o))}})))}},P=function(n,r,e){var i=new Set(r.d.keys()),o=new Set;null==e||e.forEach((function(r,e){if(i.has(e))i.delete(e);else{o.add(e);var a=t.get(e);a&&a.t.delete(n)}})),i.forEach((function(t){k(t,n)})),o.forEach((function(n){var r=t.get(n);r&&z(n,r)}))},x=function(n){var r=[];n.forEach((function n(t){var o;if(e.has(t)){var a=e.get(t),u=a[0],f=a[1];e.delete(t),r.push([t,u]),f.forEach(n),null==(o=i(t))||o.d.forEach((function(t,r){return n(r)}))}})),r.forEach((function(n){var r=n[0],e=n[1],o=i(r);if(o&&o!==e){var a=t.get(r);a&&o.d!==(null==e?void 0:e.d)&&P(r,o,null==e?void 0:e.d),a&&(g(e)||!h(e,o)&&!p(e,o))&&a.l.forEach((function(n){return n()}))}}))};return{get:function(n){return w(E(n))},set:j,sub:function(n,t){var r=k(n);x([n]);var e=r.l;return e.add(t),function(){e.delete(t),z(n,r)}}}},b=y,m=function(){return a||(a=y()),a};n.atom=function(n,i){var o="atom"+ ++t,a={toString:function(){return o}};return"function"==typeof n?a.read=n:(a.init=n,a.read=r,a.write=e),i&&(a.write=i),a},n.createStore=b,n.getDefaultStore=m}));