@xstate/react 4.0.0-alpha.0 → 4.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
- import { useEffect, useState, useCallback } from 'react';
2
- import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
3
- import { toObserver, State, InterpreterStatus, interpret, ObservableActorRef } from 'xstate';
4
- import { u as useConstant, _ as _slicedToArray } from './useConstant-94bfdbb5.esm.js';
1
+ import { u as useConstant, _ as _slicedToArray } from './useConstant-0013a606.esm.js';
2
+ import { useEffect, useState, useCallback, createContext, createElement, useContext } from 'react';
5
3
  import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
4
+ import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
5
+ import { toObserver, InterpreterStatus, interpret } from 'xstate';
6
6
  import { useSyncExternalStore } from 'use-sync-external-store/shim';
7
7
 
8
8
  function _objectWithoutPropertiesLoose(source, excluded) {
@@ -10,13 +10,11 @@ function _objectWithoutPropertiesLoose(source, excluded) {
10
10
  var target = {};
11
11
  var sourceKeys = Object.keys(source);
12
12
  var key, i;
13
-
14
13
  for (i = 0; i < sourceKeys.length; i++) {
15
14
  key = sourceKeys[i];
16
15
  if (excluded.indexOf(key) >= 0) continue;
17
16
  target[key] = source[key];
18
17
  }
19
-
20
18
  return target;
21
19
  }
22
20
 
@@ -24,10 +22,8 @@ function _objectWithoutProperties(source, excluded) {
24
22
  if (source == null) return {};
25
23
  var target = _objectWithoutPropertiesLoose(source, excluded);
26
24
  var key, i;
27
-
28
25
  if (Object.getOwnPropertySymbols) {
29
26
  var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
30
-
31
27
  for (i = 0; i < sourceSymbolKeys.length; i++) {
32
28
  key = sourceSymbolKeys[i];
33
29
  if (excluded.indexOf(key) >= 0) continue;
@@ -35,37 +31,29 @@ function _objectWithoutProperties(source, excluded) {
35
31
  target[key] = source[key];
36
32
  }
37
33
  }
38
-
39
34
  return target;
40
35
  }
41
36
 
42
- var _excluded = ["context", "actors", "guards", "actions", "delays", "state"];
37
+ var _excluded = ["actors", "guards", "actions", "delays"];
43
38
  function useIdleInterpreter(getMachine, options) {
44
39
  var machine = useConstant(function () {
45
40
  return typeof getMachine === 'function' ? getMachine() : getMachine;
46
41
  });
47
-
48
42
  if (process.env.NODE_ENV !== 'production' && typeof getMachine !== 'function') {
49
43
  var _useState = useState(machine),
50
- _useState2 = _slicedToArray(_useState, 1),
51
- initialMachine = _useState2[0];
52
-
44
+ _useState2 = _slicedToArray(_useState, 1),
45
+ initialMachine = _useState2[0];
53
46
  if (getMachine !== initialMachine) {
54
47
  console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' + 'Please make sure that you pass the same Machine as argument each time.');
55
48
  }
56
49
  }
57
-
58
- var context = options.context,
59
- actors = options.actors,
60
- guards = options.guards,
61
- actions = options.actions,
62
- delays = options.delays,
63
- rehydratedState = options.state,
64
- interpreterOptions = _objectWithoutProperties(options, _excluded);
65
-
50
+ var actors = options.actors,
51
+ guards = options.guards,
52
+ actions = options.actions,
53
+ delays = options.delays,
54
+ interpreterOptions = _objectWithoutProperties(options, _excluded);
66
55
  var service = useConstant(function () {
67
56
  var machineConfig = {
68
- context: context,
69
57
  guards: guards,
70
58
  actions: actions,
71
59
  actors: actors,
@@ -73,15 +61,16 @@ function useIdleInterpreter(getMachine, options) {
73
61
  };
74
62
  var machineWithConfig = machine.provide(machineConfig);
75
63
  return interpret(machineWithConfig, interpreterOptions);
76
- }); // Make sure options are kept updated when they change.
64
+ });
65
+
66
+ // Make sure options are kept updated when they change.
77
67
  // This mutation assignment is safe because the service instance is only used
78
68
  // in one place -- this hook's caller.
79
-
80
69
  useIsomorphicLayoutEffect(function () {
81
- Object.assign(service.machine.options.actions, actions);
82
- Object.assign(service.machine.options.guards, guards);
83
- Object.assign(service.machine.options.actors, actors);
84
- Object.assign(service.machine.options.delays, delays);
70
+ Object.assign(service.behavior.options.actions, actions);
71
+ Object.assign(service.behavior.options.guards, guards);
72
+ Object.assign(service.behavior.options.actors, actors);
73
+ Object.assign(service.behavior.options.delays, delays);
85
74
  }, [actions, guards, actors, delays]);
86
75
  return service;
87
76
  }
@@ -89,27 +78,25 @@ function useInterpret(getMachine) {
89
78
  for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
90
79
  _ref[_key - 1] = arguments[_key];
91
80
  }
92
-
93
81
  var _ref$ = _ref[0],
94
- options = _ref$ === void 0 ? {} : _ref$,
95
- observerOrListener = _ref[1];
82
+ options = _ref$ === void 0 ? {} : _ref$,
83
+ observerOrListener = _ref[1];
96
84
  var service = useIdleInterpreter(getMachine, options);
97
85
  useEffect(function () {
98
86
  if (!observerOrListener) {
99
87
  return;
100
88
  }
101
-
102
89
  var sub = service.subscribe(toObserver(observerOrListener));
103
90
  return function () {
104
91
  sub.unsubscribe();
105
92
  };
106
93
  }, [observerOrListener]);
107
94
  useEffect(function () {
108
- var rehydratedState = options.state;
109
- service.start(rehydratedState ? State.create(rehydratedState) : undefined);
95
+ service.start();
110
96
  return function () {
111
97
  service.stop();
112
98
  service.status = InterpreterStatus.NotStarted;
99
+ service._initState();
113
100
  };
114
101
  }, []);
115
102
  return service;
@@ -118,51 +105,33 @@ function useInterpret(getMachine) {
118
105
  function identity(a) {
119
106
  return a;
120
107
  }
121
-
122
108
  var isEqual = function isEqual(prevState, nextState) {
123
109
  return prevState === nextState || nextState.changed === false;
124
110
  };
125
-
126
- // TODO: rethink how we can do this better
127
- var cachedRehydratedStates = new WeakMap();
128
111
  function useMachine(getMachine) {
129
112
  for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
130
113
  _ref[_key - 1] = arguments[_key];
131
114
  }
132
-
133
115
  var _ref$ = _ref[0],
134
- options = _ref$ === void 0 ? {} : _ref$;
116
+ options = _ref$ === void 0 ? {} : _ref$;
135
117
  // using `useIdleInterpreter` allows us to subscribe to the service *before* we start it
136
118
  // so we don't miss any notifications
137
119
  var service = useIdleInterpreter(getMachine, options);
138
120
  var getSnapshot = useCallback(function () {
139
- if (service.status === InterpreterStatus.NotStarted && options.state) {
140
- var cached = cachedRehydratedStates.get(options.state);
141
-
142
- if (cached) {
143
- return cached;
144
- }
145
-
146
- var created = State.create(options.state);
147
- cachedRehydratedStates.set(options.state, created);
148
- return created;
149
- }
150
-
151
121
  return service.getSnapshot();
152
122
  }, [service]);
153
123
  var subscribe = useCallback(function (handleStoreChange) {
154
124
  var _service$subscribe = service.subscribe(handleStoreChange),
155
- unsubscribe = _service$subscribe.unsubscribe;
156
-
125
+ unsubscribe = _service$subscribe.unsubscribe;
157
126
  return unsubscribe;
158
127
  }, [service]);
159
128
  var storeSnapshot = useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
160
129
  useEffect(function () {
161
- var rehydratedState = options.state;
162
- service.start(rehydratedState ? State.create(rehydratedState) : undefined);
130
+ service.start();
163
131
  return function () {
164
132
  service.stop();
165
133
  service.status = InterpreterStatus.NotStarted;
134
+ service._initState();
166
135
  };
167
136
  }, []);
168
137
  return [storeSnapshot, service.send, service];
@@ -171,8 +140,7 @@ function useMachine(getMachine) {
171
140
  function useActor(actorRef) {
172
141
  var subscribe = useCallback(function (handleStoreChange) {
173
142
  var _actorRef$subscribe = actorRef.subscribe(handleStoreChange),
174
- unsubscribe = _actorRef$subscribe.unsubscribe;
175
-
143
+ unsubscribe = _actorRef$subscribe.unsubscribe;
176
144
  return unsubscribe;
177
145
  }, [actorRef]);
178
146
  var boundGetSnapshot = useCallback(function () {
@@ -188,13 +156,11 @@ function useActor(actorRef) {
188
156
  function defaultCompare(a, b) {
189
157
  return a === b;
190
158
  }
191
-
192
159
  function useSelector(actor, selector) {
193
160
  var compare = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultCompare;
194
161
  var subscribe = useCallback(function (handleStoreChange) {
195
162
  var _actor$subscribe = actor.subscribe(handleStoreChange),
196
- unsubscribe = _actor$subscribe.unsubscribe;
197
-
163
+ unsubscribe = _actor$subscribe.unsubscribe;
198
164
  return unsubscribe;
199
165
  }, [actor]);
200
166
  var boundGetSnapshot = useCallback(function () {
@@ -211,19 +177,145 @@ function useSelector(actor, selector) {
211
177
  * @param behavior The actor behavior to spawn
212
178
  * @returns An ActorRef with the specified `behavior`
213
179
  */
214
-
215
180
  function useSpawn(behavior) {
216
181
  var actorRef = useConstant(function () {
217
182
  // TODO: figure out what to do about the name argument
218
- return new ObservableActorRef(behavior, '');
183
+ return interpret(behavior);
219
184
  });
220
185
  useEffect(function () {
221
- actorRef.start();
186
+ var _actorRef$start;
187
+ (_actorRef$start = actorRef.start) === null || _actorRef$start === void 0 ? void 0 : _actorRef$start.call(actorRef);
222
188
  return function () {
223
- actorRef.stop();
189
+ var _stop, _ref;
190
+ (_stop = (_ref = actorRef).stop) === null || _stop === void 0 ? void 0 : _stop.call(_ref);
224
191
  };
225
192
  }, []);
226
193
  return actorRef;
227
194
  }
228
195
 
229
- export { useActor, useInterpret, useMachine, useSelector, useSpawn };
196
+ function _typeof(obj) {
197
+ "@babel/helpers - typeof";
198
+
199
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
200
+ return typeof obj;
201
+ } : function (obj) {
202
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
203
+ }, _typeof(obj);
204
+ }
205
+
206
+ // From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
207
+ function is(x, y) {
208
+ if (x === y) {
209
+ return x !== 0 || y !== 0 || 1 / x === 1 / y;
210
+ } else {
211
+ return x !== x && y !== y;
212
+ }
213
+ }
214
+ function shallowEqual(objA, objB) {
215
+ if (is(objA, objB)) return true;
216
+ if (_typeof(objA) !== 'object' || objA === null || _typeof(objB) !== 'object' || objB === null) {
217
+ return false;
218
+ }
219
+ var keysA = Object.keys(objA);
220
+ var keysB = Object.keys(objB);
221
+ if (keysA.length !== keysB.length) return false;
222
+ for (var i = 0; i < keysA.length; i++) {
223
+ if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
224
+ return false;
225
+ }
226
+ }
227
+ return true;
228
+ }
229
+
230
+ function _toPrimitive(input, hint) {
231
+ if (typeof input !== "object" || input === null) return input;
232
+ var prim = input[Symbol.toPrimitive];
233
+ if (prim !== undefined) {
234
+ var res = prim.call(input, hint || "default");
235
+ if (typeof res !== "object") return res;
236
+ throw new TypeError("@@toPrimitive must return a primitive value.");
237
+ }
238
+ return (hint === "string" ? String : Number)(input);
239
+ }
240
+
241
+ function _toPropertyKey(arg) {
242
+ var key = _toPrimitive(arg, "string");
243
+ return typeof key === "symbol" ? key : String(key);
244
+ }
245
+
246
+ function _defineProperty(obj, key, value) {
247
+ key = _toPropertyKey(key);
248
+ if (key in obj) {
249
+ Object.defineProperty(obj, key, {
250
+ value: value,
251
+ enumerable: true,
252
+ configurable: true,
253
+ writable: true
254
+ });
255
+ } else {
256
+ obj[key] = value;
257
+ }
258
+ return obj;
259
+ }
260
+
261
+ function ownKeys(object, enumerableOnly) {
262
+ var keys = Object.keys(object);
263
+ if (Object.getOwnPropertySymbols) {
264
+ var symbols = Object.getOwnPropertySymbols(object);
265
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
266
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
267
+ })), keys.push.apply(keys, symbols);
268
+ }
269
+ return keys;
270
+ }
271
+ function _objectSpread2(target) {
272
+ for (var i = 1; i < arguments.length; i++) {
273
+ var source = null != arguments[i] ? arguments[i] : {};
274
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
275
+ _defineProperty(target, key, source[key]);
276
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
277
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
278
+ });
279
+ }
280
+ return target;
281
+ }
282
+
283
+ function createActorContext(machine, interpreterOptions, observerOrListener) {
284
+ var ReactContext = /*#__PURE__*/createContext(null);
285
+ var OriginalProvider = ReactContext.Provider;
286
+ function Provider(_ref) {
287
+ var children = _ref.children,
288
+ _ref$machine = _ref.machine,
289
+ providedMachine = _ref$machine === void 0 ? machine : _ref$machine,
290
+ options = _ref.options;
291
+ var actor = useInterpret(providedMachine, _objectSpread2(_objectSpread2({}, interpreterOptions), options), observerOrListener);
292
+ return /*#__PURE__*/createElement(OriginalProvider, {
293
+ value: actor,
294
+ children: children
295
+ });
296
+ }
297
+ Provider.displayName = "ActorProvider(".concat(machine.id, ")");
298
+ function useContext$1() {
299
+ var actor = useContext(ReactContext);
300
+ if (!actor) {
301
+ throw new Error("You used a hook from \"".concat(Provider.displayName, "\" but it's not inside a <").concat(Provider.displayName, "> component."));
302
+ }
303
+ return actor;
304
+ }
305
+ function useActor$1() {
306
+ var actor = useContext$1();
307
+ return useActor(actor);
308
+ }
309
+ function useSelector$1(selector, compare) {
310
+ var actor = useContext$1();
311
+ return useSelector(actor, selector, compare);
312
+ }
313
+ return {
314
+ Provider: Provider,
315
+ useActorRef: useContext$1,
316
+ useActor: useActor$1,
317
+ useSelector: useSelector$1
318
+ };
319
+ }
320
+
321
+ export { createActorContext, shallowEqual, useActor, useInterpret, useMachine, useSelector, useSpawn };
@@ -2,11 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var useConstant = require('../../dist/useConstant-ff65b597.cjs.dev.js');
6
+ var fsm = require('@xstate/fsm');
5
7
  var React = require('react');
6
- var withSelector = require('use-sync-external-store/shim/with-selector');
7
- var useConstant = require('../../dist/useConstant-644f0e11.cjs.dev.js');
8
8
  var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
9
- var fsm = require('@xstate/fsm');
9
+ var withSelector = require('use-sync-external-store/shim/with-selector');
10
10
 
11
11
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
12
12
 
@@ -15,7 +15,6 @@ var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorp
15
15
  function identity(a) {
16
16
  return a;
17
17
  }
18
-
19
18
  var getServiceState = function getServiceState(service) {
20
19
  var currentValue;
21
20
  service.subscribe(function (state) {
@@ -23,42 +22,35 @@ var getServiceState = function getServiceState(service) {
23
22
  }).unsubscribe();
24
23
  return currentValue;
25
24
  };
26
-
27
25
  function useMachine(stateMachine, options) {
28
26
  var persistedStateRef = React.useRef();
29
-
30
27
  if (process.env.NODE_ENV !== 'production') {
31
28
  var _useState = React.useState(stateMachine),
32
- _useState2 = useConstant._slicedToArray(_useState, 1),
33
- initialMachine = _useState2[0];
34
-
29
+ _useState2 = useConstant._slicedToArray(_useState, 1),
30
+ initialMachine = _useState2[0];
35
31
  if (stateMachine !== initialMachine) {
36
32
  console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' + 'Please make sure that you pass the same Machine as argument each time.');
37
33
  }
38
34
  }
39
-
40
35
  var _useConstant = useConstant.useConstant(function () {
41
- var queue = [];
42
- var service = fsm.interpret(fsm.createMachine(stateMachine.config, options ? options : stateMachine._options));
43
- var send = service.send;
44
-
45
- service.send = function (event) {
46
- if (service.status === fsm.InterpreterStatus.NotStarted) {
47
- queue.push(event);
48
- return;
49
- }
50
-
51
- send(event);
52
- persistedStateRef.current = service.state;
53
- };
54
-
55
- return [service, queue];
56
- }),
57
- _useConstant2 = useConstant._slicedToArray(_useConstant, 2),
58
- service = _useConstant2[0],
59
- queue = _useConstant2[1]; // TODO: consider using `useInsertionEffect` if available
60
-
61
-
36
+ var queue = [];
37
+ var service = fsm.interpret(fsm.createMachine(stateMachine.config, options ? options : stateMachine._options));
38
+ var send = service.send;
39
+ service.send = function (event) {
40
+ if (service.status === fsm.InterpreterStatus.NotStarted) {
41
+ queue.push(event);
42
+ return;
43
+ }
44
+ send(event);
45
+ persistedStateRef.current = service.state;
46
+ };
47
+ return [service, queue];
48
+ }),
49
+ _useConstant2 = useConstant._slicedToArray(_useConstant, 2),
50
+ service = _useConstant2[0],
51
+ queue = _useConstant2[1];
52
+
53
+ // TODO: consider using `useInsertionEffect` if available
62
54
  useIsomorphicLayoutEffect__default['default'](function () {
63
55
  if (options) {
64
56
  service._machine._options = options;
@@ -75,19 +67,16 @@ function useMachine(stateMachine, options) {
75
67
  }, []);
76
68
  return useServiceResult;
77
69
  }
78
-
79
70
  var isEqual = function isEqual(_prevState, nextState) {
80
71
  return nextState.changed === false;
81
72
  };
82
-
83
73
  function useService(service) {
84
74
  var getSnapshot = React.useCallback(function () {
85
75
  return getServiceState(service);
86
76
  }, [service]);
87
77
  var subscribe = React.useCallback(function (handleStoreChange) {
88
78
  var _service$subscribe = service.subscribe(handleStoreChange),
89
- unsubscribe = _service$subscribe.unsubscribe;
90
-
79
+ unsubscribe = _service$subscribe.unsubscribe;
91
80
  return unsubscribe;
92
81
  }, [service]);
93
82
  var storeSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
@@ -2,11 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var fsm = require('@xstate/fsm');
5
6
  var React = require('react');
6
- var withSelector = require('use-sync-external-store/shim/with-selector');
7
7
  var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
8
- var useConstant = require('../../dist/useConstant-eeb49d3f.cjs.prod.js');
9
- var fsm = require('@xstate/fsm');
8
+ var withSelector = require('use-sync-external-store/shim/with-selector');
9
+ var useConstant = require('../../dist/useConstant-c09b427a.cjs.prod.js');
10
10
 
11
11
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
12
12
 
@@ -17,37 +17,36 @@ function _arrayWithHoles(arr) {
17
17
  }
18
18
 
19
19
  function _iterableToArrayLimit(arr, i) {
20
- if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
21
- var _arr = [];
22
- var _n = true;
23
- var _d = false;
24
- var _e = undefined;
25
-
26
- try {
27
- for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
28
- _arr.push(_s.value);
29
-
30
- if (i && _arr.length === i) break;
31
- }
32
- } catch (err) {
33
- _d = true;
34
- _e = err;
35
- } finally {
20
+ var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
21
+ if (null != _i) {
22
+ var _s,
23
+ _e,
24
+ _x,
25
+ _r,
26
+ _arr = [],
27
+ _n = !0,
28
+ _d = !1;
36
29
  try {
37
- if (!_n && _i["return"] != null) _i["return"]();
30
+ if (_x = (_i = _i.call(arr)).next, 0 === i) {
31
+ if (Object(_i) !== _i) return;
32
+ _n = !1;
33
+ } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
34
+ } catch (err) {
35
+ _d = !0, _e = err;
38
36
  } finally {
39
- if (_d) throw _e;
37
+ try {
38
+ if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
39
+ } finally {
40
+ if (_d) throw _e;
41
+ }
40
42
  }
43
+ return _arr;
41
44
  }
42
-
43
- return _arr;
44
45
  }
45
46
 
46
47
  function _arrayLikeToArray(arr, len) {
47
48
  if (len == null || len > arr.length) len = arr.length;
48
-
49
49
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
50
-
51
50
  return arr2;
52
51
  }
53
52
 
@@ -71,7 +70,6 @@ function _slicedToArray(arr, i) {
71
70
  function identity(a) {
72
71
  return a;
73
72
  }
74
-
75
73
  var getServiceState = function getServiceState(service) {
76
74
  var currentValue;
77
75
  service.subscribe(function (state) {
@@ -79,32 +77,27 @@ var getServiceState = function getServiceState(service) {
79
77
  }).unsubscribe();
80
78
  return currentValue;
81
79
  };
82
-
83
80
  function useMachine(stateMachine, options) {
84
81
  var persistedStateRef = React.useRef();
85
-
86
82
  var _useConstant = useConstant.useConstant(function () {
87
- var queue = [];
88
- var service = fsm.interpret(fsm.createMachine(stateMachine.config, options ? options : stateMachine._options));
89
- var send = service.send;
90
-
91
- service.send = function (event) {
92
- if (service.status === fsm.InterpreterStatus.NotStarted) {
93
- queue.push(event);
94
- return;
95
- }
96
-
97
- send(event);
98
- persistedStateRef.current = service.state;
99
- };
100
-
101
- return [service, queue];
102
- }),
103
- _useConstant2 = _slicedToArray(_useConstant, 2),
104
- service = _useConstant2[0],
105
- queue = _useConstant2[1]; // TODO: consider using `useInsertionEffect` if available
106
-
107
-
83
+ var queue = [];
84
+ var service = fsm.interpret(fsm.createMachine(stateMachine.config, options ? options : stateMachine._options));
85
+ var send = service.send;
86
+ service.send = function (event) {
87
+ if (service.status === fsm.InterpreterStatus.NotStarted) {
88
+ queue.push(event);
89
+ return;
90
+ }
91
+ send(event);
92
+ persistedStateRef.current = service.state;
93
+ };
94
+ return [service, queue];
95
+ }),
96
+ _useConstant2 = _slicedToArray(_useConstant, 2),
97
+ service = _useConstant2[0],
98
+ queue = _useConstant2[1];
99
+
100
+ // TODO: consider using `useInsertionEffect` if available
108
101
  useIsomorphicLayoutEffect__default['default'](function () {
109
102
  if (options) {
110
103
  service._machine._options = options;
@@ -121,19 +114,16 @@ function useMachine(stateMachine, options) {
121
114
  }, []);
122
115
  return useServiceResult;
123
116
  }
124
-
125
117
  var isEqual = function isEqual(_prevState, nextState) {
126
118
  return nextState.changed === false;
127
119
  };
128
-
129
120
  function useService(service) {
130
121
  var getSnapshot = React.useCallback(function () {
131
122
  return getServiceState(service);
132
123
  }, [service]);
133
124
  var subscribe = React.useCallback(function (handleStoreChange) {
134
125
  var _service$subscribe = service.subscribe(handleStoreChange),
135
- unsubscribe = _service$subscribe.unsubscribe;
136
-
126
+ unsubscribe = _service$subscribe.unsubscribe;
137
127
  return unsubscribe;
138
128
  }, [service]);
139
129
  var storeSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);