1 | import { jsx } from 'react/jsx-runtime';
|
2 | import ReactExports, { createContext, useContext, useRef, useReducer, useEffect, useDebugValue, useCallback } from 'react';
|
3 | import { getDefaultStore, createStore } from 'jotai/vanilla';
|
4 |
|
5 | const StoreContext = createContext(void 0);
|
6 | const useStore = (options) => {
|
7 | const store = useContext(StoreContext);
|
8 | return (options == null ? void 0 : options.store) || store || getDefaultStore();
|
9 | };
|
10 | const Provider = ({
|
11 | children,
|
12 | store
|
13 | }) => {
|
14 | const storeRef = useRef();
|
15 | if (!store && !storeRef.current) {
|
16 | storeRef.current = createStore();
|
17 | }
|
18 | return jsx(StoreContext.Provider, { value: store || storeRef.current, children });
|
19 | };
|
20 |
|
21 | const isPromise = (x) => x instanceof Promise;
|
22 | const use = ReactExports.use || ((promise) => {
|
23 | if (promise.status === "pending") {
|
24 | throw promise;
|
25 | } else if (promise.status === "fulfilled") {
|
26 | return promise.value;
|
27 | } else if (promise.status === "rejected") {
|
28 | throw promise.reason;
|
29 | } else {
|
30 | promise.status = "pending";
|
31 | promise.then(
|
32 | (v) => {
|
33 | promise.status = "fulfilled";
|
34 | promise.value = v;
|
35 | },
|
36 | (e) => {
|
37 | promise.status = "rejected";
|
38 | promise.reason = e;
|
39 | }
|
40 | );
|
41 | throw promise;
|
42 | }
|
43 | });
|
44 | function useAtomValue(atom, options) {
|
45 | const store = useStore(options);
|
46 | const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] = useReducer(
|
47 | (prev) => {
|
48 | const nextValue = store.get(atom);
|
49 | if (Object.is(prev[0], nextValue) && prev[1] === store && prev[2] === atom) {
|
50 | return prev;
|
51 | }
|
52 | return [nextValue, store, atom];
|
53 | },
|
54 | void 0,
|
55 | () => [store.get(atom), store, atom]
|
56 | );
|
57 | let value = valueFromReducer;
|
58 | if (storeFromReducer !== store || atomFromReducer !== atom) {
|
59 | rerender();
|
60 | value = store.get(atom);
|
61 | }
|
62 | const delay = options == null ? void 0 : options.delay;
|
63 | useEffect(() => {
|
64 | const unsub = store.sub(atom, () => {
|
65 | if (typeof delay === "number") {
|
66 | setTimeout(rerender, delay);
|
67 | return;
|
68 | }
|
69 | rerender();
|
70 | });
|
71 | rerender();
|
72 | return unsub;
|
73 | }, [store, atom, delay]);
|
74 | useDebugValue(value);
|
75 | return isPromise(value) ? use(value) : value;
|
76 | }
|
77 |
|
78 | function useSetAtom(atom, options) {
|
79 | const store = useStore(options);
|
80 | const setAtom = useCallback(
|
81 | (...args) => {
|
82 | if (process.env.NODE_ENV !== "production" && !("write" in atom)) {
|
83 | throw new Error("not writable atom");
|
84 | }
|
85 | return store.set(atom, ...args);
|
86 | },
|
87 | [store, atom]
|
88 | );
|
89 | return setAtom;
|
90 | }
|
91 |
|
92 | function useAtom(atom, options) {
|
93 | return [
|
94 | useAtomValue(atom, options),
|
95 | useSetAtom(atom, options)
|
96 | ];
|
97 | }
|
98 |
|
99 | export { Provider, useAtom, useAtomValue, useSetAtom, useStore };
|