1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var xstate = require('xstate');
|
6 | var jotai = require('jotai');
|
7 |
|
8 | function _objectWithoutPropertiesLoose(source, excluded) {
|
9 | if (source == null) return {};
|
10 | var target = {};
|
11 | var sourceKeys = Object.keys(source);
|
12 | var key, i;
|
13 |
|
14 | for (i = 0; i < sourceKeys.length; i++) {
|
15 | key = sourceKeys[i];
|
16 | if (excluded.indexOf(key) >= 0) continue;
|
17 | target[key] = source[key];
|
18 | }
|
19 |
|
20 | return target;
|
21 | }
|
22 |
|
23 | function atomWithMachine(getMachine, options) {
|
24 | if (options === void 0) {
|
25 | options = {};
|
26 | }
|
27 |
|
28 | var _options = options,
|
29 | guards = _options.guards,
|
30 | actions = _options.actions,
|
31 | activities = _options.activities,
|
32 | services = _options.services,
|
33 | delays = _options.delays,
|
34 | interpreterOptions = _objectWithoutPropertiesLoose(_options, ["guards", "actions", "activities", "services", "delays"]);
|
35 |
|
36 | var machineConfig = {
|
37 | guards: guards,
|
38 | actions: actions,
|
39 | activities: activities,
|
40 | services: services,
|
41 | delays: delays
|
42 | };
|
43 | var cachedMachineAtom = jotai.atom(null);
|
44 | var machineAtom = jotai.atom(function (get) {
|
45 | var cachedMachine = get(cachedMachineAtom);
|
46 |
|
47 | if (cachedMachine) {
|
48 | return cachedMachine;
|
49 | }
|
50 |
|
51 | var initializing = true;
|
52 | var machine = typeof getMachine === 'function' ? getMachine(function (a) {
|
53 | if (initializing) {
|
54 | return get(a);
|
55 | }
|
56 |
|
57 | throw new Error('get not allowed after initialization');
|
58 | }) : getMachine;
|
59 | initializing = false;
|
60 | var machineWithConfig = machine.withConfig(machineConfig, machine.context);
|
61 | var service = xstate.interpret(machineWithConfig, interpreterOptions);
|
62 | return {
|
63 | machine: machineWithConfig,
|
64 | service: service
|
65 | };
|
66 | }, function (get, set, _arg) {
|
67 | set(cachedMachineAtom, get(machineAtom));
|
68 | });
|
69 |
|
70 | machineAtom.onMount = function (commit) {
|
71 | commit();
|
72 | };
|
73 |
|
74 | var cachedMachineStateAtom = jotai.atom(null);
|
75 | var machineStateAtom = jotai.atom(function (get) {
|
76 | var _get;
|
77 |
|
78 | return (_get = get(cachedMachineStateAtom)) != null ? _get : get(machineAtom).machine.initialState;
|
79 | }, function (get, set, registerCleanup) {
|
80 | var _get2 = get(machineAtom),
|
81 | service = _get2.service;
|
82 |
|
83 | service.onTransition(function (nextState) {
|
84 | set(cachedMachineStateAtom, nextState);
|
85 | });
|
86 | service.start();
|
87 | registerCleanup(function () {
|
88 | service.stop();
|
89 | });
|
90 | });
|
91 |
|
92 | machineStateAtom.onMount = function (initialize) {
|
93 | var unsub;
|
94 | initialize(function (cleanup) {
|
95 | if (unsub === false) {
|
96 | cleanup();
|
97 | } else {
|
98 | unsub = cleanup;
|
99 | }
|
100 | });
|
101 | return function () {
|
102 | if (unsub) {
|
103 | unsub();
|
104 | }
|
105 |
|
106 | unsub = false;
|
107 | };
|
108 | };
|
109 |
|
110 | var machineStateWithServiceAtom = jotai.atom(function (get) {
|
111 | return get(machineStateAtom);
|
112 | }, function (get, _set, event) {
|
113 | var _get3 = get(machineAtom),
|
114 | service = _get3.service;
|
115 |
|
116 | service.send(event);
|
117 | });
|
118 | return machineStateWithServiceAtom;
|
119 | }
|
120 |
|
121 | exports.atomWithMachine = atomWithMachine;
|