UNPKG

jotai

Version:

👻 Next gen state management that will spook you

2 lines (1 loc) • 8.18 kB
!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)},s=new WeakMap,l={},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=new WeakMap;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 s=f.value,l=s[0],v=s[1];if(t(l,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,s,l,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){s=n,null==a||a(n),null==c||c(n)},p=function(){return!c},y=function(){l&&(clearTimeout(v),l.unsubscribe()),l=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(){l&&(l.unsubscribe(),l=void 0)}),e.unstable_timeout))};y();var h=t.atom(s||d);return h.onMount=function(n){return c=n,s&&n(s),l?clearTimeout(v):y(),function(){c=void 0,l&&(l.unsubscribe(),l=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=t.atom((function(){return{}}));return t.atom((function(t){var i=t(o),a=function(n){var t=e(n);return"prev"in i&&r(i.prev,t)?i.prev:i.prev=t},u=t(n);return u instanceof Promise?u.then(a):a(u)}))},u=e,f=r,c=i((function(){return new WeakMap}),a,n),s=i((function(){return new WeakMap}),c,u),i(o,s,f);var o,u,f,c,s},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),s=[],l=[];return a.forEach((function(r,u){var f=e?e(r):u;l[u]=f;var m=c&&c.atomList[c.keyList.indexOf(f)];if(m)s[u]=m;else{var d=function(t){var e=t(i),r=t(n),u=o(r,e.prev).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]};s[u]=v(n)?t.atom(d,(function(t,e,r){var a=t(i),u=t(n),c=o(u,a.prev).keyList.indexOf(f);if(c<0||c>=u.length)throw new Error("splitAtom: index out of bounds for write");var s="function"==typeof r?r(u[c]):r;e(n,[].concat(u.slice(0,c),[s],u.slice(c+1)))})):t.atom(d)}})),f=c&&c.keyList.length===l.length&&c.keyList.every((function(n,t){return n===l[t]}))?c:{atomList:s,keyList:l},r.set(a,f),f},i=t.atom((function(){return{}})),a=function(t){var e=t(i),r=t(n),a=o(r,e.prev);return e.prev=r,a.atomList},u=v(n)?t.atom(a,(function(t,e,r){switch(r.type){case"remove":var o=t(u).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 a=r.before?t(u).indexOf(r.before):t(u).length;if(a>=0){var f=t(n);e(n,[].concat(f.slice(0,a),[r.value],f.slice(a)))}break;case"move":var c=t(u).indexOf(r.atom),s=r.before?t(u).indexOf(r.before):t(u).length;if(c>=0&&s>=0){var l=t(n);e(n,c<s?[].concat(l.slice(0,c),l.slice(c+1,s),[l[c]],l.slice(s)):[].concat(l.slice(0,s),[l[c]],l.slice(s,c),l.slice(c+1)))}}})):t.atom(a);return u},o=e||l,i=c((function(){return new WeakMap}),s,n),c(r,i,o);var r,o,i},n.unstable_NO_STORAGE_VALUE=d,n.unstable_unwrap=function(n,e){return r=function(){var r=t.atom((function(){return{}})),o=t.atom(0),i=t.atom((function(t,i){var a=i.setSelf;t(o);var u=t(r),f=t(n);if(u.p!==f&&(f.then((function(n){return u.v=n}),(function(n){return u.e=n})).finally(a),u.p=f),"e"in u)throw u.e;return"v"in u?u.v:e}),(function(n,t){t(o,(function(n){return n+1}))}));return t.atom((function(n){return n(i)}))},o=n,(w.has(o)?w:w.set(o,r())).get(o);var r,o}}));