1 | 'use client';
|
2 | 'use strict';
|
3 |
|
4 | var react$1 = require('react');
|
5 | var react = require('jotai/react');
|
6 | var utils = require('jotai/vanilla/utils');
|
7 | var vanilla = require('jotai/vanilla');
|
8 |
|
9 | function useResetAtom(anAtom, options) {
|
10 | var setAtom = react.useSetAtom(anAtom, options);
|
11 | var resetAtom = react$1.useCallback(function () {
|
12 | return setAtom(utils.RESET);
|
13 | }, [setAtom]);
|
14 | return resetAtom;
|
15 | }
|
16 |
|
17 | function useReducerAtom(anAtom, reducer, options) {
|
18 | var _useAtom = react.useAtom(anAtom, options),
|
19 | state = _useAtom[0],
|
20 | setState = _useAtom[1];
|
21 | var dispatch = react$1.useCallback(function (action) {
|
22 | setState(function (prev) {
|
23 | return reducer(prev, action);
|
24 | });
|
25 | }, [setState, reducer]);
|
26 | return [state, dispatch];
|
27 | }
|
28 |
|
29 | function useAtomCallback(callback, options) {
|
30 | var anAtom = react$1.useMemo(function () {
|
31 | return vanilla.atom(null, function (get, set) {
|
32 | for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
33 | args[_key - 2] = arguments[_key];
|
34 | }
|
35 | return callback.apply(void 0, [get, set].concat(args));
|
36 | });
|
37 | }, [callback]);
|
38 | return react.useSetAtom(anAtom, options);
|
39 | }
|
40 |
|
41 | function _unsupportedIterableToArray(o, minLen) {
|
42 | if (!o) return;
|
43 | if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
44 | var n = Object.prototype.toString.call(o).slice(8, -1);
|
45 | if (n === "Object" && o.constructor) n = o.constructor.name;
|
46 | if (n === "Map" || n === "Set") return Array.from(o);
|
47 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
48 | }
|
49 | function _arrayLikeToArray(arr, len) {
|
50 | if (len == null || len > arr.length) len = arr.length;
|
51 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
52 | return arr2;
|
53 | }
|
54 | function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
55 | var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
56 | if (it) return (it = it.call(o)).next.bind(it);
|
57 | if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
58 | if (it) o = it;
|
59 | var i = 0;
|
60 | return function () {
|
61 | if (i >= o.length) return {
|
62 | done: true
|
63 | };
|
64 | return {
|
65 | done: false,
|
66 | value: o[i++]
|
67 | };
|
68 | };
|
69 | }
|
70 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
71 | }
|
72 |
|
73 | var hydratedMap = new WeakMap();
|
74 | function useHydrateAtoms(values, options) {
|
75 | var store = react.useStore(options);
|
76 | var hydratedSet = getHydratedSet(store);
|
77 | for (var _iterator = _createForOfIteratorHelperLoose(values), _step; !(_step = _iterator()).done;) {
|
78 | var _step$value = _step.value,
|
79 | atom = _step$value[0],
|
80 | value = _step$value[1];
|
81 | if (!hydratedSet.has(atom) || options != null && options.dangerouslyForceHydrate) {
|
82 | hydratedSet.add(atom);
|
83 | store.set(atom, value);
|
84 | }
|
85 | }
|
86 | }
|
87 | var getHydratedSet = function getHydratedSet(store) {
|
88 | var hydratedSet = hydratedMap.get(store);
|
89 | if (!hydratedSet) {
|
90 | hydratedSet = new WeakSet();
|
91 | hydratedMap.set(store, hydratedSet);
|
92 | }
|
93 | return hydratedSet;
|
94 | };
|
95 |
|
96 | exports.useAtomCallback = useAtomCallback;
|
97 | exports.useHydrateAtoms = useHydrateAtoms;
|
98 | exports.useReducerAtom = useReducerAtom;
|
99 | exports.useResetAtom = useResetAtom;
|