1 | 'use strict';
|
2 |
|
3 | var react = require('react');
|
4 | var jotai = require('jotai');
|
5 |
|
6 | function _extends() {
|
7 | _extends = Object.assign ? Object.assign.bind() : function (target) {
|
8 | for (var i = 1; i < arguments.length; i++) {
|
9 | var source = arguments[i];
|
10 | for (var key in source) {
|
11 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
12 | target[key] = source[key];
|
13 | }
|
14 | }
|
15 | }
|
16 | return target;
|
17 | };
|
18 | return _extends.apply(this, arguments);
|
19 | }
|
20 | function _unsupportedIterableToArray(o, minLen) {
|
21 | if (!o) return;
|
22 | if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
23 | var n = Object.prototype.toString.call(o).slice(8, -1);
|
24 | if (n === "Object" && o.constructor) n = o.constructor.name;
|
25 | if (n === "Map" || n === "Set") return Array.from(o);
|
26 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
27 | }
|
28 | function _arrayLikeToArray(arr, len) {
|
29 | if (len == null || len > arr.length) len = arr.length;
|
30 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
31 | return arr2;
|
32 | }
|
33 | function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
34 | var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
35 | if (it) return (it = it.call(o)).next.bind(it);
|
36 | if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
37 | if (it) o = it;
|
38 | var i = 0;
|
39 | return function () {
|
40 | if (i >= o.length) return {
|
41 | done: true
|
42 | };
|
43 | return {
|
44 | done: false,
|
45 | value: o[i++]
|
46 | };
|
47 | };
|
48 | }
|
49 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
50 | }
|
51 |
|
52 | var RESTORE_ATOMS = 'h';
|
53 |
|
54 | var DEV_SUBSCRIBE_STATE = 'n';
|
55 | var DEV_GET_MOUNTED_ATOMS = 'l';
|
56 | var DEV_GET_ATOM_STATE = 'a';
|
57 | var DEV_GET_MOUNTED = 'm';
|
58 |
|
59 | var atomToPrintable$1 = function atomToPrintable(atom) {
|
60 | return atom.debugLabel || atom.toString();
|
61 | };
|
62 | var stateToPrintable = function stateToPrintable(_ref) {
|
63 | var store = _ref[0],
|
64 | atoms = _ref[1];
|
65 | return Object.fromEntries(atoms.flatMap(function (atom) {
|
66 | var _store$DEV_GET_MOUNTE, _store$DEV_GET_ATOM_S;
|
67 | var mounted = (_store$DEV_GET_MOUNTE = store[DEV_GET_MOUNTED]) == null ? void 0 : _store$DEV_GET_MOUNTE.call(store, atom);
|
68 | if (!mounted) {
|
69 | return [];
|
70 | }
|
71 | var dependents = mounted.t;
|
72 | var atomState = ((_store$DEV_GET_ATOM_S = store[DEV_GET_ATOM_STATE]) == null ? void 0 : _store$DEV_GET_ATOM_S.call(store, atom)) || {};
|
73 | return [[atomToPrintable$1(atom), _extends({}, 'e' in atomState && {
|
74 | error: atomState.e
|
75 | }, 'p' in atomState && {
|
76 | promise: atomState.p
|
77 | }, 'v' in atomState && {
|
78 | value: atomState.v
|
79 | }, {
|
80 | dependents: Array.from(dependents).map(atomToPrintable$1)
|
81 | })]];
|
82 | }));
|
83 | };
|
84 | var useAtomsDebugValue = function useAtomsDebugValue(options) {
|
85 | var _options$enabled;
|
86 | var enabled = (_options$enabled = options == null ? void 0 : options.enabled) != null ? _options$enabled : process.env.NODE_ENV !== "production";
|
87 | var ScopeContext = jotai.SECRET_INTERNAL_getScopeContext(options == null ? void 0 : options.scope);
|
88 | var _useContext = react.useContext(ScopeContext),
|
89 | store = _useContext.s;
|
90 | var _useState = react.useState([]),
|
91 | atoms = _useState[0],
|
92 | setAtoms = _useState[1];
|
93 | react.useEffect(function () {
|
94 | var _store$DEV_SUBSCRIBE_;
|
95 | if (!enabled) {
|
96 | return;
|
97 | }
|
98 | var callback = function callback() {
|
99 | var _store$DEV_GET_MOUNTE2;
|
100 | setAtoms(Array.from(((_store$DEV_GET_MOUNTE2 = store[DEV_GET_MOUNTED_ATOMS]) == null ? void 0 : _store$DEV_GET_MOUNTE2.call(store)) || []));
|
101 | };
|
102 | var unsubscribe = (_store$DEV_SUBSCRIBE_ = store[DEV_SUBSCRIBE_STATE]) == null ? void 0 : _store$DEV_SUBSCRIBE_.call(store, callback);
|
103 | callback();
|
104 | return unsubscribe;
|
105 | }, [enabled, store]);
|
106 | react.useDebugValue([store, atoms], stateToPrintable);
|
107 | };
|
108 |
|
109 | function useAtomDevtools(anAtom, options, deprecatedScope) {
|
110 | if (typeof options === 'string' || typeof deprecatedScope !== 'undefined') {
|
111 | console.warn('DEPRECATED [useAtomDevtools] use DevtoolOptions');
|
112 | options = {
|
113 | name: options,
|
114 | scope: deprecatedScope
|
115 | };
|
116 | }
|
117 |
|
118 | var _ref = options || {},
|
119 | enabled = _ref.enabled,
|
120 | name = _ref.name,
|
121 | scope = _ref.scope;
|
122 | var extension;
|
123 | try {
|
124 | extension = (enabled != null ? enabled : process.env.NODE_ENV !== "production") && window.__REDUX_DEVTOOLS_EXTENSION__;
|
125 | } catch (_unused) {
|
126 | }
|
127 | if (!extension) {
|
128 | if (process.env.NODE_ENV !== "production" && enabled) {
|
129 | console.warn('Please install/enable Redux devtools extension');
|
130 | }
|
131 | }
|
132 | var _useAtom = jotai.useAtom(anAtom, scope),
|
133 | value = _useAtom[0],
|
134 | setValue = _useAtom[1];
|
135 | var lastValue = react.useRef(value);
|
136 | var isTimeTraveling = react.useRef(false);
|
137 | var devtools = react.useRef();
|
138 | var atomName = name || anAtom.debugLabel || anAtom.toString();
|
139 | react.useEffect(function () {
|
140 | if (!extension) {
|
141 | return;
|
142 | }
|
143 | var setValueIfWritable = function setValueIfWritable(value) {
|
144 | if (typeof setValue === 'function') {
|
145 | setValue(value);
|
146 | return;
|
147 | }
|
148 | console.warn('[Warn] you cannot do write operations (Time-travelling, etc) in read-only atoms\n', anAtom);
|
149 | };
|
150 | devtools.current = extension.connect({
|
151 | name: atomName
|
152 | });
|
153 | var unsubscribe = devtools.current.subscribe(function (message) {
|
154 | var _message$payload3, _message$payload4;
|
155 | if (message.type === 'ACTION' && message.payload) {
|
156 | try {
|
157 | setValueIfWritable(JSON.parse(message.payload));
|
158 | } catch (e) {
|
159 | console.error('please dispatch a serializable value that JSON.parse() support\n', e);
|
160 | }
|
161 | } else if (message.type === 'DISPATCH' && message.state) {
|
162 | var _message$payload, _message$payload2;
|
163 | if (((_message$payload = message.payload) == null ? void 0 : _message$payload.type) === 'JUMP_TO_ACTION' || ((_message$payload2 = message.payload) == null ? void 0 : _message$payload2.type) === 'JUMP_TO_STATE') {
|
164 | isTimeTraveling.current = true;
|
165 | setValueIfWritable(JSON.parse(message.state));
|
166 | }
|
167 | } else if (message.type === 'DISPATCH' && ((_message$payload3 = message.payload) == null ? void 0 : _message$payload3.type) === 'COMMIT') {
|
168 | var _devtools$current;
|
169 | (_devtools$current = devtools.current) == null ? void 0 : _devtools$current.init(lastValue.current);
|
170 | } else if (message.type === 'DISPATCH' && ((_message$payload4 = message.payload) == null ? void 0 : _message$payload4.type) === 'IMPORT_STATE') {
|
171 | var _message$payload$next;
|
172 | var computedStates = ((_message$payload$next = message.payload.nextLiftedState) == null ? void 0 : _message$payload$next.computedStates) || [];
|
173 | computedStates.forEach(function (_ref2, index) {
|
174 | var state = _ref2.state;
|
175 | if (index === 0) {
|
176 | var _devtools$current2;
|
177 | (_devtools$current2 = devtools.current) == null ? void 0 : _devtools$current2.init(state);
|
178 | } else {
|
179 | setValueIfWritable(state);
|
180 | }
|
181 | });
|
182 | }
|
183 | });
|
184 | devtools.current.shouldInit = true;
|
185 | return unsubscribe;
|
186 | }, [anAtom, extension, atomName, setValue]);
|
187 | react.useEffect(function () {
|
188 | if (!devtools.current) {
|
189 | return;
|
190 | }
|
191 | lastValue.current = value;
|
192 | if (devtools.current.shouldInit) {
|
193 | devtools.current.init(value);
|
194 | devtools.current.shouldInit = false;
|
195 | } else if (isTimeTraveling.current) {
|
196 | isTimeTraveling.current = false;
|
197 | } else {
|
198 | devtools.current.send(atomName + " - " + new Date().toLocaleString(), value);
|
199 | }
|
200 | }, [anAtom, extension, atomName, value]);
|
201 | }
|
202 |
|
203 | var isEqualAtomsValues = function isEqualAtomsValues(left, right) {
|
204 | return left.size === right.size && Array.from(left).every(function (_ref) {
|
205 | var left = _ref[0],
|
206 | v = _ref[1];
|
207 | return Object.is(right.get(left), v);
|
208 | });
|
209 | };
|
210 | var isEqualAtomsDependents = function isEqualAtomsDependents(left, right) {
|
211 | return left.size === right.size && Array.from(left).every(function (_ref2) {
|
212 | var a = _ref2[0],
|
213 | dLeft = _ref2[1];
|
214 | var dRight = right.get(a);
|
215 | return dRight && dLeft.size === dRight.size && Array.from(dLeft).every(function (d) {
|
216 | return dRight.has(d);
|
217 | });
|
218 | });
|
219 | };
|
220 | function useAtomsSnapshot(scope) {
|
221 | var ScopeContext = jotai.SECRET_INTERNAL_getScopeContext(scope);
|
222 | var scopeContainer = react.useContext(ScopeContext);
|
223 | var store = scopeContainer.s;
|
224 | var _useState = react.useState(function () {
|
225 | return {
|
226 | values: new Map(),
|
227 | dependents: new Map()
|
228 | };
|
229 | }),
|
230 | atomsSnapshot = _useState[0],
|
231 | setAtomsSnapshot = _useState[1];
|
232 | react.useEffect(function () {
|
233 | if (!store[DEV_SUBSCRIBE_STATE]) return;
|
234 | var prevValues = new Map();
|
235 | var prevDependents = new Map();
|
236 | var invalidatedAtoms = new Set();
|
237 | var callback = function callback() {
|
238 | var values = new Map();
|
239 | var dependents = new Map();
|
240 | var hasNewInvalidatedAtoms = false;
|
241 | for (var _iterator = _createForOfIteratorHelperLoose(store[DEV_GET_MOUNTED_ATOMS]() || []), _step; !(_step = _iterator()).done;) {
|
242 | var atom = _step.value;
|
243 | var atomState = store[DEV_GET_ATOM_STATE](atom);
|
244 | if (atomState) {
|
245 | if (!atomState.y) {
|
246 | if ('p' in atomState) {
|
247 | return;
|
248 | }
|
249 | if (!invalidatedAtoms.has(atom)) {
|
250 | invalidatedAtoms.add(atom);
|
251 | hasNewInvalidatedAtoms = true;
|
252 | }
|
253 | }
|
254 | if ('v' in atomState) {
|
255 | values.set(atom, atomState.v);
|
256 | }
|
257 | }
|
258 | var mounted = store[DEV_GET_MOUNTED](atom);
|
259 | if (mounted) {
|
260 | dependents.set(atom, mounted.t);
|
261 | }
|
262 | }
|
263 | if (hasNewInvalidatedAtoms) {
|
264 | return;
|
265 | }
|
266 | if (isEqualAtomsValues(prevValues, values) && isEqualAtomsDependents(prevDependents, dependents)) {
|
267 | return;
|
268 | }
|
269 | prevValues = values;
|
270 | prevDependents = dependents;
|
271 | invalidatedAtoms.clear();
|
272 | setAtomsSnapshot({
|
273 | values: values,
|
274 | dependents: dependents
|
275 | });
|
276 | };
|
277 | var unsubscribe = store[DEV_SUBSCRIBE_STATE](callback);
|
278 | callback();
|
279 | return unsubscribe;
|
280 | }, [store]);
|
281 | return atomsSnapshot;
|
282 | }
|
283 |
|
284 | function useGotoAtomsSnapshot(scope) {
|
285 | var ScopeContext = jotai.SECRET_INTERNAL_getScopeContext(scope);
|
286 | var _useContext = react.useContext(ScopeContext),
|
287 | store = _useContext.s,
|
288 | versionedWrite = _useContext.w;
|
289 | return react.useCallback(function (snapshot) {
|
290 | if (!store[DEV_SUBSCRIBE_STATE]) return;
|
291 | var restoreAtoms = function restoreAtoms(values) {
|
292 | if (versionedWrite) {
|
293 | versionedWrite(function (version) {
|
294 | store[RESTORE_ATOMS](values, version);
|
295 | });
|
296 | } else {
|
297 | store[RESTORE_ATOMS](values);
|
298 | }
|
299 | };
|
300 | if (isIterable(snapshot)) {
|
301 | if (process.env.NODE_ENV !== "production") {
|
302 | console.warn('snapshot as iterable is deprecated. use an object instead.');
|
303 | }
|
304 | restoreAtoms(snapshot);
|
305 | return;
|
306 | }
|
307 | restoreAtoms(snapshot.values);
|
308 | }, [store, versionedWrite]);
|
309 | }
|
310 | var isIterable = function isIterable(item) {
|
311 | return typeof item[Symbol.iterator] === 'function';
|
312 | };
|
313 |
|
314 | var atomToPrintable = function atomToPrintable(atom) {
|
315 | return atom.debugLabel ? atom + ":" + atom.debugLabel : "" + atom;
|
316 | };
|
317 | var getDevtoolsState = function getDevtoolsState(atomsSnapshot) {
|
318 | var values = {};
|
319 | atomsSnapshot.values.forEach(function (v, atom) {
|
320 | values[atomToPrintable(atom)] = v;
|
321 | });
|
322 | var dependents = {};
|
323 | atomsSnapshot.dependents.forEach(function (d, atom) {
|
324 | dependents[atomToPrintable(atom)] = Array.from(d).map(atomToPrintable);
|
325 | });
|
326 | return {
|
327 | values: values,
|
328 | dependents: dependents
|
329 | };
|
330 | };
|
331 | function useAtomsDevtools(name, options) {
|
332 | if (typeof options !== 'undefined' && typeof options !== 'object') {
|
333 | console.warn('DEPRECATED [useAtomsDevtools] use DevtoolsOptions');
|
334 | options = {
|
335 | scope: options
|
336 | };
|
337 | }
|
338 | var _ref = options || {},
|
339 | enabled = _ref.enabled,
|
340 | scope = _ref.scope;
|
341 | var extension;
|
342 | try {
|
343 | extension = (enabled != null ? enabled : process.env.NODE_ENV !== "production") && window.__REDUX_DEVTOOLS_EXTENSION__;
|
344 | } catch (_unused) {
|
345 | }
|
346 | if (!extension) {
|
347 | if (process.env.NODE_ENV !== "production" && enabled) {
|
348 | console.warn('Please install/enable Redux devtools extension');
|
349 | }
|
350 | }
|
351 |
|
352 | var atomsSnapshot = useAtomsSnapshot(scope);
|
353 | var goToSnapshot = useGotoAtomsSnapshot(scope);
|
354 | var isTimeTraveling = react.useRef(false);
|
355 | var isRecording = react.useRef(true);
|
356 | var devtools = react.useRef();
|
357 | var snapshots = react.useRef([]);
|
358 | react.useEffect(function () {
|
359 | if (!extension) {
|
360 | return;
|
361 | }
|
362 | var getSnapshotAt = function getSnapshotAt(index) {
|
363 | if (index === void 0) {
|
364 | index = snapshots.current.length - 1;
|
365 | }
|
366 | var snapshot = snapshots.current[index >= 0 ? index : 0];
|
367 | if (!snapshot) {
|
368 | throw new Error('snaphost index out of bounds');
|
369 | }
|
370 | return snapshot;
|
371 | };
|
372 | var connection = extension.connect({
|
373 | name: name
|
374 | });
|
375 | var devtoolsUnsubscribe = connection.subscribe(function (message) {
|
376 | var _message$payload;
|
377 | switch (message.type) {
|
378 | case 'DISPATCH':
|
379 | switch ((_message$payload = message.payload) == null ? void 0 : _message$payload.type) {
|
380 | case 'RESET':
|
381 | break;
|
382 | case 'COMMIT':
|
383 | connection.init(getDevtoolsState(getSnapshotAt()));
|
384 | snapshots.current = [];
|
385 | break;
|
386 | case 'JUMP_TO_ACTION':
|
387 | case 'JUMP_TO_STATE':
|
388 | isTimeTraveling.current = true;
|
389 | goToSnapshot(getSnapshotAt(message.payload.actionId - 1));
|
390 | break;
|
391 | case 'PAUSE_RECORDING':
|
392 | isRecording.current = !isRecording.current;
|
393 | break;
|
394 | }
|
395 | }
|
396 | });
|
397 | devtools.current = connection;
|
398 | devtools.current.shouldInit = true;
|
399 | return function () {
|
400 | extension.disconnect();
|
401 | devtoolsUnsubscribe == null ? void 0 : devtoolsUnsubscribe();
|
402 | };
|
403 | }, [extension, goToSnapshot, name]);
|
404 | react.useEffect(function () {
|
405 | if (!devtools.current) {
|
406 | return;
|
407 | }
|
408 | if (devtools.current.shouldInit) {
|
409 | devtools.current.init(undefined);
|
410 | devtools.current.shouldInit = false;
|
411 | return;
|
412 | }
|
413 | if (isTimeTraveling.current) {
|
414 | isTimeTraveling.current = false;
|
415 | } else if (isRecording.current) {
|
416 | snapshots.current.push(atomsSnapshot);
|
417 | devtools.current.send({
|
418 | type: "" + snapshots.current.length,
|
419 | updatedAt: new Date().toLocaleString()
|
420 | }, getDevtoolsState(atomsSnapshot));
|
421 | }
|
422 | }, [atomsSnapshot]);
|
423 | }
|
424 |
|
425 | exports.useAtomDevtools = useAtomDevtools;
|
426 | exports.useAtomsDebugValue = useAtomsDebugValue;
|
427 | exports.useAtomsDevtools = useAtomsDevtools;
|
428 | exports.useAtomsSnapshot = useAtomsSnapshot;
|
429 | exports.useGotoAtomsSnapshot = useGotoAtomsSnapshot;
|