jotai
Version:
👻 Primitive and flexible state management for React
2 lines (1 loc) • 2.27 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("jotai/vanilla"),require("jotai/vanilla/internals")):"function"==typeof define&&define.amd?define(["exports","react","jotai/vanilla","jotai/vanilla/internals"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiReact={},e.React,e.jotaiVanilla,e.jotaiVanillaInternals)}(this,(function(e,t,n,r){"use strict";var u=t.createContext(void 0);function i(e){var r=t.useContext(u);return(null==e?void 0:e.store)||r||n.getDefaultStore()}var o=function(e){return"function"==typeof(null==e?void 0:e.then)},a=function(e){e.status="pending",e.then((function(t){e.status="fulfilled",e.value=t}),(function(t){e.status="rejected",e.reason=t}))},f=t.use||function(e){if("pending"===e.status)throw e;if("fulfilled"===e.status)return e.value;throw"rejected"===e.status?e.reason:(a(e),e)},c=new WeakMap,l=function(e,t){var n=c.get(e);return n||(n=new Promise((function(u,i){var a=e,f=function(e){return function(t){a===e&&u(t)}},l=function(e){return function(t){a===e&&i(t)}},s=function(){try{var e=t();o(e)?(c.set(e,n),a=e,e.then(f(e),l(e)),r.INTERNAL_registerAbortHandler(e,s)):u(e)}catch(e){i(e)}};e.then(f(e),l(e)),r.INTERNAL_registerAbortHandler(e,s)})),c.set(e,n)),n};function s(e,n){var r=i(n),u=t.useReducer((function(t){var n=r.get(e);return Object.is(t[0],n)&&t[1]===r&&t[2]===e?t:[n,r,e]}),void 0,(function(){return[r.get(e),r,e]})),c=u[0],s=c[0],v=c[1],d=c[2],g=u[1],p=s;v===r&&d===e||(g(),p=r.get(e));var h=null==n?void 0:n.delay;if(t.useEffect((function(){var t=r.sub(e,(function(){if("number"==typeof h){var t=r.get(e);return o(t)&&a(l(t,(function(){return r.get(e)}))),void setTimeout(g,h)}g()}));return g(),t}),[r,e,h]),t.useDebugValue(p),o(p)){var j=l(p,(function(){return r.get(e)}));return f(j)}return p}function v(e,n){var r=i(n),u=t.useCallback((function(){for(var t=arguments.length,n=new Array(t),u=0;u<t;u++)n[u]=arguments[u];return r.set.apply(r,[e].concat(n))}),[r,e]);return u}e.Provider=function(e){var r=e.children,i=e.store,o=t.useRef(void 0);return i||o.current||(o.current=n.createStore()),t.createElement(u.Provider,{value:i||o.current},r)},e.useAtom=function(e,t){return[s(e,t),v(e,t)]},e.useAtomValue=s,e.useSetAtom=v,e.useStore=i}));