UNPKG

2.3 kBJavaScriptView Raw
1System.register(['react', 'jotai/react', 'jotai/vanilla/utils', 'jotai/vanilla'], (function (exports) {
2 'use strict';
3 var useCallback, useMemo, useSetAtom, useAtom, useStore, RESET, atom;
4 return {
5 setters: [function (module) {
6 useCallback = module.useCallback;
7 useMemo = module.useMemo;
8 }, function (module) {
9 useSetAtom = module.useSetAtom;
10 useAtom = module.useAtom;
11 useStore = module.useStore;
12 }, function (module) {
13 RESET = module.RESET;
14 }, function (module) {
15 atom = module.atom;
16 }],
17 execute: (function () {
18
19 exports({
20 useAtomCallback: useAtomCallback,
21 useHydrateAtoms: useHydrateAtoms,
22 useReducerAtom: useReducerAtom,
23 useResetAtom: useResetAtom
24 });
25
26 function useResetAtom(anAtom, options) {
27 const setAtom = useSetAtom(anAtom, options);
28 const resetAtom = useCallback(() => setAtom(RESET), [setAtom]);
29 return resetAtom;
30 }
31
32 function useReducerAtom(anAtom, reducer, options) {
33 const [state, setState] = useAtom(anAtom, options);
34 const dispatch = useCallback(
35 (action) => {
36 setState((prev) => reducer(prev, action));
37 },
38 [setState, reducer]
39 );
40 return [state, dispatch];
41 }
42
43 function useAtomCallback(callback, options) {
44 const anAtom = useMemo(
45 () => atom(null, (get, set, ...args) => callback(get, set, ...args)),
46 [callback]
47 );
48 return useSetAtom(anAtom, options);
49 }
50
51 const hydratedMap = /* @__PURE__ */ new WeakMap();
52 function useHydrateAtoms(values, options) {
53 const store = useStore(options);
54 const hydratedSet = getHydratedSet(store);
55 for (const [atom, value] of values) {
56 if (!hydratedSet.has(atom) || (options == null ? void 0 : options.dangerouslyForceHydrate)) {
57 hydratedSet.add(atom);
58 store.set(atom, value);
59 }
60 }
61 }
62 const getHydratedSet = (store) => {
63 let hydratedSet = hydratedMap.get(store);
64 if (!hydratedSet) {
65 hydratedSet = /* @__PURE__ */ new WeakSet();
66 hydratedMap.set(store, hydratedSet);
67 }
68 return hydratedSet;
69 };
70
71 })
72 };
73}));