UNPKG

4.2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var xstate = require('xstate');
6var jotai = require('jotai');
7
8function _extends() {
9 _extends = Object.assign ? Object.assign.bind() : function (target) {
10 for (var i = 1; i < arguments.length; i++) {
11 var source = arguments[i];
12
13 for (var key in source) {
14 if (Object.prototype.hasOwnProperty.call(source, key)) {
15 target[key] = source[key];
16 }
17 }
18 }
19
20 return target;
21 };
22 return _extends.apply(this, arguments);
23}
24
25function _objectWithoutPropertiesLoose(source, excluded) {
26 if (source == null) return {};
27 var target = {};
28 var sourceKeys = Object.keys(source);
29 var key, i;
30
31 for (i = 0; i < sourceKeys.length; i++) {
32 key = sourceKeys[i];
33 if (excluded.indexOf(key) >= 0) continue;
34 target[key] = source[key];
35 }
36
37 return target;
38}
39
40var _excluded = ["guards", "actions", "services", "delays", "context"];
41var RESTART = Symbol();
42function atomWithMachine(getMachine, getOptions) {
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
53 var safeGet = function safeGet(a) {
54 if (initializing) {
55 return get(a);
56 }
57
58 throw new Error('get not allowed after initialization');
59 };
60
61 var machine = isGetter(getMachine) ? getMachine(safeGet) : getMachine;
62 var options = isGetter(getOptions) ? getOptions(safeGet) : getOptions;
63 initializing = false;
64
65 var _ref = options || {},
66 guards = _ref.guards,
67 actions = _ref.actions,
68 services = _ref.services,
69 delays = _ref.delays,
70 context = _ref.context,
71 interpreterOptions = _objectWithoutPropertiesLoose(_ref, _excluded);
72
73 var machineConfig = _extends({}, guards && {
74 guards: guards
75 }, actions && {
76 actions: actions
77 }, services && {
78 services: services
79 }, delays && {
80 delays: delays
81 });
82
83 var machineWithConfig = machine.withConfig(machineConfig, function () {
84 return _extends({}, machine.context, context);
85 });
86 var service = xstate.interpret(machineWithConfig, interpreterOptions);
87 return {
88 machine: machineWithConfig,
89 service: service
90 };
91 }, function (get, set, _arg) {
92 set(cachedMachineAtom, get(machineAtom));
93 });
94
95 machineAtom.onMount = function (commit) {
96 commit();
97 };
98
99 var cachedMachineStateAtom = jotai.atom(null);
100 var machineStateAtom = jotai.atom(function (get) {
101 var _get;
102
103 return (_get = get(cachedMachineStateAtom)) != null ? _get : get(machineAtom).machine.initialState;
104 }, function (get, set, registerCleanup) {
105 var _get2 = get(machineAtom),
106 service = _get2.service;
107
108 service.onTransition(function (nextState) {
109 set(cachedMachineStateAtom, nextState);
110 });
111 service.start();
112 registerCleanup(function () {
113 var _get3 = get(machineAtom),
114 service = _get3.service;
115
116 service.stop();
117 });
118 });
119
120 machineStateAtom.onMount = function (initialize) {
121 var unsub;
122 initialize(function (cleanup) {
123 if (unsub === false) {
124 cleanup();
125 } else {
126 unsub = cleanup;
127 }
128 });
129 return function () {
130 if (unsub) {
131 unsub();
132 }
133
134 unsub = false;
135 };
136 };
137
138 var machineStateWithServiceAtom = jotai.atom(function (get) {
139 return get(machineStateAtom);
140 }, function (get, set, event) {
141 var _get4 = get(machineAtom),
142 service = _get4.service;
143
144 if (event === RESTART) {
145 service.stop();
146 set(cachedMachineAtom, null);
147 set(machineAtom, null);
148
149 var _get5 = get(machineAtom),
150 newService = _get5.service;
151
152 newService.onTransition(function (nextState) {
153 set(cachedMachineStateAtom, nextState);
154 });
155 newService.start();
156 } else {
157 service.send(event);
158 }
159 });
160 return machineStateWithServiceAtom;
161}
162
163var isGetter = function isGetter(v) {
164 return typeof v === 'function';
165};
166
167exports.RESTART = RESTART;
168exports.atomWithMachine = atomWithMachine;