UNPKG

8.74 kBJavaScriptView Raw
1!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){var r,e;null==(r=(e=n[o]).c)||r.call(e)},f=function n(r,e){var t=r[o].o,i=e[o].o;return t===i||r===i||u(t)&&n(t,e)},c=function(n){var r={o:n,c:null},e=new Promise((function(e){r.c=function(){r.c=null,e()},n.then(r.c,r.c)}));return e[o]=r,e},v=function(n){return"init"in n},l=function(n){var r,t=new WeakMap,l=new WeakMap,s=new Map;if(n)for(var d,p=i(n);!(d=p()).done;){var h=d.value,w=h[0],g={v:h[1],r:0,d:new Map};t.set(w,g)}var m=new WeakMap,y=new WeakMap,E=function(n){var r=y.get(n);return r||(r=new Map,y.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))&&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)||!f(o.p,i)))return o;"p"in o&&a(o.p)}var u={v:e,r:(null==o?void 0:o.r)||0,d:A(n,null==o?void 0:o.d,t)},c=!1;return o&&"v"in o&&Object.is(o.v,e)?!("i"in o)&&(u.d===o.d||u.d.size===o.d.size&&Array.from(u.d.keys()).every((function(n){return o.d.has(n)})))||(c=!0,Promise.resolve().then((function(){N(n)}))):(c=!0,++u.r,u.d.has(r)&&(u.d=new Map(u.d).set(r,u.r))),o&&!c?o:(S(n,r,u),u)},_=function(n,r,e,t,i){var o=b(n,r);if(o){if(i&&(!("p"in o)||!f(o.p,i)))return o;"p"in o&&a(o.p)}var u={e:e,r:(null==o?void 0:o.r)||0,d:A(n,null==o?void 0:o.d,t)};return S(n,r,u),u},j=function(n,r,e,t){var i=b(n,r);if(i&&"p"in i){if(f(i.p,e))return i;a(i.p)}!function(n,r,e){var t=m.get(r);t||(t=new Map,m.set(r,t)),e.then((function(){t.get(n)===e&&(t.delete(n),t.size||m.delete(r))})),t.set(n,e)}(n,r,e);var o={p:e,r:(null==i?void 0:i.r)||0,d:A(n,null==i?void 0:i.d,t)};return S(n,r,o),o},P=function(n,r,e,t){if(e instanceof Promise){var i=c(e.then((function(e){M(n,r,e,t,i)})).catch((function(e){if(e instanceof Promise)return u(e)?e.then((function(){z(n,r,!0)})):e;_(n,r,e,t,i)})));return j(n,r,i,t)}return M(n,r,e,t)},z=function n(r,e,t){if(!t){var i=b(r,e);if(i){if(i.r!==i.i&&"p"in i&&!function(n){return!n[o].c}(i.p))return i;if(i.d.forEach((function(t,i){if(i!==e)if(l.has(i)){var o=b(r,i);o&&o.r===o.i&&n(r,i)}else n(r,i)})),Array.from(i.d).every((function(n){var e=n[0],t=n[1],i=b(r,e);return i&&"v"in i&&i.r===t})))return i}}var u=new Set;try{var a=e.read((function(t){u.add(t);var i=t===e?b(r,t):n(r,t);if(i){if("e"in i)throw i.e;if("p"in i)throw i.p;return i.v}if(v(t))return t.init;throw new Error("no atom init")}));return P(r,e,a,u)}catch(n){if(n instanceof Promise){var f=c(n);return j(r,e,f,u)}return _(r,e,n,u)}},C=function(n,r){return!r.l.size&&(!r.t.size||1===r.t.size&&r.t.has(n))},O=function n(r,t){var i=l.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,{i:t.r});S(n,r,i)}}(r,i),n(r,i))}))},R=function n(r,e,t){var i=!0,o=e.write((function n(e,t){var i=z(r,e);if("e"in i)throw i.e;if("p"in i){if(null!=t&&t.unstable_promise)return i.p.then((function(){return 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(!v(t))throw new Error("atom not writable");var f=function(n){var r=new Set,e=m.get(n);return e&&(m.delete(n),e.forEach((function(n,e){a(n),r.add(e)}))),r}(t);f.forEach((function(n){n!==r&&P(n,t,o)})),b(r,t)!==P(r,t,o)&&O(r,t)}else u=n(r,t,o);return i||N(r),u}),t);return i=!1,o},T=function(n,r,e){var t=R(e,n,r);return N(e),t},x=function n(r,e,t){var i={t:new Set(t&&[t]),l:new Set};if(l.set(e,i),z(void 0,e).d.forEach((function(t,i){var o=l.get(i);o?o.t.add(e):i!==e&&n(r,i,e)})),function(n){return!!n.write}(e)&&e.onMount){var o=e.onMount((function(n){return T(e,n,r)}));r=void 0,o&&(i.u=o)}return i},k=function n(r,e){var t,i=null==(t=l.get(e))?void 0:t.u;i&&i(),l.delete(e);var o=b(r,e);o&&o.d.forEach((function(t,i){if(i!==e){var o=l.get(i);o&&(o.t.delete(e),C(i,o)&&n(r,i))}}))},I=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 o=l.get(t);o&&(o.t.delete(r),C(t,o)&&k(n,t))}})),i.forEach((function(e){var t=l.get(e);t?t.t.add(r):l.has(r)&&x(n,e,r)}))},N=function(n){if(n)E(n).forEach((function(r,e){if(r!==t.get(e)){var i=l.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)&&I(void 0,r,t,null==e?void 0:e.d),!(e&&"i"in e&&t)||"i"in t){var i=l.get(r);null==i||i.l.forEach((function(n){return n()}))}}))}};return(r={}).r=function(n,r){return z(r,n)},r.w=T,r.c=function(n,r){r&&function(n){E(n).forEach((function(r,e){var i=t.get(e);(!i||r.r>i.r||"v"in r&&r.r===i.r&&r.d!==i.d)&&(t.set(e,r),r.d!==(null==i?void 0:i.d)&&I(n,e,r,null==i?void 0:i.d))}))}(r),N(void 0)},r.s=function(n,r,e){var t=function(n,r){var e=l.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=l.get(r);e&&C(r,e)&&k(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];v(u)&&(P(r,u,a),O(r,u))}N(r)},r},s=function(n,r){return{s:r?r(n).SECRET_INTERNAL_store:l(n)}},d=new Map,p=function(n){return d.has(n)||d.set(n,r.createContext(s())),d.get(n)},h=0;function w(n,e){var t=p(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],l=c[1],s=c[2],d=f[1],h=l;return s!==n&&(d(v),h=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(h),h}function g(n,e){var t=p(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 l=s(t,o);if(u){var d=0;l.w=function(n){c((function(r){var e=d?r:{p:r};return n(e),e}))},l.v=f,l.r=function(n){++d,n(),--d}}v.current=l}var h=p(i);return r.createElement(h.Provider,{value:v.current},e)},n.SECRET_INTERNAL_getScopeContext=p,n.atom=function(n,r){var e="atom"+ ++h,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=l(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),[w(n,r),g(n,r)]},n.useAtomValue=w,n.useSetAtom=g,Object.defineProperty(n,"__esModule",{value:!0})}));