UNPKG

jotai

Version:

👻 Next gen state management that will spook you

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