UNPKG

17.6 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("jotai"),require("react")):"function"==typeof define&&define.amd?define(["exports","jotai","react"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jotaiUtils={},t.jotai,t.React)}(this,(function(t,e,r){"use strict";var n=Symbol();function o(){
2/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
3o=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",u=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function f(t,e,r,n){var o=e&&e.prototype instanceof v?e:v,i=Object.create(o.prototype),a=new O(n||[]);return i._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return A()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=x(a,r);if(u){if(u===s)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=l(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===s)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}(t,r,a),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=f;var s={};function v(){}function h(){}function d(){}var m={};c(m,i,(function(){return this}));var p=Object.getPrototypeOf,y=p&&p(p(E([])));y&&y!==e&&r.call(y,i)&&(m=y);var g=d.prototype=v.prototype=Object.create(m);function b(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){function n(o,i,a,u){var c=l(t[o],t,i);if("throw"!==c.type){var f=c.arg,s=f.value;return s&&"object"==typeof s&&r.call(s,"__await")?e.resolve(s.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):e.resolve(s).then((function(t){f.value=t,a(f)}),(function(t){return n("throw",t,a,u)}))}u(c.arg)}var o;this._invoke=function(t,r){function i(){return new e((function(e,o){n(t,r,e,o)}))}return o=o?o.then(i,i):i()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return s;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return s}var n=l(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,s;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,s):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,s)}function L(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function S(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,this),this.reset(!0)}function E(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:A}}function A(){return{value:void 0,done:!0}}return h.prototype=d,c(g,"constructor",d),c(d,"constructor",h),h.displayName=c(d,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,d):(t.__proto__=d,c(t,u,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},b(w.prototype),c(w.prototype,a,(function(){return this})),t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(f(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},b(g),c(g,u,"Generator"),c(g,i,(function(){return this})),c(g,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=E,O.prototype={constructor:O,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(S),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(u&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,s):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),s},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),s}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;S(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:E(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),s}},t}function i(t,e,r,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function a(){return a=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},a.apply(this,arguments)}function u(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function c(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return u(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?u(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}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 f=function(){var t=new WeakMap;return function(e,r){var n=function(t,e){do{var r=e,n=r[0],o=r.slice(1),i=t.get(n);if(!i)return;if(!o.length)return i[1];t=i[0],e=o}while(e.length)}(t,r);if(n)return n;var o=e();return function(t,e,r){do{var n=e,o=n[0],i=n.slice(1),a=t.get(o);if(a||(a=[new WeakMap],t.set(o,a)),!i.length)return void(a[1]=r);t=a[0],e=i}while(e.length)}(t,r,o),o}},l=f();var s=f(),v=function t(e){if("object"==typeof e&&null!==e){Object.freeze(e);for(var r,n=c(Object.getOwnPropertyNames(e));!(r=n()).done;){t(e[r.value])}return e}};var h=f(),d=function(t){return!!t.write};var m=f(),p=e.atom((function(){return[]}));var y=function(t){return Array.isArray(t)?t:Object.getOwnPropertyNames(t).map((function(e){return t[e]}))},g=function(t,e){return Array.isArray(t)?e:Object.getOwnPropertyNames(t).reduce((function(t,r,n){var o;return a({},t,((o={})[r]=e[n],o))}),{})};function b(t){var e,r;return{getItem:function(n){var o=function(t){return e!==(t=t||"")&&(r=JSON.parse(t),e=t),r},i=t().getItem(n);return i instanceof Promise?i.then(o):o(i)},setItem:function(e,r){return t().setItem(e,JSON.stringify(r))},removeItem:function(e){return t().removeItem(e)}}}var w=b((function(){return localStorage}));function x(t,r,o){void 0===o&&(o=w);var i=function(){try{var e=o.getItem(t);return e instanceof Promise?e.catch((function(){return r})):e}catch(t){return r}},a=e.atom(o.delayInit?r:i());return a.onMount=function(e){var r;if(o.subscribe&&(r=o.subscribe(t,e),e(i())),o.delayInit){var n=i();n instanceof Promise?n.then(e):e(n)}return r},e.atom((function(t){return t(a)}),(function(e,i,u){if(u===n)return i(a,r),o.removeItem(t);var c="function"==typeof u?u(e(a)):u;return i(a,c),o.setItem(t,c)}))}w.subscribe=function(t,e){var r=function(r){r.key===t&&r.newValue&&e(JSON.parse(r.newValue))};return window.addEventListener("storage",r),function(){window.removeEventListener("storage",r)}};var L=new WeakMap;var S=f(),O={state:"loading"};Object.defineProperty(t,"useAtomValue",{enumerable:!0,get:function(){return e.useAtomValue}}),Object.defineProperty(t,"useUpdateAtom",{enumerable:!0,get:function(){return e.useSetAtom}}),t.RESET=n,t.atomFamily=function(t,e){var r=null,n=new Map,o=function(o){var i;if(void 0===e)i=n.get(o);else for(var a,u=c(n);!(a=u()).done;){var f=a.value,l=f[0],s=f[1];if(e(l,o)){i=s;break}}if(void 0!==i){if(null==r||!r(i[1],o))return i[0];n.delete(o)}var v=t(o);return n.set(o,[v,Date.now()]),v};return o.remove=function(t){if(void 0===e)n.delete(t);else for(var r,o=c(n);!(r=o()).done;){var i=r.value[0];if(e(i,t)){n.delete(i);break}}},o.setShouldRemove=function(t){if(r=t)for(var e,o=c(n);!(e=o()).done;){var i=e.value,a=i[0],u=i[1];r(u[1],a)&&n.delete(a)}},o},t.atomWithDefault=function(t){var r=Symbol(),o=e.atom(r),i=e.atom((function(e){var n=e(o);return n!==r?n:t(e)}),(function(t,e,a){return e(o,a===n?r:"function"==typeof a?a(t(i)):a)}));return i},t.atomWithHash=function(t,e,r){var n=(null==r?void 0:r.serialize)||JSON.stringify,o=(null==r?void 0:r.deserialize)||JSON.parse,i=(null==r?void 0:r.subscribe)||function(t){return window.addEventListener("hashchange",t),function(){window.removeEventListener("hashchange",t)}},u=a({getItem:function(t){var e=new URLSearchParams(location.hash.slice(1)).get(t);if(null===e)throw new Error("no value stored");return o(e)},setItem:function(t,e){var o=new URLSearchParams(location.hash.slice(1));o.set(t,n(e)),null!=r&&r.replaceState?history.replaceState(null,"","#"+o.toString()):location.hash=o.toString()},removeItem:function(t){var e=new URLSearchParams(location.hash.slice(1));e.delete(t),null!=r&&r.replaceState?history.replaceState(null,"","#"+e.toString()):location.hash=e.toString()}},(null==r?void 0:r.delayInit)&&{delayInit:!0},{subscribe:function(t,r){return i((function(){var n=new URLSearchParams(location.hash.slice(1)).get(t);r(null!==n?o(n):e)}))}});return x(t,e,u)},t.atomWithObservable=function(t,r){var n=e.atom((function(n){var o,i,a=t(n),u=null==(o=(i=a)[Symbol.observable])?void 0:o.call(i);u&&(a=u);var c,f=Symbol(),l=null,s=void 0===(null==r?void 0:r.initialValue)?new Promise((function(t){l=t})):void 0,v=!1,h=f,d=!0,m=function(t){void 0!==(null==r?void 0:r.initialValue)||v?h=t:(d&&(s=t),null==l||l(t),v=!0,l=null)},p=function(t){m(t)},y=function(t){m(Promise.reject(t))},g=null;void 0!==(null==r?void 0:r.initialValue)?c=function(t){var e=t.initialValue;return e instanceof Function?e():e}(r):(g=a.subscribe(p,y),c=s),d=!1;var b=e.atom(c);return b.onMount=function(t){return m=t,h!==f&&t(h),g||(g=a.subscribe(p,y)),function(){var t;null==(t=g)||t.unsubscribe(),g=null}},{dataAtom:b,observable:a}}));return e.atom((function(t){var e=t(n);return t(e.dataAtom)}),(function(t,e,r){var o=t(n),i=o.dataAtom,a=o.observable;if(!("next"in a))throw new Error("observable is not subject");var u=null;u=a.subscribe((function(t){var r;e(i,t),null==(r=u)||r.unsubscribe()})),a.next(r)}))},t.atomWithReducer=function(t,r){var n=e.atom(t,(function(t,e,o){return e(n,r(t(n),o))}));return n},t.atomWithReset=function(t){var r=e.atom(t,(function(e,o,i){o(r,i===n?t:"function"==typeof i?i(e(r)):i)}));return r},t.atomWithStorage=x,t.createJSONStorage=b,t.freezeAtom=function(t){return s((function(){return e.atom((function(e){return v(e(t))}),(function(e,r,n){return r(t,n)}))}),[t])},t.freezeAtomCreator=function(t){return function(){var e=t.apply(void 0,arguments),r=e.read;return e.read=function(t){return v(r(t))},e}},t.loadable=function(t){return S((function(){var r=new WeakMap,n=e.atom((function(n){var a;try{var u=n(t);return e.atom({state:"hasData",data:u})}catch(t){if(!(t instanceof Promise))return e.atom({state:"hasError",error:t});a=t}var c=r.get(a);if(c)return c;var f,l,s=e.atom(O,(f=o().mark((function e(r,n){var i;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,r(t,{unstable_promise:!0});case 3:i=e.sent,n(s,{state:"hasData",data:i}),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),n(s,{state:"hasError",error:e.t0});case 10:case"end":return e.stop()}}),e,null,[[0,7]])})),l=function(){var t=this,e=arguments;return new Promise((function(r,n){var o=f.apply(t,e);function a(t){i(o,r,n,a,u,"next",t)}function u(t){i(o,r,n,a,u,"throw",t)}a(void 0)}))},function(t,e){return l.apply(this,arguments)}));return s.onMount=function(t){t()},r.set(a,s),s}));return e.atom((function(t){var e=t(n);return t(e)}))}),[t])},t.selectAtom=function(t,r,n){return void 0===n&&(n=Object.is),l((function(){var o=e.atom((function(){return{}}));return e.atom((function(e){var i=r(e(t)),a=e(o);return"prev"in a&&n(a.prev,i)?a.prev:(a.prev=i,i)}))}),[t,r,n])},t.splitAtom=function(t,r){return h((function(){var n=new WeakMap,o=function o(a,u){var c=n.get(a);if(c)return c;var f=u&&n.get(u),l=[],s=[];return a.forEach((function(n,u){var c=r?r(n):u;s[u]=c;var v=f&&f.atomList[f.keyList.indexOf(c)];if(v)l[u]=v;else{var h=function(e){var r=e(i),n=e(t),u=o(n,r.prev).keyList.indexOf(c);if(u<0||u>=n.length){var f=a[o(a).keyList.indexOf(c)];if(f)return f;throw new Error("splitAtom: index out of bounds for read")}return n[u]};l[u]=d(t)?e.atom(h,(function(e,r,n){var a=e(i),u=e(t),f=o(u,a.prev).keyList.indexOf(c);if(f<0||f>=u.length)throw new Error("splitAtom: index out of bounds for write");var l="function"==typeof n?n(u[f]):n;r(t,[].concat(u.slice(0,f),[l],u.slice(f+1)))})):e.atom(h)}})),c=f&&f.keyList.length===s.length&&f.keyList.every((function(t,e){return t===s[e]}))?f:{atomList:l,keyList:s},n.set(a,c),c},i=e.atom((function(){return{}})),a=function(e){var r=e(i),n=e(t),a=o(n,r.prev);return r.prev=n,a.atomList},u=d(t)?e.atom(a,(function(e,r,n){switch("read"in n&&(console.warn("atomToRemove is deprecated. use action with type"),n={type:"remove",atom:n}),n.type){case"remove":var o=e(u).indexOf(n.atom);if(o>=0){var i=e(t);r(t,[].concat(i.slice(0,o),i.slice(o+1)))}break;case"insert":var a=n.before?e(u).indexOf(n.before):e(u).length;if(a>=0){var c=e(t);r(t,[].concat(c.slice(0,a),[n.value],c.slice(a)))}break;case"move":var f=e(u).indexOf(n.atom),l=n.before?e(u).indexOf(n.before):e(u).length;if(f>=0&&l>=0){var s=e(t);r(t,f<l?[].concat(s.slice(0,f),s.slice(f+1,l),[s[f]],s.slice(l)):[].concat(s.slice(0,l),[s[f]],s.slice(l,f),s.slice(f+1)))}}})):e.atom(a);return u}),r?[t,r]:[t])},t.useAtomCallback=function(t,n){var o=r.useMemo((function(){return e.atom(null,(function(e,r,n){var o=n[0],i=n[1],a=n[2];try{i(t(e,r,o))}catch(t){a(t)}}))}),[t]),i=e.useSetAtom(o,n);return r.useCallback((function(t){var e=!0,r={},n=new Promise((function(n,o){i([t,function(t){e?r={v:t}:n(t)},function(t){e?r={e:t}:o(t)}])}));if(e=!1,"e"in r)throw r.e;return"v"in r?r.v:n}),[i])},t.useHydrateAtoms=function(t,n){for(var o,i=e.SECRET_INTERNAL_getScopeContext(n),a=r.useContext(i),u=a.s,f=function(t){var e=L.get(t);e||(e=new WeakSet,L.set(t,e));return e}(a),l=[],s=c(t);!(o=s()).done;){var v=o.value,h=v[0];f.has(h)||(f.add(h),l.push(v))}l.length&&u.h(l)},t.useReducerAtom=function(t,n,o){var i=e.useAtom(t,o),a=i[0],u=i[1];return[a,r.useCallback((function(t){u((function(e){return n(e,t)}))}),[u,n])]},t.useResetAtom=function(t,o){var i=e.SECRET_INTERNAL_getScopeContext(o),a=r.useContext(i).s;return r.useCallback((function(){return a.w(t,n)}),[a,t])},t.waitForAll=function(t){var r=function(){var r=y(t);return e.atom((function(e){var n=[],o=r.map((function(t,r){try{return e(t)}catch(t){if(!(t instanceof Promise))throw t;n[r]=t}}));if(n.length)throw Promise.all(n);return g(t,o)}))};return Array.isArray(t)?t.length?m(r,t):p:r()},Object.defineProperty(t,"__esModule",{value:!0})}));