1 | 'use strict';
|
2 |
|
3 | var ReactExports = require('react');
|
4 | var vanilla = require('jotai/vanilla');
|
5 |
|
6 | var StoreContext = ReactExports.createContext(undefined);
|
7 | var useStore = function useStore(options) {
|
8 | var store = ReactExports.useContext(StoreContext);
|
9 | return (options == null ? void 0 : options.store) || store || vanilla.getDefaultStore();
|
10 | };
|
11 | var Provider = function Provider(_ref) {
|
12 | var children = _ref.children,
|
13 | store = _ref.store;
|
14 | var storeRef = ReactExports.useRef();
|
15 | if (!store && !storeRef.current) {
|
16 | storeRef.current = vanilla.createStore();
|
17 | }
|
18 | return ReactExports.createElement(StoreContext.Provider, {
|
19 | value: store || storeRef.current
|
20 | }, children);
|
21 | };
|
22 |
|
23 | var isPromiseLike = function isPromiseLike(x) {
|
24 | return typeof (x == null ? void 0 : x.then) === 'function';
|
25 | };
|
26 | var use = ReactExports.use || function (promise) {
|
27 | if (promise.status === 'pending') {
|
28 | throw promise;
|
29 | } else if (promise.status === 'fulfilled') {
|
30 | return promise.value;
|
31 | } else if (promise.status === 'rejected') {
|
32 | throw promise.reason;
|
33 | } else {
|
34 | promise.status = 'pending';
|
35 | promise.then(function (v) {
|
36 | promise.status = 'fulfilled';
|
37 | promise.value = v;
|
38 | }, function (e) {
|
39 | promise.status = 'rejected';
|
40 | promise.reason = e;
|
41 | });
|
42 | throw promise;
|
43 | }
|
44 | };
|
45 | function useAtomValue(atom, options) {
|
46 | var store = useStore(options);
|
47 | var _useReducer = ReactExports.useReducer(function (prev) {
|
48 | var 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 | }, undefined, function () {
|
54 | return [store.get(atom), store, atom];
|
55 | }),
|
56 | _useReducer$ = _useReducer[0],
|
57 | valueFromReducer = _useReducer$[0],
|
58 | storeFromReducer = _useReducer$[1],
|
59 | atomFromReducer = _useReducer$[2],
|
60 | rerender = _useReducer[1];
|
61 | var value = valueFromReducer;
|
62 | if (storeFromReducer !== store || atomFromReducer !== atom) {
|
63 | rerender();
|
64 | value = store.get(atom);
|
65 | }
|
66 | var delay = options == null ? void 0 : options.delay;
|
67 | ReactExports.useEffect(function () {
|
68 | var unsub = store.sub(atom, function () {
|
69 | if (typeof delay === 'number') {
|
70 | setTimeout(rerender, delay);
|
71 | return;
|
72 | }
|
73 | rerender();
|
74 | });
|
75 | rerender();
|
76 | return unsub;
|
77 | }, [store, atom, delay]);
|
78 | ReactExports.useDebugValue(value);
|
79 | return isPromiseLike(value) ? use(value) : value;
|
80 | }
|
81 |
|
82 | function useSetAtom(atom, options) {
|
83 | var store = useStore(options);
|
84 | var setAtom = ReactExports.useCallback(function () {
|
85 | if (process.env.NODE_ENV !== 'production' && !('write' in atom)) {
|
86 | throw new Error('not writable atom');
|
87 | }
|
88 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
89 | args[_key] = arguments[_key];
|
90 | }
|
91 | return store.set.apply(store, [atom].concat(args));
|
92 | }, [store, atom]);
|
93 | return setAtom;
|
94 | }
|
95 |
|
96 | function useAtom(atom, options) {
|
97 | return [useAtomValue(atom, options), useSetAtom(atom, options)];
|
98 | }
|
99 |
|
100 | exports.Provider = Provider;
|
101 | exports.useAtom = useAtom;
|
102 | exports.useAtomValue = useAtomValue;
|
103 | exports.useSetAtom = useSetAtom;
|
104 | exports.useStore = useStore;
|