UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

2 lines (1 loc) • 9.88 kB
!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(){return r=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},r.apply(this,arguments)}function o(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=new Array(n);e<n;e++)r[e]=t[e];return r}function i(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 o(t,n);var e=Object.prototype.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)?o(t,n):void 0}}(t))||n&&t&&"number"==typeof t.length){e&&(t=e);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[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 a=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},u=new WeakMap;var f=new WeakMap,c=function t(n){if("object"==typeof n&&null!==n){Object.freeze(n);for(var e,r=i(Object.getOwnPropertyNames(n));!(e=r()).done;){t(n[e.value])}return n}};var s=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},l=new WeakMap,v={},m=function(t){return!!t.write};var d=function(t){return"function"==typeof(null==t?void 0:t.then)};function h(t,n){var e,r;void 0===t&&(t=function(){try{return window.localStorage}catch(t){return}});var o={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 d(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)}};return"undefined"!=typeof window&&"function"==typeof window.addEventListener&&window.Storage&&(o.subscribe=function(n,e,r){if(!(t()instanceof window.Storage))return function(){};var o=function(o){if(o.storageArea===t()&&o.key===n){var i;try{i=JSON.parse(o.newValue||"")}catch(t){i=r}e(i)}};return window.addEventListener("storage",o),function(){window.removeEventListener("storage",o)}}),o}var p=h();var b=new WeakMap,y={state:"loading"};var g=function(t,n,e){return(n.has(e)?n:n.set(e,t())).get(e)},w=new WeakMap,k=function(){};t.RESET=e,t.atomFamily=function(t,n){var e=null,r=new Map,o=function o(a){var u;if(void 0===n)u=r.get(a);else for(var f,c=i(r);!(f=c()).done;){var s=f.value,l=s[0],v=s[1];if(n(l,a)){u=v;break}}if(void 0!==u){if(null==e||!e(u[1],a))return u[0];o.remove(a)}var m=t(a);return r.set(a,[m,Date.now()]),m};return o.remove=function(t){if(void 0===n)r.delete(t);else for(var e,o=i(r);!(e=o()).done;){var a=e.value[0];if(n(a,t)){r.delete(a);break}}},o.setShouldRemove=function(t){if(e=t)for(var n,o=i(r);!(n=o()).done;){var a=n.value,u=a[0],f=a[1];e(f[1],u)&&r.delete(u)}},o},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.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,s,l,v,m=function(){return new Promise((function(t){a=t}))},d=e&&"initialValue"in e?{d:"function"==typeof e.initialValue?e.initialValue():e.initialValue}:m(),h=function(t){s=t,null==a||a(t),null==c||c(t)},p=function(){return!c},b=function(){l&&(clearTimeout(v),l.unsubscribe()),l=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(){l&&(l.unsubscribe(),l=void 0)}),e.unstable_timeout))};b();var y=n.atom(s||d);return y.onMount=function(t){return c=t,s&&t(s),l?clearTimeout(v):b(),function(){c=void 0,l&&(l.unsubscribe(),l=void 0)}},[y,u,m,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=p);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 a||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=h,t.freezeAtom=function(t){return e=function(){return n.atom((function(n){return c(n(t))}),(function(n,e,r){return e(t,r)}))},r=t,(f.has(r)?f:f.set(r,e())).get(r);var e,r},t.freezeAtomCreator=function(t){return function(){var n=t.apply(void 0,arguments),e=n.read;return n.read=function(t,n){return c(e.call(this,t,n))},n}},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,y),y)}),(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=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},a=n.atom((function(n){var e=n(a),r=n(t);return r instanceof Promise||e instanceof Promise?Promise.all([r,e]).then(i):i([r,e])}));return a.init=o,a},i=e,f=r,c=a((function(){return new WeakMap}),u,t),s=a((function(){return new WeakMap}),c,i),a(o,s,f);var o,i,f,c,s},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),s=[],l=[];return a.forEach((function(r,u){var f=e?e(r):u;l[u]=f;var v=c&&c.atomList[c.keyList.indexOf(f)];if(v)s[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]};s[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 s="function"==typeof r?r(u[c]):r;Object.is(u[c],s)||e(t,[].concat(u.slice(0,c),[s],u.slice(c+1)))})):n.atom(d)}})),f=c&&c.keyList.length===l.length&&c.keyList.every((function(t,n){return t===l[n]}))?c:{arr:a,atomList:s,keyList:l},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),s=r.before?n(a).indexOf(r.before):n(a).length;if(c>=0&&s>=0){var l=n(t);e(t,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)))}}})):n.atom((function(t){return t(i).atomList}));return a},o=e||v,i=s((function(){return new WeakMap}),l,t),s(r,i,o);var r,o,i},t.unstable_withStorageValidator=function(t){return function(n){return r({},n,{getItem:function(e,r){var o=function(n){return t(n)?n:r},i=n.getItem(e,r);return d(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),s=n(t);if(!(s instanceof Promise))return{v:s};if(s!==(null==c?void 0:c.p)&&("fulfilled"===s.status?o.set(s,s.value):"rejected"===s.status?r.set(s,s.reason):s.then((function(t){return o.set(s,t)}),(function(t){return r.set(s,t)})).finally(f)),r.has(s))throw r.get(s);return o.has(s)?{p:s,v:o.get(s)}:c&&"v"in c?{p:s,f:e(c.v),v:c.v}:{p:s,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}),w,t),g(r,i,o);var r,o,i}}));