UNPKG

8.55 kBJavaScriptView Raw
1!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai/vanilla")):"function"==typeof define&&define.amd?define(["exports","jotai/vanilla"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).jotaiUtils={},n.vanilla)}(this,(function(n,t){"use strict";var e=Symbol();function r(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new Array(t);e<t;e++)r[e]=n[e];return r}function o(n,t){var e="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(e)return(e=e.call(n)).next.bind(e);if(Array.isArray(n)||(e=function(n,t){if(n){if("string"==typeof n)return r(n,t);var e=Object.prototype.toString.call(n).slice(8,-1);return"Object"===e&&n.constructor&&(e=n.constructor.name),"Map"===e||"Set"===e?Array.from(n):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?r(n,t):void 0}}(n))||t&&n&&"number"==typeof n.length){e&&(n=e);var o=0;return function(){return o>=n.length?{done:!0}:{done:!1,value:n[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.")}var i=function(n,t,e){return(t.has(e)?t:t.set(e,n())).get(e)},a=new WeakMap;var u=new WeakMap,f=function n(t){if("object"==typeof t&&null!==t){Object.freeze(t);for(var e,r=o(Object.getOwnPropertyNames(t));!(e=r()).done;){n(t[e.value])}return t}};var c=function(n,t,e){return(t.has(e)?t:t.set(e,n())).get(e)},l=new WeakMap,s={},v=function(n){return!!n.write};var m=function(n,t){return"function"==typeof t?t(n):t};var d=Symbol();function b(n){var t,e,r={getItem:function(r){var o,i,a=function(n){if(t!==(n=n||"")){try{e=JSON.parse(n)}catch(n){return d}t=n}return e},u=null!=(o=null==(i=n())?void 0:i.getItem(r))?o:null;return u instanceof Promise?u.then(a):a(u)},setItem:function(t,e){var r;return null==(r=n())?void 0:r.setItem(t,JSON.stringify(e))},removeItem:function(t){var e;return null==(e=n())?void 0:e.removeItem(t)}};return"undefined"!=typeof window&&"function"==typeof window.addEventListener&&(r.subscribe=function(n,t){var e=function(e){e.key===n&&e.newValue&&t(JSON.parse(e.newValue))};return window.addEventListener("storage",e),function(){window.removeEventListener("storage",e)}}),r}var p=b((function(){return"undefined"!=typeof window?window.localStorage:void 0}));var y=new WeakMap,h={state:"loading"};var w=function(n,t,e){return(t.has(e)?t:t.set(e,n())).get(e)},g=new WeakMap,k=function(){};n.RESET=e,n.atomFamily=function(n,t){var e=null,r=new Map,i=function i(a){var u;if(void 0===t)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(t(s,a)){u=v;break}}if(void 0!==u){if(null==e||!e(u[1],a))return u[0];i.remove(a)}var m=n(a);return r.set(a,[m,Date.now()]),m};return i.remove=function(n){if(void 0===t)r.delete(n);else for(var e,i=o(r);!(e=i()).done;){var a=e.value[0];if(t(a,n)){r.delete(a);break}}},i.setShouldRemove=function(n){if(e=n)for(var t,i=o(r);!(t=i()).done;){var a=t.value,u=a[0],f=a[1];e(f[1],u)&&r.delete(u)}},i},n.atomWithDefault=function(n){var r=Symbol(),o=t.atom(r),i=t.atom((function(t,e){var i=t(o);return i!==r?i:n(t,e)}),(function(n,t,a){if(a===e)return t(o,r);var u=n(i);return u instanceof Promise?u.then((function(n){return t(o,m(n,a))})):t(o,m(u,a))}));return i},n.atomWithObservable=function(n,e){var r=function(n){if("e"in n)throw n.e;return n.d},o=t.atom((function(r){var o,i,a,u=n(r),f=null==(o=(i=u)[Symbol.observable])?void 0:o.call(i);f&&(u=f);var c,l,s,v,m=function(){return new Promise((function(n){a=n}))},d=e&&"initialValue"in e?{d:"function"==typeof e.initialValue?e.initialValue():e.initialValue}:m(),b=function(n){l=n,null==a||a(n),null==c||c(n)},p=function(){return!c},y=function(){s&&(clearTimeout(v),s.unsubscribe()),s=u.subscribe({next:function(n){return b({d:n})},error:function(n){return b({e:n})},complete:function(){}}),p()&&null!=e&&e.unstable_timeout&&(v=setTimeout((function(){s&&(s.unsubscribe(),s=void 0)}),e.unstable_timeout))};y();var h=t.atom(l||d);return h.onMount=function(n){return c=n,l&&n(l),s?clearTimeout(v):y(),function(){c=void 0,s&&(s.unsubscribe(),s=void 0)}},[h,u,m,y,p]}));return t.atom((function(n){var t=n(o),e=n(t[0]);return e instanceof Promise?e.then(r):r(e)}),(function(n,t,e){var r=n(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()&&(t(i,u()),f()),a.next(e)}))},n.atomWithReducer=function(n,e){var r=t.atom(n,(function(n,t,o){return t(r,e(n(r),o))}));return r},n.atomWithReset=function(n){var r=t.atom(n,(function(t,o,i){var a="function"==typeof i?i(t(r)):i;o(r,a===e?n:a)}));return r},n.atomWithStorage=function(n,r,o){void 0===o&&(o=p);var i=t.atom(r);return i.onMount=function(t){var e,i=o.getItem(n);return i instanceof Promise?i.then((function(n){return t(n===d?r:n)})):t(i===d?r:i),o.subscribe&&(e=o.subscribe(n,t)),e},t.atom((function(n){return n(i)}),(function(t,a,u){var f="function"==typeof u?u(t(i)):u;return f===e?(a(i,r),o.removeItem(n)):(a(i,f),o.setItem(n,f))}))},n.createJSONStorage=b,n.freezeAtom=function(n){return e=function(){return t.atom((function(t){return f(t(n))}),(function(t,e,r){return e(n,r)}))},r=n,(u.has(r)?u:u.set(r,e())).get(r);var e,r},n.freezeAtomCreator=function(n){return function(){var t=n.apply(void 0,arguments),e=t.read;return t.read=function(n,t){return f(e(n,t))},t}},n.loadable=function(n){return e=function(){var e=new WeakMap,r=t.atom(0),o=t.atom((function(t,o){var i=o.setSelf;t(r);var a=t(n);if(!(a instanceof Promise))return{state:"hasData",data:a};var u=e.get(a);return u||(e.set(a,h),a.then((function(n){e.set(a,{state:"hasData",data:n})}),(function(n){e.set(a,{state:"hasError",error:n})})).finally(i),h)}),(function(n,t){t(r,(function(n){return n+1}))}));return t.atom((function(n){return n(o)}))},r=n,(y.has(r)?y:y.set(r,e())).get(r);var e,r},n.selectAtom=function(n,e,r){return void 0===r&&(r=Object.is),o=function(){var o=Symbol(),i=function(n){var t=n[0],i=n[1],a=e(t);return i!==o&&r(i,a)?i:a},a=t.atom((function(t){var e=t(a),r=t(n);return r instanceof Promise||e instanceof Promise?Promise.all([r,e]).then(i):i([r,e])}));return a.init=o,a},u=e,f=r,c=i((function(){return new WeakMap}),a,n),l=i((function(){return new WeakMap}),c,u),i(o,l,f);var o,u,f,c,l},n.splitAtom=function(n,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 m=c&&c.atomList[c.keyList.indexOf(f)];if(m)l[u]=m;else{var d=function(t){var e=t(i),r=t(n),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]=v(n)?t.atom(d,(function(t,e,r){var a=t(i),u=t(n),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;e(n,[].concat(u.slice(0,c),[l],u.slice(c+1)))})):t.atom(d)}})),f=c&&c.keyList.length===s.length&&c.keyList.every((function(n,t){return n===s[t]}))?c:{arr:a,atomList:l,keyList:s},r.set(a,f),f},i=t.atom((function(t){var e=t(i),r=t(n);return o(r,null==e?void 0:e.arr)}));i.init=void 0;var a=v(n)?t.atom((function(n){return n(i).atomList}),(function(t,e,r){switch(r.type){case"remove":var o=t(a).indexOf(r.atom);if(o>=0){var i=t(n);e(n,[].concat(i.slice(0,o),i.slice(o+1)))}break;case"insert":var u=r.before?t(a).indexOf(r.before):t(a).length;if(u>=0){var f=t(n);e(n,[].concat(f.slice(0,u),[r.value],f.slice(u)))}break;case"move":var c=t(a).indexOf(r.atom),l=r.before?t(a).indexOf(r.before):t(a).length;if(c>=0&&l>=0){var s=t(n);e(n,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)))}}})):t.atom((function(n){return n(i).atomList}));return a},o=e||s,i=c((function(){return new WeakMap}),l,n),c(r,i,o);var r,o,i},n.unstable_NO_STORAGE_VALUE=d,n.unstable_unwrap=function(n,e){return void 0===e&&(e=k),r=function(){var r=new WeakMap,o=new WeakMap,i=t.atom(0),a=t.atom((function(t,u){var f=u.setSelf;t(i);var c=t(a),l=t(n);if(l===(null==c?void 0:c.p)){if(r.has(l))throw r.get(l);if(o.has(l))return{p:l,v:o.get(l)}}return l!==(null==c?void 0:c.p)&&l.then((function(n){return o.set(l,n)}),(function(n){return r.set(l,n)})).finally(f),c&&"v"in c?{p:l,f:e(c.v)}:{p:l,f:e()}}),(function(n,t){t(i,(function(n){return n+1}))}));return a.init=void 0,t.atom((function(n){var t=n(a);return"v"in t?t.v:t.f}))},o=e,i=w((function(){return new WeakMap}),g,n),w(r,i,o);var r,o,i}}));