UNPKG

18.5 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("jotai"),require("jotai/vanilla/utils"),require("react")):"function"==typeof define&&define.amd?define(["exports","jotai","jotai/vanilla/utils","react"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jotaiUtils={},t.jotai,t.utils,t.React)}(this,(function(t,e,r,n){"use strict";function o(){o=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",u=i.asyncIterator||"@@asyncIterator",c=i.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function l(t,e,r,o){var i=e&&e.prototype instanceof h?e:h,a=Object.create(i.prototype),u=new A(o||[]);return n(a,"_invoke",{value:O(t,r,u)}),a}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=l;var v={};function h(){}function d(){}function m(){}var p={};f(p,a,(function(){return this}));var y=Object.getPrototypeOf,g=y&&y(y(_([])));g&&g!==e&&r.call(g,a)&&(p=g);var b=m.prototype=h.prototype=Object.create(p);function w(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,e){function o(n,i,a,u){var c=s(t[n],t,i);if("throw"!==c.type){var f=c.arg,l=f.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){o("next",t,a,u)}),(function(t){o("throw",t,a,u)})):e.resolve(l).then((function(t){f.value=t,a(f)}),(function(t){return o("throw",t,a,u)}))}u(c.arg)}var i;n(this,"_invoke",{value:function(t,r){function n(){return new e((function(e,n){o(t,r,e,n)}))}return i=i?i.then(n,n):n()}})}function O(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 j()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=L(a,r);if(u){if(u===v)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=s(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===v)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function L(t,e){var r=e.method,n=t.iterator[r];if(void 0===n)return e.delegate=null,"throw"===r&&t.iterator.return&&(e.method="return",e.arg=void 0,L(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),v;var o=s(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,v;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,v):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,v)}function S(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 x(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function A(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(S,this),this.reset(!0)}function _(t){if(t){var e=t[a];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:j}}function j(){return{value:void 0,done:!0}}return d.prototype=m,n(b,"constructor",{value:m,configurable:!0}),n(m,"constructor",{value:d,configurable:!0}),d.displayName=f(m,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===d||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,f(t,c,"GeneratorFunction")),t.prototype=Object.create(b),t},t.awrap=function(t){return{__await:t}},w(E.prototype),f(E.prototype,u,(function(){return this})),t.AsyncIterator=E,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new E(l(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},w(b),f(b,c,"Generator"),f(b,a,(function(){return this})),f(b,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},t.values=_,A.prototype={constructor:A,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(x),!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,v):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),v},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),x(r),v}},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;x(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:_(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),v}},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,n,o={getItem:function(o){var i,a,u=function(t){if(e!==(t=t||"")){try{n=JSON.parse(t)}catch(t){return r.unstable_NO_STORAGE_VALUE}e=t}return n},c=null!=(i=null==(a=t())?void 0:a.getItem(o))?i:null;return c instanceof Promise?c.then(u):u(c)},setItem:function(e,r){var n;return null==(n=t())?void 0:n.setItem(e,JSON.stringify(r))},removeItem:function(e){var r;return null==(r=t())?void 0:r.removeItem(e)}};return"undefined"!=typeof window&&"function"==typeof window.addEventListener&&(o.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)}}),o}var w=b((function(){return"undefined"!=typeof window?window.localStorage:void 0}));function E(t,n,o){void 0===o&&(o=w);var i=function(){var e=o.getItem(t);return e instanceof Promise?e.then((function(t){return t===r.unstable_NO_STORAGE_VALUE?n:t})):e===r.unstable_NO_STORAGE_VALUE?n:e},a=e.atom(o.delayInit?n: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){var c="function"==typeof u?u(e(a)):u;return c===r.RESET?(i(a,n),o.removeItem(t)):(i(a,c),o.setItem(t,c))}))}var O=new WeakMap;var L=f(),S={state:"loading"};Object.defineProperty(t,"useAtomValue",{enumerable:!0,get:function(){return e.useAtomValue}}),Object.defineProperty(t,"useUpdateAtom",{enumerable:!0,get:function(){return e.useSetAtom}}),Object.defineProperty(t,"RESET",{enumerable:!0,get:function(){return r.RESET}}),Object.defineProperty(t,"unstable_NO_STORAGE_VALUE",{enumerable:!0,get:function(){return r.unstable_NO_STORAGE_VALUE}}),t.abortableAtom=function(t,r){return e.atom((function(r){var n=new AbortController,o=t(r,{signal:n.signal});return o instanceof Promise&&e.SECRET_INTERNAL_registerPromiseAbort(o,(function(){return n.abort()})),o}),r)},t.atomFamily=function(t,e){var r=null,n=new Map,o=function o(i){var a;if(void 0===e)a=n.get(i);else for(var u,f=c(n);!(u=f()).done;){var l=u.value,s=l[0],v=l[1];if(e(s,i)){a=v;break}}if(void 0!==a){if(null==r||!r(a[1],i))return a[0];o.remove(i)}var h=t(i);return n.set(i,[h,Date.now()]),h};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 n=Symbol(),o=e.atom(n),i=e.atom((function(e){var r=e(o);return r!==n?r:t(e)}),(function(t,e,a){return a===r.RESET?e(o,n):e(o,"function"==typeof a?a(t(i)):a)}));return i},t.atomWithHash=function(t,e,n){var o=(null==n?void 0:n.serialize)||JSON.stringify,i=(null==n?void 0:n.deserialize)||function(t){try{return JSON.parse(t||"")}catch(t){return r.unstable_NO_STORAGE_VALUE}},u=(null==n?void 0:n.subscribe)||function(t){return window.addEventListener("hashchange",t),function(){window.removeEventListener("hashchange",t)}},c=a({getItem:function(t){if("undefined"==typeof location)return r.unstable_NO_STORAGE_VALUE;var e=new URLSearchParams(location.hash.slice(1)).get(t);return i(e)},setItem:function(t,e){var r=new URLSearchParams(location.hash.slice(1));r.set(t,o(e)),null!=n&&n.replaceState?history.replaceState(null,"",location.pathname+location.search+"#"+r.toString()):location.hash=r.toString()},removeItem:function(t){var e=new URLSearchParams(location.hash.slice(1));e.delete(t),null!=n&&n.replaceState?history.replaceState(null,"",location.pathname+location.search+"#"+e.toString()):location.hash=e.toString()}},(null==n?void 0:n.delayInit)&&{delayInit:!0},{subscribe:function(t,r){return u((function(){var n=new URLSearchParams(location.hash.slice(1)).get(t);r(null!==n?i(n):e)}))}});return E(t,e,c)},t.atomWithObservable=function(t,r){var n=e.atom((function(n){var o,i,a,u=t(n),c=null==(o=(i=u)[Symbol.observable])?void 0:o.call(i);c&&(u=c);var f,l,s,v,h=function(){return new Promise((function(t){a=t}))},d=r&&"initialValue"in r?{d:"function"==typeof r.initialValue?r.initialValue():r.initialValue}:h(),m=function(t){l=t,null==a||a(t),null==f||f(t)},p=function(){return!f},y=function(){s&&(clearTimeout(v),s.unsubscribe()),s=u.subscribe({next:function(t){return m({d:t})},error:function(t){return m({e:t})},complete:function(){}}),p()&&null!=r&&r.unstable_timeout&&(v=setTimeout((function(){s&&(s.unsubscribe(),s=void 0)}),r.unstable_timeout))};y();var g=e.atom(l||d);return g.onMount=function(t){return f=t,l&&t(l),s?clearTimeout(v):y(),function(){f=void 0,s&&(s.unsubscribe(),s=void 0)}},[g,u,h,y,p]}));return e.atom((function(t){var e=t(n),r=t(e[0]);if("e"in r)throw r.e;return r.d}),(function(t,e,r){var o=t(n),i=o[0],a=o[1],u=o[2],c=o[3],f=o[4];if(!("next"in a))throw new Error("observable is not subject");f()&&(e(i,u()),c()),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 n=e.atom(t,(function(e,o,i){var a="function"==typeof i?i(e(n)):i;o(n,a===r.RESET?t:a)}));return n},t.atomWithStorage=E,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 L((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(S,(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,r){var o=n.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,r);return n.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,r){for(var o,i=e.SECRET_INTERNAL_getScopeContext(r),a=n.useContext(i),u=a.s,f=function(t){var e=O.get(t);e||(e=new WeakSet,O.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,r,o){var i=e.useAtom(t,o),a=i[0],u=i[1];return[a,n.useCallback((function(t){u((function(e){return r(e,t)}))}),[u,r])]},t.useResetAtom=function(t,o){var i=e.SECRET_INTERNAL_getScopeContext(o),a=n.useContext(i).s;return n.useCallback((function(){return a.w(t,r.RESET)}),[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()}}));