@xstate/react 2.0.0 → 4.0.0-alpha.0

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.
Files changed (68) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/dist/declarations/src/fsm.d.ts +3 -0
  3. package/{es/index.js → dist/declarations/src/index.d.ts} +5 -5
  4. package/dist/declarations/src/types.d.ts +15 -0
  5. package/dist/declarations/src/useActor.d.ts +3 -0
  6. package/{es → dist/declarations/src}/useConstant.d.ts +1 -2
  7. package/dist/declarations/src/useInterpret.d.ts +13 -0
  8. package/dist/declarations/src/useMachine.d.ts +21 -0
  9. package/dist/declarations/src/useSelector.d.ts +2 -0
  10. package/{es → dist/declarations/src}/useSpawn.d.ts +9 -10
  11. package/dist/useConstant-644f0e11.cjs.dev.js +74 -0
  12. package/dist/useConstant-94bfdbb5.esm.js +71 -0
  13. package/dist/useConstant-eeb49d3f.cjs.prod.js +17 -0
  14. package/dist/xstate-react.cjs.d.ts +1 -0
  15. package/dist/xstate-react.cjs.dev.js +241 -0
  16. package/dist/xstate-react.cjs.js +7 -0
  17. package/dist/xstate-react.cjs.prod.js +231 -0
  18. package/dist/xstate-react.esm.js +229 -0
  19. package/fsm/dist/xstate-react-fsm.cjs.d.ts +1 -0
  20. package/fsm/dist/xstate-react-fsm.cjs.dev.js +98 -0
  21. package/fsm/dist/xstate-react-fsm.cjs.js +7 -0
  22. package/fsm/dist/xstate-react-fsm.cjs.prod.js +144 -0
  23. package/fsm/dist/xstate-react-fsm.esm.js +89 -0
  24. package/fsm/package.json +3 -5
  25. package/package.json +25 -48
  26. package/dist/xstate-react-fsm.umd.min.js +0 -28
  27. package/dist/xstate-react.umd.min.js +0 -28
  28. package/es/fsm.d.ts +0 -20
  29. package/es/fsm.js +0 -73
  30. package/es/index.d.ts +0 -6
  31. package/es/types.d.ts +0 -31
  32. package/es/types.js +0 -5
  33. package/es/useActor.d.ts +0 -9
  34. package/es/useActor.js +0 -79
  35. package/es/useConstant.js +0 -8
  36. package/es/useInterpret.d.ts +0 -13
  37. package/es/useInterpret.js +0 -116
  38. package/es/useMachine.d.ts +0 -24
  39. package/es/useMachine.js +0 -78
  40. package/es/useReactEffectActions.d.ts +0 -3
  41. package/es/useReactEffectActions.js +0 -76
  42. package/es/useSelector.d.ts +0 -3
  43. package/es/useSelector.js +0 -70
  44. package/es/useSpawn.js +0 -15
  45. package/es/utils.d.ts +0 -4
  46. package/es/utils.js +0 -53
  47. package/lib/fsm.d.ts +0 -20
  48. package/lib/fsm.js +0 -78
  49. package/lib/index.d.ts +0 -6
  50. package/lib/index.js +0 -15
  51. package/lib/types.d.ts +0 -31
  52. package/lib/types.js +0 -8
  53. package/lib/useActor.d.ts +0 -9
  54. package/lib/useActor.js +0 -84
  55. package/lib/useConstant.d.ts +0 -2
  56. package/lib/useConstant.js +0 -11
  57. package/lib/useInterpret.d.ts +0 -13
  58. package/lib/useInterpret.js +0 -120
  59. package/lib/useMachine.d.ts +0 -24
  60. package/lib/useMachine.js +0 -84
  61. package/lib/useReactEffectActions.d.ts +0 -3
  62. package/lib/useReactEffectActions.js +0 -80
  63. package/lib/useSelector.d.ts +0 -3
  64. package/lib/useSelector.js +0 -74
  65. package/lib/useSpawn.d.ts +0 -10
  66. package/lib/useSpawn.js +0 -19
  67. package/lib/utils.d.ts +0 -4
  68. package/lib/utils.js +0 -58
@@ -0,0 +1,231 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var React = require('react');
6
+ var withSelector = require('use-sync-external-store/shim/with-selector');
7
+ var xstate = require('xstate');
8
+ var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
9
+ var useConstant = require('./useConstant-eeb49d3f.cjs.prod.js');
10
+ var shim = require('use-sync-external-store/shim');
11
+
12
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
13
+
14
+ var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
15
+
16
+ function _objectWithoutPropertiesLoose(source, excluded) {
17
+ if (source == null) return {};
18
+ var target = {};
19
+ var sourceKeys = Object.keys(source);
20
+ var key, i;
21
+
22
+ for (i = 0; i < sourceKeys.length; i++) {
23
+ key = sourceKeys[i];
24
+ if (excluded.indexOf(key) >= 0) continue;
25
+ target[key] = source[key];
26
+ }
27
+
28
+ return target;
29
+ }
30
+
31
+ function _objectWithoutProperties(source, excluded) {
32
+ if (source == null) return {};
33
+ var target = _objectWithoutPropertiesLoose(source, excluded);
34
+ var key, i;
35
+
36
+ if (Object.getOwnPropertySymbols) {
37
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
38
+
39
+ for (i = 0; i < sourceSymbolKeys.length; i++) {
40
+ key = sourceSymbolKeys[i];
41
+ if (excluded.indexOf(key) >= 0) continue;
42
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
43
+ target[key] = source[key];
44
+ }
45
+ }
46
+
47
+ return target;
48
+ }
49
+
50
+ var _excluded = ["context", "actors", "guards", "actions", "delays", "state"];
51
+ function useIdleInterpreter(getMachine, options) {
52
+ var machine = useConstant.useConstant(function () {
53
+ return typeof getMachine === 'function' ? getMachine() : getMachine;
54
+ });
55
+
56
+ var context = options.context,
57
+ actors = options.actors,
58
+ guards = options.guards,
59
+ actions = options.actions,
60
+ delays = options.delays,
61
+ rehydratedState = options.state,
62
+ interpreterOptions = _objectWithoutProperties(options, _excluded);
63
+
64
+ var service = useConstant.useConstant(function () {
65
+ var machineConfig = {
66
+ context: context,
67
+ guards: guards,
68
+ actions: actions,
69
+ actors: actors,
70
+ delays: delays
71
+ };
72
+ var machineWithConfig = machine.provide(machineConfig);
73
+ return xstate.interpret(machineWithConfig, interpreterOptions);
74
+ }); // Make sure options are kept updated when they change.
75
+ // This mutation assignment is safe because the service instance is only used
76
+ // in one place -- this hook's caller.
77
+
78
+ useIsomorphicLayoutEffect__default['default'](function () {
79
+ Object.assign(service.machine.options.actions, actions);
80
+ Object.assign(service.machine.options.guards, guards);
81
+ Object.assign(service.machine.options.actors, actors);
82
+ Object.assign(service.machine.options.delays, delays);
83
+ }, [actions, guards, actors, delays]);
84
+ return service;
85
+ }
86
+ function useInterpret(getMachine) {
87
+ for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
88
+ _ref[_key - 1] = arguments[_key];
89
+ }
90
+
91
+ var _ref$ = _ref[0],
92
+ options = _ref$ === void 0 ? {} : _ref$,
93
+ observerOrListener = _ref[1];
94
+ var service = useIdleInterpreter(getMachine, options);
95
+ React.useEffect(function () {
96
+ if (!observerOrListener) {
97
+ return;
98
+ }
99
+
100
+ var sub = service.subscribe(xstate.toObserver(observerOrListener));
101
+ return function () {
102
+ sub.unsubscribe();
103
+ };
104
+ }, [observerOrListener]);
105
+ React.useEffect(function () {
106
+ var rehydratedState = options.state;
107
+ service.start(rehydratedState ? xstate.State.create(rehydratedState) : undefined);
108
+ return function () {
109
+ service.stop();
110
+ service.status = xstate.InterpreterStatus.NotStarted;
111
+ };
112
+ }, []);
113
+ return service;
114
+ }
115
+
116
+ function identity(a) {
117
+ return a;
118
+ }
119
+
120
+ var isEqual = function isEqual(prevState, nextState) {
121
+ return prevState === nextState || nextState.changed === false;
122
+ };
123
+
124
+ // TODO: rethink how we can do this better
125
+ var cachedRehydratedStates = new WeakMap();
126
+ function useMachine(getMachine) {
127
+ for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
128
+ _ref[_key - 1] = arguments[_key];
129
+ }
130
+
131
+ var _ref$ = _ref[0],
132
+ options = _ref$ === void 0 ? {} : _ref$;
133
+ // using `useIdleInterpreter` allows us to subscribe to the service *before* we start it
134
+ // so we don't miss any notifications
135
+ var service = useIdleInterpreter(getMachine, options);
136
+ var getSnapshot = React.useCallback(function () {
137
+ if (service.status === xstate.InterpreterStatus.NotStarted && options.state) {
138
+ var cached = cachedRehydratedStates.get(options.state);
139
+
140
+ if (cached) {
141
+ return cached;
142
+ }
143
+
144
+ var created = xstate.State.create(options.state);
145
+ cachedRehydratedStates.set(options.state, created);
146
+ return created;
147
+ }
148
+
149
+ return service.getSnapshot();
150
+ }, [service]);
151
+ var subscribe = React.useCallback(function (handleStoreChange) {
152
+ var _service$subscribe = service.subscribe(handleStoreChange),
153
+ unsubscribe = _service$subscribe.unsubscribe;
154
+
155
+ return unsubscribe;
156
+ }, [service]);
157
+ var storeSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
158
+ React.useEffect(function () {
159
+ var rehydratedState = options.state;
160
+ service.start(rehydratedState ? xstate.State.create(rehydratedState) : undefined);
161
+ return function () {
162
+ service.stop();
163
+ service.status = xstate.InterpreterStatus.NotStarted;
164
+ };
165
+ }, []);
166
+ return [storeSnapshot, service.send, service];
167
+ }
168
+
169
+ function useActor(actorRef) {
170
+ var subscribe = React.useCallback(function (handleStoreChange) {
171
+ var _actorRef$subscribe = actorRef.subscribe(handleStoreChange),
172
+ unsubscribe = _actorRef$subscribe.unsubscribe;
173
+
174
+ return unsubscribe;
175
+ }, [actorRef]);
176
+ var boundGetSnapshot = React.useCallback(function () {
177
+ return actorRef.getSnapshot();
178
+ }, [actorRef]);
179
+ var storeSnapshot = shim.useSyncExternalStore(subscribe, boundGetSnapshot, boundGetSnapshot);
180
+ var boundSend = React.useCallback(function (event) {
181
+ return actorRef.send(event);
182
+ }, [actorRef]);
183
+ return [storeSnapshot, boundSend];
184
+ }
185
+
186
+ function defaultCompare(a, b) {
187
+ return a === b;
188
+ }
189
+
190
+ function useSelector(actor, selector) {
191
+ var compare = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultCompare;
192
+ var subscribe = React.useCallback(function (handleStoreChange) {
193
+ var _actor$subscribe = actor.subscribe(handleStoreChange),
194
+ unsubscribe = _actor$subscribe.unsubscribe;
195
+
196
+ return unsubscribe;
197
+ }, [actor]);
198
+ var boundGetSnapshot = React.useCallback(function () {
199
+ return actor.getSnapshot();
200
+ }, [actor]);
201
+ var selectedSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
202
+ return selectedSnapshot;
203
+ }
204
+
205
+ /**
206
+ * React hook that spawns an `ActorRef` with the specified `behavior`.
207
+ * The returned `ActorRef` can be used with the `useActor(actorRef)` hook.
208
+ *
209
+ * @param behavior The actor behavior to spawn
210
+ * @returns An ActorRef with the specified `behavior`
211
+ */
212
+
213
+ function useSpawn(behavior) {
214
+ var actorRef = useConstant.useConstant(function () {
215
+ // TODO: figure out what to do about the name argument
216
+ return new xstate.ObservableActorRef(behavior, '');
217
+ });
218
+ React.useEffect(function () {
219
+ actorRef.start();
220
+ return function () {
221
+ actorRef.stop();
222
+ };
223
+ }, []);
224
+ return actorRef;
225
+ }
226
+
227
+ exports.useActor = useActor;
228
+ exports.useInterpret = useInterpret;
229
+ exports.useMachine = useMachine;
230
+ exports.useSelector = useSelector;
231
+ exports.useSpawn = useSpawn;
@@ -0,0 +1,229 @@
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';
5
+ import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
6
+ import { useSyncExternalStore } from 'use-sync-external-store/shim';
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 _objectWithoutProperties(source, excluded) {
24
+ if (source == null) return {};
25
+ var target = _objectWithoutPropertiesLoose(source, excluded);
26
+ var key, i;
27
+
28
+ if (Object.getOwnPropertySymbols) {
29
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
30
+
31
+ for (i = 0; i < sourceSymbolKeys.length; i++) {
32
+ key = sourceSymbolKeys[i];
33
+ if (excluded.indexOf(key) >= 0) continue;
34
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
35
+ target[key] = source[key];
36
+ }
37
+ }
38
+
39
+ return target;
40
+ }
41
+
42
+ var _excluded = ["context", "actors", "guards", "actions", "delays", "state"];
43
+ function useIdleInterpreter(getMachine, options) {
44
+ var machine = useConstant(function () {
45
+ return typeof getMachine === 'function' ? getMachine() : getMachine;
46
+ });
47
+
48
+ if (process.env.NODE_ENV !== 'production' && typeof getMachine !== 'function') {
49
+ var _useState = useState(machine),
50
+ _useState2 = _slicedToArray(_useState, 1),
51
+ initialMachine = _useState2[0];
52
+
53
+ if (getMachine !== initialMachine) {
54
+ 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
+ }
56
+ }
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
+
66
+ var service = useConstant(function () {
67
+ var machineConfig = {
68
+ context: context,
69
+ guards: guards,
70
+ actions: actions,
71
+ actors: actors,
72
+ delays: delays
73
+ };
74
+ var machineWithConfig = machine.provide(machineConfig);
75
+ return interpret(machineWithConfig, interpreterOptions);
76
+ }); // Make sure options are kept updated when they change.
77
+ // This mutation assignment is safe because the service instance is only used
78
+ // in one place -- this hook's caller.
79
+
80
+ 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);
85
+ }, [actions, guards, actors, delays]);
86
+ return service;
87
+ }
88
+ function useInterpret(getMachine) {
89
+ for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
90
+ _ref[_key - 1] = arguments[_key];
91
+ }
92
+
93
+ var _ref$ = _ref[0],
94
+ options = _ref$ === void 0 ? {} : _ref$,
95
+ observerOrListener = _ref[1];
96
+ var service = useIdleInterpreter(getMachine, options);
97
+ useEffect(function () {
98
+ if (!observerOrListener) {
99
+ return;
100
+ }
101
+
102
+ var sub = service.subscribe(toObserver(observerOrListener));
103
+ return function () {
104
+ sub.unsubscribe();
105
+ };
106
+ }, [observerOrListener]);
107
+ useEffect(function () {
108
+ var rehydratedState = options.state;
109
+ service.start(rehydratedState ? State.create(rehydratedState) : undefined);
110
+ return function () {
111
+ service.stop();
112
+ service.status = InterpreterStatus.NotStarted;
113
+ };
114
+ }, []);
115
+ return service;
116
+ }
117
+
118
+ function identity(a) {
119
+ return a;
120
+ }
121
+
122
+ var isEqual = function isEqual(prevState, nextState) {
123
+ return prevState === nextState || nextState.changed === false;
124
+ };
125
+
126
+ // TODO: rethink how we can do this better
127
+ var cachedRehydratedStates = new WeakMap();
128
+ function useMachine(getMachine) {
129
+ for (var _len = arguments.length, _ref = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
130
+ _ref[_key - 1] = arguments[_key];
131
+ }
132
+
133
+ var _ref$ = _ref[0],
134
+ options = _ref$ === void 0 ? {} : _ref$;
135
+ // using `useIdleInterpreter` allows us to subscribe to the service *before* we start it
136
+ // so we don't miss any notifications
137
+ var service = useIdleInterpreter(getMachine, options);
138
+ 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
+ return service.getSnapshot();
152
+ }, [service]);
153
+ var subscribe = useCallback(function (handleStoreChange) {
154
+ var _service$subscribe = service.subscribe(handleStoreChange),
155
+ unsubscribe = _service$subscribe.unsubscribe;
156
+
157
+ return unsubscribe;
158
+ }, [service]);
159
+ var storeSnapshot = useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
160
+ useEffect(function () {
161
+ var rehydratedState = options.state;
162
+ service.start(rehydratedState ? State.create(rehydratedState) : undefined);
163
+ return function () {
164
+ service.stop();
165
+ service.status = InterpreterStatus.NotStarted;
166
+ };
167
+ }, []);
168
+ return [storeSnapshot, service.send, service];
169
+ }
170
+
171
+ function useActor(actorRef) {
172
+ var subscribe = useCallback(function (handleStoreChange) {
173
+ var _actorRef$subscribe = actorRef.subscribe(handleStoreChange),
174
+ unsubscribe = _actorRef$subscribe.unsubscribe;
175
+
176
+ return unsubscribe;
177
+ }, [actorRef]);
178
+ var boundGetSnapshot = useCallback(function () {
179
+ return actorRef.getSnapshot();
180
+ }, [actorRef]);
181
+ var storeSnapshot = useSyncExternalStore(subscribe, boundGetSnapshot, boundGetSnapshot);
182
+ var boundSend = useCallback(function (event) {
183
+ return actorRef.send(event);
184
+ }, [actorRef]);
185
+ return [storeSnapshot, boundSend];
186
+ }
187
+
188
+ function defaultCompare(a, b) {
189
+ return a === b;
190
+ }
191
+
192
+ function useSelector(actor, selector) {
193
+ var compare = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultCompare;
194
+ var subscribe = useCallback(function (handleStoreChange) {
195
+ var _actor$subscribe = actor.subscribe(handleStoreChange),
196
+ unsubscribe = _actor$subscribe.unsubscribe;
197
+
198
+ return unsubscribe;
199
+ }, [actor]);
200
+ var boundGetSnapshot = useCallback(function () {
201
+ return actor.getSnapshot();
202
+ }, [actor]);
203
+ var selectedSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
204
+ return selectedSnapshot;
205
+ }
206
+
207
+ /**
208
+ * React hook that spawns an `ActorRef` with the specified `behavior`.
209
+ * The returned `ActorRef` can be used with the `useActor(actorRef)` hook.
210
+ *
211
+ * @param behavior The actor behavior to spawn
212
+ * @returns An ActorRef with the specified `behavior`
213
+ */
214
+
215
+ function useSpawn(behavior) {
216
+ var actorRef = useConstant(function () {
217
+ // TODO: figure out what to do about the name argument
218
+ return new ObservableActorRef(behavior, '');
219
+ });
220
+ useEffect(function () {
221
+ actorRef.start();
222
+ return function () {
223
+ actorRef.stop();
224
+ };
225
+ }, []);
226
+ return actorRef;
227
+ }
228
+
229
+ export { useActor, useInterpret, useMachine, useSelector, useSpawn };
@@ -0,0 +1 @@
1
+ export * from "../../dist/declarations/src/fsm";
@@ -0,0 +1,98 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ 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
+ var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
9
+ var fsm = require('@xstate/fsm');
10
+
11
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
12
+
13
+ var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
14
+
15
+ function identity(a) {
16
+ return a;
17
+ }
18
+
19
+ var getServiceState = function getServiceState(service) {
20
+ var currentValue;
21
+ service.subscribe(function (state) {
22
+ currentValue = state;
23
+ }).unsubscribe();
24
+ return currentValue;
25
+ };
26
+
27
+ function useMachine(stateMachine, options) {
28
+ var persistedStateRef = React.useRef();
29
+
30
+ if (process.env.NODE_ENV !== 'production') {
31
+ var _useState = React.useState(stateMachine),
32
+ _useState2 = useConstant._slicedToArray(_useState, 1),
33
+ initialMachine = _useState2[0];
34
+
35
+ if (stateMachine !== initialMachine) {
36
+ 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
+ }
38
+ }
39
+
40
+ 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
+
62
+ useIsomorphicLayoutEffect__default['default'](function () {
63
+ if (options) {
64
+ service._machine._options = options;
65
+ }
66
+ });
67
+ var useServiceResult = useService(service);
68
+ React.useEffect(function () {
69
+ service.start(persistedStateRef.current);
70
+ queue.forEach(service.send);
71
+ persistedStateRef.current = service.state;
72
+ return function () {
73
+ service.stop();
74
+ };
75
+ }, []);
76
+ return useServiceResult;
77
+ }
78
+
79
+ var isEqual = function isEqual(_prevState, nextState) {
80
+ return nextState.changed === false;
81
+ };
82
+
83
+ function useService(service) {
84
+ var getSnapshot = React.useCallback(function () {
85
+ return getServiceState(service);
86
+ }, [service]);
87
+ var subscribe = React.useCallback(function (handleStoreChange) {
88
+ var _service$subscribe = service.subscribe(handleStoreChange),
89
+ unsubscribe = _service$subscribe.unsubscribe;
90
+
91
+ return unsubscribe;
92
+ }, [service]);
93
+ var storeSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
94
+ return [storeSnapshot, service.send, service];
95
+ }
96
+
97
+ exports.useMachine = useMachine;
98
+ exports.useService = useService;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./xstate-react-fsm.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./xstate-react-fsm.cjs.dev.js");
7
+ }