UNPKG

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