UNPKG

jotai

Version:

👻 Next gen state management that will spook you

2 lines (1 loc) • 9.02 kB
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((n="undefined"!=typeof globalThis?globalThis:n||self).jotai={},n.React)}(this,(function(n,r){"use strict";function e(){return e=Object.assign?Object.assign.bind():function(n){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=e[t])}return n},e.apply(this,arguments)}function t(n,r){(null==r||r>n.length)&&(r=n.length);for(var e=0,t=new Array(r);e<r;e++)t[e]=n[e];return t}function i(n,r){var e="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(e)return(e=e.call(n)).next.bind(e);if(Array.isArray(n)||(e=function(n,r){if(n){if("string"==typeof n)return t(n,r);var e=Object.prototype.toString.call(n).slice(8,-1);return"Object"===e&&n.constructor&&(e=n.constructor.name),"Map"===e||"Set"===e?Array.from(n):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?t(n,r):void 0}}(n))||r&&n&&"number"==typeof n.length){e&&(n=e);var i=0;return function(){return i>=n.length?{done:!0}:{done:!1,value:n[i++]}}}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 o=Symbol(),u=function(n){return!!n[o]},a=function(n){return!n[o].c},f=function(n){var r,e=n[o],t=e.b,i=e.c;i&&(i(),null==(r=s.get(t))||r())},c=function n(r,e){var t=r[o].o,i=e[o].o;return t===i||r===i||u(t)&&n(t,e)},v=function(n,r){var e={b:n,o:r,c:null},t=new Promise((function(n){e.c=function(){e.c=null,n()},r.finally(e.c)}));return t[o]=e,t},s=new WeakMap,l=function(n){return"init"in n},d=function(n){var r,t=new WeakMap,o=new WeakMap,s=new Map;if(n)for(var d,p=i(n);!(d=p()).done;){var h=d.value,w=h[0],y={v:h[1],r:0,y:!0,d:new Map};t.set(w,y)}var g=new WeakMap,m=new WeakMap,E=function(n){var r=m.get(n);return r||(r=new Map,m.set(n,r)),r},b=function n(r,e){if(r){var i=E(r),o=i.get(e);return o||((o=n(r.p,e))&&"p"in o&&a(o.p)&&(o=void 0),o&&i.set(e,o)),o}return t.get(e)},S=function(n,r,e){if(n){E(n).set(r,e)}else{var i=t.get(r);t.set(r,e),s.has(r)||s.set(r,i)}},A=function(n,r,e){if(void 0===r&&(r=new Map),!e)return r;var t=new Map,i=!1;return e.forEach((function(e){var o,u=(null==(o=b(n,e))?void 0:o.r)||0;t.set(e,u),r.get(e)!==u&&(i=!0)})),r.size!==t.size||i?t:r},M=function(n,r,e,t,i){var o=b(n,r);if(o){if(i&&(!("p"in o)||!c(o.p,i)))return o;"p"in o&&f(o.p)}var u={v:e,r:(null==o?void 0:o.r)||0,y:!0,d:A(n,null==o?void 0:o.d,t)},a=!(null!=o&&o.y);return o&&"v"in o&&Object.is(o.v,e)?u.d===o.d||u.d.size===o.d.size&&Array.from(u.d.keys()).every((function(n){return o.d.has(n)}))||(a=!0,Promise.resolve().then((function(){I(n)}))):(a=!0,++u.r,u.d.has(r)&&(u.d=new Map(u.d).set(r,u.r))),o&&!a?o:(S(n,r,u),u)},P=function(n,r,e,t,i){var o=b(n,r);if(o){if(i&&(!("p"in o)||!c(o.p,i)))return o;"p"in o&&f(o.p)}var u={e:e,r:((null==o?void 0:o.r)||0)+1,y:!0,d:A(n,null==o?void 0:o.d,t)};return S(n,r,u),u},T=function(n,r,t,i){var o=b(n,r);if(o&&"p"in o){if(c(o.p,t))return o.y?o:e({},o,{y:!0});f(o.p)}!function(n,r,e){var t=g.get(r);t||(t=new Map,g.set(r,t)),e.then((function(){t.get(n)===e&&(t.delete(n),t.size||g.delete(r))})),t.set(n,e)}(n,r,t);var u={p:t,r:((null==o?void 0:o.r)||0)+1,y:!0,d:A(n,null==o?void 0:o.d,i)};return S(n,r,u),u},_=function(n,r,e,t){if(e instanceof Promise){var i=v(e,e.then((function(e){M(n,r,e,t,i)})).catch((function(e){if(e instanceof Promise)return u(e)?e.then((function(){R(n,r,!0)})):e;P(n,r,e,t,i)})));return T(n,r,i,t)}return M(n,r,e,t)},R=function n(r,t,i){if(!i){var u=b(r,t);if(u){if(u.y&&"p"in u&&!a(u.p))return u;if(u.d.forEach((function(e,i){if(i!==t)if(o.has(i)){var u=b(r,i);u&&!u.y&&n(r,i)}else n(r,i)})),Array.from(u.d).every((function(n){var e=n[0],t=n[1],i=b(r,e);return i&&!("p"in i)&&i.r===t})))return u.y?u:e({},u,{y:!0})}}var f=new Set;try{var c=t.read((function(e){f.add(e);var i=e===t?b(r,e):n(r,e);if(i){if("e"in i)throw i.e;if("p"in i)throw i.p;return i.v}if(l(e))return e.init;throw new Error("no atom init")}));return _(r,t,c,f)}catch(n){if(n instanceof Promise){var s=v(n,n);return T(r,t,s,f)}return P(r,t,n,f)}},j=function(n,r){return!r.l.size&&(!r.t.size||1===r.t.size&&r.t.has(n))},C=function n(r,t){var i=o.get(t);null==i||i.t.forEach((function(i){i!==t&&(!function(n,r){var t=b(n,r);if(t){var i=e({},t,{y:!1});S(n,r,i)}}(r,i),n(r,i))}))},z=function n(r,e,t){var i=!0,o=e.write((function n(e,t){var i=R(r,e);if("e"in i)throw i.e;if("p"in i){if(null!=t&&t.unstable_promise)return i.p.then((function(){var o=b(r,e);return o&&"p"in o&&o.p===i.p?new Promise((function(n){return setTimeout(n)})).then((function(){return n(e,t)})):n(e,t)}));throw i.p}if("v"in i)return i.v;throw new Error("no value found")}),(function(t,o){var u;if(t===e){if(!l(t))throw new Error("atom not writable");var a=function(n){var r=new Set,e=g.get(n);return e&&(g.delete(n),e.forEach((function(n,e){f(n),r.add(e)}))),r}(t);a.forEach((function(n){n!==r&&_(n,t,o)})),b(r,t)!==_(r,t,o)&&C(r,t)}else u=n(r,t,o);return i||I(r),u}),t);return i=!1,o},k=function(n,r,e){var t=z(e,n,r);return I(e),t},x=function n(r,e,t){var i={t:new Set(t&&[t]),l:new Set};if(o.set(e,i),R(void 0,e).d.forEach((function(t,i){var u=o.get(i);u?u.t.add(e):i!==e&&n(r,i,e)})),function(n){return!!n.write}(e)&&e.onMount){var u=e.onMount((function(n){return k(e,n,r)}));r=void 0,u&&(i.u=u)}return i},N=function n(r,e){var t,i=null==(t=o.get(e))?void 0:t.u;i&&i(),o.delete(e);var u=b(r,e);u&&("p"in u&&f(u.p),u.d.forEach((function(t,i){if(i!==e){var u=o.get(i);u&&(u.t.delete(e),j(i,u)&&n(r,i))}})))},O=function(n,r,e,t){var i=new Set(e.d.keys());null==t||t.forEach((function(e,t){if(i.has(t))i.delete(t);else{var u=o.get(t);u&&(u.t.delete(r),j(t,u)&&N(n,t))}})),i.forEach((function(e){var t=o.get(e);t?t.t.add(r):o.has(r)&&x(n,e,r)}))},I=function(n){if(n)E(n).forEach((function(r,e){if(r!==t.get(e)){var i=o.get(e);null==i||i.l.forEach((function(r){return r(n)}))}}));else for(;s.size;){var r=Array.from(s);s.clear(),r.forEach((function(n){var r=n[0],e=n[1],t=b(void 0,r);if(t&&t.d!==(null==e?void 0:e.d)&&O(void 0,r,t,null==e?void 0:e.d),!e||e.y||null==t||!t.y){var i=o.get(r);null==i||i.l.forEach((function(n){return n()}))}}))}};return(r={}).r=function(n,r){return R(r,n)},r.w=k,r.c=function(n,r){r&&function(n){E(n).forEach((function(r,e){var i=t.get(e);(!i||r.r>i.r||r.y!==i.y||r.r===i.r&&r.d!==i.d)&&(t.set(e,r),r.d!==(null==i?void 0:i.d)&&O(n,e,r,null==i?void 0:i.d))}))}(r),I(void 0)},r.s=function(n,r,e){var t=function(n,r){var e=o.get(r);return e||(e=x(n,r)),e}(e,n),i=t.l;return i.add(r),function(){i.delete(r),function(n,r){var e=o.get(r);e&&j(r,e)&&N(n,r)}(e,n)}},r.h=function(n,r){for(var e,t=i(n);!(e=t()).done;){var o=e.value,u=o[0],a=o[1];l(u)&&(_(r,u,a),C(r,u))}I(r)},r},p=function(n,r){return{s:r?r(n).SECRET_INTERNAL_store:d(n)}},h=new Map,w=function(n){return h.has(n)||h.set(n,r.createContext(p())),h.get(n)},y=0;function g(n,e){var t=w(e),i=r.useContext(t),o=i.s,u=i.v,a=function(r){var e=o.r(n,r);if("e"in e)throw e.e;if("p"in e)throw e.p;if("v"in e)return e.v;throw new Error("no atom value")},f=r.useReducer((function(r,e){var t=a(e);return Object.is(r[1],t)&&r[2]===n?r:[e,t,n]}),u,(function(r){return[r,a(r),n]})),c=f[0],v=c[0],s=c[1],l=c[2],d=f[1],p=s;return l!==n&&(d(v),p=a(v)),r.useEffect((function(){var r=i.v;r&&o.c(n,r);var e=o.s(n,d,r);return d(r),e}),[o,n,i]),r.useEffect((function(){o.c(n,v)})),r.useDebugValue(p),p}function m(n,e){var t=w(e),i=r.useContext(t),o=i.s,u=i.w;return r.useCallback((function(r){var e=function(e){return o.w(n,r,e)};return u?u(e):e()}),[o,u,n])}n.Provider=function(n){var e=n.children,t=n.initialValues,i=n.scope,o=n.unstable_createStore,u=n.unstable_enableVersionedWrite,a=r.useState({}),f=a[0],c=a[1];r.useEffect((function(){var n=v.current;n.w&&(n.s.c(null,f),delete f.p,n.v=f)}),[f]);var v=r.useRef();if(!v.current){var s=p(t,o);if(u){var l=0;s.w=function(n){c((function(r){var e=l?r:{p:r};return n(e),e}))},s.v=f,s.r=function(n){++l,n(),--l}}v.current=s}var d=w(i);return r.createElement(d.Provider,{value:v.current},e)},n.SECRET_INTERNAL_getScopeContext=w,n.SECRET_INTERNAL_registerPromiseAbort=function(n,r){s.set(n,r)},n.atom=function(n,r){var e="atom"+ ++y,t={toString:function(){return e}};return"function"==typeof n?t.read=n:(t.init=n,t.read=function(n){return n(t)},t.write=function(n,r,e){return r(t,"function"==typeof e?e(n(t)):e)}),r&&(t.write=r),t},n.unstable_createStore=function(n){var r=d(n);return{get:function(n){var e=r.r(n);if("e"in e)throw e.e;if(!("p"in e))return e.v},asyncGet:function n(e){return new Promise((function(t,i){var o=r.r(e);"e"in o?i(o.e):t("p"in o?o.p.then((function(){return n(e)})):o.v)}))},set:function(n,e){return r.w(n,e)},sub:function(n,e){return r.s(n,e)},SECRET_INTERNAL_store:r}},n.useAtom=function(n,r){return"scope"in n&&(console.warn("atom.scope is deprecated. Please do useAtom(atom, scope) instead."),r=n.scope),[g(n,r),m(n,r)]},n.useAtomValue=g,n.useSetAtom=m}));