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