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