UNPKG

10.8 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai"),require("react")):"function"==typeof define&&define.amd?define(["exports","jotai","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiUtils={},e.jotai,e.React)}(this,(function(e,t,n){"use strict";var r=Symbol();function o(e,t,n,r,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void n(e)}u.done?t(c):Promise.resolve(c).then(r,o)}function a(){return a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},a.apply(this,arguments)}function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function u(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return i(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[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 c=function(){var e=new WeakMap;return function(t,n){var r=function(e,t){do{var n=t,r=n[0],o=n.slice(1),a=e.get(r);if(!a)return;if(!o.length)return a[1];e=a[0],t=o}while(t.length)}(e,n);if(r)return r;var o=t();return function(e,t,n){do{var r=t,o=r[0],a=r.slice(1),i=e.get(o);if(i||(i=[new WeakMap],e.set(o,i)),!a.length)return void(i[1]=n);e=i[0],t=a}while(t.length)}(e,n,o),o}},f=c();var s=c(),l=function e(t){if("object"==typeof t&&null!==t){Object.freeze(t);for(var n,r=u(Object.getOwnPropertyNames(t));!(n=r()).done;){e(t[n.value])}return t}};var v=c(),m=function(e){return!!e.write};var d=c();var h=function(e){return Array.isArray(e)?e:Object.getOwnPropertyNames(e).map((function(t){return e[t]}))},p=function(e,t){return Array.isArray(e)?t:Object.getOwnPropertyNames(e).reduce((function(e,n,r){var o;return a({},e,((o={})[n]=t[r],o))}),{})};function b(e){var t,n;return{getItem:function(r){var o=function(e){return t!==(e=e||"")&&(n=JSON.parse(e),t=e),n},a=e().getItem(r);return a instanceof Promise?a.then(o):o(a)},setItem:function(t,n){return e().setItem(t,JSON.stringify(n))},removeItem:function(t){return e().removeItem(t)}}}var y=b((function(){return localStorage}));function g(e,n,o){void 0===o&&(o=y);var a=function(){try{var t=o.getItem(e);return t instanceof Promise?t.catch((function(){return n})):t}catch(e){return n}},i=t.atom(o.delayInit?n:a());return i.onMount=function(t){var n;if(o.subscribe&&(n=o.subscribe(e,t),t(a())),o.delayInit){var r=a();r instanceof Promise?r.then(t):t(r)}return n},t.atom((function(e){return e(i)}),(function(t,a,u){if(u===r)return a(i,n),o.removeItem(e);var c="function"==typeof u?u(t(i)):u;return a(i,c),o.setItem(e,c)}))}y.subscribe=function(e,t){var n=function(n){n.key===e&&n.newValue&&t(JSON.parse(n.newValue))};return window.addEventListener("storage",n),function(){window.removeEventListener("storage",n)}};var w=new WeakMap;var S=c(),A={state:"loading"};Object.defineProperty(e,"useAtomValue",{enumerable:!0,get:function(){return t.useAtomValue}}),Object.defineProperty(e,"useUpdateAtom",{enumerable:!0,get:function(){return t.useSetAtom}}),e.RESET=r,e.atomFamily=function(e,t){var n=null,r=new Map,o=function(o){var a;if(void 0===t)a=r.get(o);else for(var i,c=u(r);!(i=c()).done;){var f=i.value,s=f[0],l=f[1];if(t(s,o)){a=l;break}}if(void 0!==a){if(null==n||!n(a[1],o))return a[0];r.delete(o)}var v=e(o);return r.set(o,[v,Date.now()]),v};return o.remove=function(e){if(void 0===t)r.delete(e);else for(var n,o=u(r);!(n=o()).done;){var a=n.value[0];if(t(a,e)){r.delete(a);break}}},o.setShouldRemove=function(e){if(n=e)for(var t,o=u(r);!(t=o()).done;){var a=t.value,i=a[0],c=a[1];n(c[1],i)&&r.delete(i)}},o},e.atomWithDefault=function(e){var n=Symbol(),o=t.atom(n),a=t.atom((function(t){var r=t(o);return r!==n?r:e(t)}),(function(e,t,i){return t(o,i===r?n:"function"==typeof i?i(e(a)):i)}));return a},e.atomWithHash=function(e,t,n){var r=(null==n?void 0:n.serialize)||JSON.stringify,o=(null==n?void 0:n.deserialize)||JSON.parse,i=(null==n?void 0:n.subscribe)||function(e){return window.addEventListener("hashchange",e),function(){window.removeEventListener("hashchange",e)}},u=a({getItem:function(e){var t=new URLSearchParams(location.hash.slice(1)).get(e);if(null===t)throw new Error("no value stored");return o(t)},setItem:function(e,t){var o=new URLSearchParams(location.hash.slice(1));o.set(e,r(t)),null!=n&&n.replaceState?history.replaceState(null,"","#"+o.toString()):location.hash=o.toString()},removeItem:function(e){var t=new URLSearchParams(location.hash.slice(1));t.delete(e),null!=n&&n.replaceState?history.replaceState(null,"","#"+t.toString()):location.hash=t.toString()}},(null==n?void 0:n.delayInit)&&{delayInit:!0},{subscribe:function(e,n){return i((function(){var r=new URLSearchParams(location.hash.slice(1)).get(e);n(null!==r?o(r):t)}))}});return g(e,t,u)},e.atomWithObservable=function(e,n){var r=t.atom((function(r){var o,a,i=e(r),u=null==(o=(a=i)[Symbol.observable])?void 0:o.call(a);u&&(i=u);var c,f=t.atom(null!=n&&n.initialValue?function(e){var t=e.initialValue;return t instanceof Function?t():t}(n):(c=i,new Promise((function(e,t){var n=!1,r=c.subscribe({next:function(t){e(t),n=!0,r&&r.unsubscribe()},error:t,complete:function(){t()}});n&&r.unsubscribe()})))),s=function(){throw new Error("setting data without mount")},l=function(e){s(e)},v=function(e){s(Promise.reject(e))},m=null;return f.onMount=function(e){return s=e,m||(m=i.subscribe(l,v)),function(){var e;null==(e=m)||e.unsubscribe(),m=null}},{dataAtom:f,observable:i}}));return t.atom((function(e){var t=e(r);return e(t.dataAtom)}),(function(e,t,n){var o=e(r).observable;if(!("next"in o))throw new Error("observable is not subject");o.next(n)}))},e.atomWithReducer=function(e,n){var r=t.atom(e,(function(e,t,o){return t(r,n(e(r),o))}));return r},e.atomWithReset=function(e){var n=t.atom(e,(function(t,o,a){o(n,a===r?e:"function"==typeof a?a(t(n)):a)}));return n},e.atomWithStorage=g,e.createJSONStorage=b,e.freezeAtom=function(e){return s((function(){return t.atom((function(t){return l(t(e))}),(function(t,n,r){return n(e,r)}))}),[e])},e.freezeAtomCreator=function(e){return function(){var t=e.apply(void 0,arguments),n=t.read;return t.read=function(e){return l(n(e))},t}},e.loadable=function(e){return S((function(){var n=new WeakMap,r=t.atom((function(r){var a;try{var i=r(e);return t.atom({state:"hasData",data:i})}catch(e){if(!(e instanceof Promise))return t.atom({state:"hasError",error:e});a=e}var u=n.get(a);if(u)return u;var c,f,s=t.atom(A,(c=regeneratorRuntime.mark((function t(n,r){var o;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,n(e,{unstable_promise:!0});case 3:o=t.sent,r(s,{state:"hasData",data:o}),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),r(s,{state:"hasError",error:t.t0});case 10:case"end":return t.stop()}}),t,null,[[0,7]])})),f=function(){var e=this,t=arguments;return new Promise((function(n,r){var a=c.apply(e,t);function i(e){o(a,n,r,i,u,"next",e)}function u(e){o(a,n,r,i,u,"throw",e)}i(void 0)}))},function(e,t){return f.apply(this,arguments)}));return s.onMount=function(e){e()},n.set(a,s),s}));return t.atom((function(e){var t=e(r);return e(t)}))}),[e])},e.selectAtom=function(e,n,r){return void 0===r&&(r=Object.is),f((function(){var o=t.atom((function(){return{}}));return t.atom((function(t){var a=n(t(e)),i=t(o);return"prev"in i&&r(i.prev,a)?i.prev:(i.prev=a,a)}))}),[e,n,r])},e.splitAtom=function(e,n){return v((function(){var r=new WeakMap,o=function o(i,u){var c=r.get(i);if(c)return c;var f=u&&r.get(u),s=[],l=[];return i.forEach((function(r,u){var c=n?n(r):u;l[u]=c;var v=f&&f.atomList[f.keyList.indexOf(c)];if(v)s[u]=v;else{var d=function(t){var n=t(a),r=t(e),u=o(r,n.prev).keyList.indexOf(c);if(u<0||u>=r.length){var f=i[o(i).keyList.indexOf(c)];if(f)return f;throw new Error("splitAtom: index out of bounds for read")}return r[u]};s[u]=m(e)?t.atom(d,(function(t,n,r){var i=t(a),u=t(e),f=o(u,i.prev).keyList.indexOf(c);if(f<0||f>=u.length)throw new Error("splitAtom: index out of bounds for write");var s="function"==typeof r?r(u[f]):r;n(e,[].concat(u.slice(0,f),[s],u.slice(f+1)))})):t.atom(d)}})),c=f&&f.keyList.length===l.length&&f.keyList.every((function(e,t){return e===l[t]}))?f:{atomList:s,keyList:l},r.set(i,c),c},a=t.atom((function(){return{}})),i=function(t){var n=t(a),r=t(e),i=o(r,n.prev);return n.prev=r,i.atomList},u=m(e)?t.atom(i,(function(t,n,r){switch("read"in r&&(console.warn("atomToRemove is deprecated. use action with type"),r={type:"remove",atom:r}),r.type){case"remove":var o=t(u).indexOf(r.atom);if(o>=0){var a=t(e);n(e,[].concat(a.slice(0,o),a.slice(o+1)))}break;case"insert":var i=r.before?t(u).indexOf(r.before):t(u).length;if(i>=0){var c=t(e);n(e,[].concat(c.slice(0,i),[r.value],c.slice(i)))}break;case"move":var f=t(u).indexOf(r.atom),s=r.before?t(u).indexOf(r.before):t(u).length;if(f>=0&&s>=0){var l=t(e);n(e,f<s?[].concat(l.slice(0,f),l.slice(f+1,s),[l[f]],l.slice(s)):[].concat(l.slice(0,s),[l[f]],l.slice(s,f),l.slice(f+1)))}}})):t.atom(i);return u}),n?[e,n]:[e])},e.useAtomCallback=function(e,r){var o=n.useMemo((function(){return t.atom(null,(function(t,n,r){var o=r[0],a=r[1],i=r[2];try{a(e(t,n,o))}catch(e){i(e)}}))}),[e]),a=t.useSetAtom(o,r);return n.useCallback((function(e){var t=!0,n={},r=new Promise((function(r,o){a([e,function(e){t?n={v:e}:r(e)},function(e){t?n={e:e}:o(e)}])}));if(t=!1,"e"in n)throw n.e;return"v"in n?n.v:r}),[a])},e.useHydrateAtoms=function(e,r){for(var o,a=t.SECRET_INTERNAL_getScopeContext(r),i=n.useContext(a),c=i.s,f=function(e){var t=w.get(e);t||(t=new WeakSet,w.set(e,t));return t}(i),s=[],l=u(e);!(o=l()).done;){var v=o.value,m=v[0];f.has(m)||(f.add(m),s.push(v))}s.length&&c.h(s)},e.useReducerAtom=function(e,r,o){var a=t.useAtom(e,o),i=a[0],u=a[1];return[i,n.useCallback((function(e){u((function(t){return r(t,e)}))}),[u,r])]},e.useResetAtom=function(e,o){var a=t.SECRET_INTERNAL_getScopeContext(o),i=n.useContext(a).s;return n.useCallback((function(){return i.w(e,r)}),[i,e])},e.waitForAll=function(e){var n=function(){var n=h(e);return t.atom((function(t){var r=[],o=n.map((function(e,n){try{return t(e)}catch(e){if(!(e instanceof Promise))throw e;r[n]=e}}));if(r.length)throw Promise.all(r);return p(e,o)}))};return Array.isArray(e)?d(n,e):n()},Object.defineProperty(e,"__esModule",{value:!0})}));