1import { o as __toESM } from "./chunk-6z4oVpB-.js"; 2import { t as require_react } from "./react-CCT1erMQ.js"; 3import { i as registerAbortHandler, n as createStore, r as getDefaultStore } from "./vanilla-CLGkZFy8.js"; 4var import_react = /* @__PURE__ */ __toESM(require_react(), 1); 5var StoreContext = (0, import_react.createContext)(void 0); 6function useStore(options) { 7 const store = (0, import_react.useContext)(StoreContext); 8 return (options == null ? void 0 : options.store) || store || getDefaultStore(); 9} 10function Provider({ children, store }) { 11 const storeRef = (0, import_react.useRef)(void 0); 12 if (!store && !storeRef.current) storeRef.current = createStore(); 13 return (0, import_react.createElement)(StoreContext.Provider, { value: store || storeRef.current }, children); 14} 15var isPromiseLike = (x) => typeof (x == null ? void 0 : x.then) === "function"; 16var attachPromiseStatus = (promise) => { 17 if (!promise.status) { 18 promise.status = "pending"; 19 promise.then((v) => { 20 promise.status = "fulfilled"; 21 promise.value = v; 22 }, (e) => { 23 promise.status = "rejected"; 24 promise.reason = e; 25 }); 26 } 27}; 28var use = import_react.default.use || ((promise) => { 29 if (promise.status === "pending") throw promise; 30 else if (promise.status === "fulfilled") return promise.value; 31 else if (promise.status === "rejected") throw promise.reason; 32 else { 33 attachPromiseStatus(promise); 34 throw promise; 35 } 36}); 37var continuablePromiseMap = /* @__PURE__ */ new WeakMap(); 38var createContinuablePromise = (promise, getValue) => { 39 let continuablePromise = continuablePromiseMap.get(promise); 40 if (!continuablePromise) { 41 continuablePromise = new Promise((resolve, reject) => { 42 let curr = promise; 43 const onFulfilled = (me) => (v) => { 44 if (curr === me) resolve(v); 45 }; 46 const onRejected = (me) => (e) => { 47 if (curr === me) reject(e); 48 }; 49 const onAbort = () => { 50 try { 51 const nextValue = getValue(); 52 if (isPromiseLike(nextValue)) { 53 continuablePromiseMap.set(nextValue, continuablePromise); 54 curr = nextValue; 55 nextValue.then(onFulfilled(nextValue), onRejected(nextValue)); 56 registerAbortHandler(nextValue, onAbort); 57 }
57 else resolve(nextValue); 58 } catch (e) { 59 reject(e); 60 } 61 }; 62 promise.then(onFulfilled(promise), onRejected(promise)); 63 registerAbortHandler(promise, onAbort); 64 }); 65 continuablePromiseMap.set(promise, continuablePromise); 66 } 67 return continuablePromise; 68}; 69function useAtomValue(atom, options) { 70 const { delay, unstable_promiseStatus: promiseStatus = !import_react.default.use } = options || {}; 71 const store = useStore(options); 72 const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] = (0, import_react.useReducer)((prev) => { 73 const nextValue = store.get(atom); 74 if (Object.is(prev[0], nextValue) && prev[1] === store && prev[2] === atom) return prev; 75 return [ 76 nextValue, 77 store, 78 atom 79 ]; 80 }, void 0, () => [ 81 store.get(atom), 82 store, 83 atom 84 ]); 85 let value = valueFromReducer; 86 if (storeFromReducer !== store || atomFromReducer !== atom) { 87 rerender(); 88 value = store.get(atom); 89 } 90 (0, import_react.useEffect)(() => { 91 const unsub = store.sub(atom, () => { 92 if (promiseStatus) try { 93 const value2 = store.get(atom); 94 if (isPromiseLike(value2)) attachPromiseStatus(createContinuablePromise(value2, () => store.get(atom))); 95 } catch (e) {} 96 if (typeof delay === "number") { 97 setTimeout(rerender, delay); 98 return; 99 } 100 rerender(); 101 }); 102 rerender(); 103 return unsub; 104 }, [ 105 store, 106 atom, 107 delay, 108 promiseStatus 109 ]); 110 (0, import_react.useDebugValue)(value); 111 if (isPromiseLike(value)) { 112 const promise = createContinuablePromise(value, () => store.get(atom)); 113 if (promiseStatus) attachPromiseStatus(promise); 114 return use(promise); 115 } 116 return value; 117} 118function useSetAtom(atom, options) { 119 const store = useStore(options); 120 return (0, import_react.useCallback)((...args) => { 121 return store.set(atom, ...args); 122 }, [store, atom]); 123} 124function useAtom(atom, options) { 125 return [useAtomValue(atom, options), useSetAtom(atom, options)]; 126} 127export { useSetAtom as i, useAtom as n, useAtomValue as r, Provider as t }; 128 129//# sourceMappingURL=react-DHanflfZ.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.