@xstate/react 4.0.0-alpha.1 → 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,4 +1,4 @@
1
- import { _ as _slicedToArray, u as useConstant } from '../../dist/useConstant-94bfdbb5.esm.js';
1
+ import { _ as _slicedToArray, u as useConstant } from '../../dist/useConstant-0013a606.esm.js';
2
2
  import { interpret, createMachine, InterpreterStatus } from '@xstate/fsm';
3
3
  import { useRef, useState, useEffect, useCallback } from 'react';
4
4
  import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
@@ -7,7 +7,6 @@ import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/w
7
7
  function identity(a) {
8
8
  return a;
9
9
  }
10
-
11
10
  var getServiceState = function getServiceState(service) {
12
11
  var currentValue;
13
12
  service.subscribe(function (state) {
@@ -15,42 +14,35 @@ var getServiceState = function getServiceState(service) {
15
14
  }).unsubscribe();
16
15
  return currentValue;
17
16
  };
18
-
19
17
  function useMachine(stateMachine, options) {
20
18
  var persistedStateRef = useRef();
21
-
22
19
  if (process.env.NODE_ENV !== 'production') {
23
20
  var _useState = useState(stateMachine),
24
- _useState2 = _slicedToArray(_useState, 1),
25
- initialMachine = _useState2[0];
26
-
21
+ _useState2 = _slicedToArray(_useState, 1),
22
+ initialMachine = _useState2[0];
27
23
  if (stateMachine !== initialMachine) {
28
24
  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.');
29
25
  }
30
26
  }
31
-
32
27
  var _useConstant = useConstant(function () {
33
- var queue = [];
34
- var service = interpret(createMachine(stateMachine.config, options ? options : stateMachine._options));
35
- var send = service.send;
36
-
37
- service.send = function (event) {
38
- if (service.status === InterpreterStatus.NotStarted) {
39
- queue.push(event);
40
- return;
41
- }
42
-
43
- send(event);
44
- persistedStateRef.current = service.state;
45
- };
46
-
47
- return [service, queue];
48
- }),
49
- _useConstant2 = _slicedToArray(_useConstant, 2),
50
- service = _useConstant2[0],
51
- queue = _useConstant2[1]; // TODO: consider using `useInsertionEffect` if available
52
-
28
+ var queue = [];
29
+ var service = interpret(createMachine(stateMachine.config, options ? options : stateMachine._options));
30
+ var send = service.send;
31
+ service.send = function (event) {
32
+ if (service.status === InterpreterStatus.NotStarted) {
33
+ queue.push(event);
34
+ return;
35
+ }
36
+ send(event);
37
+ persistedStateRef.current = service.state;
38
+ };
39
+ return [service, queue];
40
+ }),
41
+ _useConstant2 = _slicedToArray(_useConstant, 2),
42
+ service = _useConstant2[0],
43
+ queue = _useConstant2[1];
53
44
 
45
+ // TODO: consider using `useInsertionEffect` if available
54
46
  useIsomorphicLayoutEffect(function () {
55
47
  if (options) {
56
48
  service._machine._options = options;
@@ -67,19 +59,16 @@ function useMachine(stateMachine, options) {
67
59
  }, []);
68
60
  return useServiceResult;
69
61
  }
70
-
71
62
  var isEqual = function isEqual(_prevState, nextState) {
72
63
  return nextState.changed === false;
73
64
  };
74
-
75
65
  function useService(service) {
76
66
  var getSnapshot = useCallback(function () {
77
67
  return getServiceState(service);
78
68
  }, [service]);
79
69
  var subscribe = useCallback(function (handleStoreChange) {
80
70
  var _service$subscribe = service.subscribe(handleStoreChange),
81
- unsubscribe = _service$subscribe.unsubscribe;
82
-
71
+ unsubscribe = _service$subscribe.unsubscribe;
83
72
  return unsubscribe;
84
73
  }, [service]);
85
74
  var storeSnapshot = useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xstate/react",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-alpha.2",
4
4
  "description": "XState tools for React",
5
5
  "keywords": [
6
6
  "state",
@@ -45,7 +45,7 @@
45
45
  "peerDependencies": {
46
46
  "@xstate/fsm": "^3.0.0-alpha.0",
47
47
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
48
- "xstate": "^5.0.0-alpha.1"
48
+ "xstate": "^5.0.0-alpha.3"
49
49
  },
50
50
  "peerDependenciesMeta": {
51
51
  "@xstate/fsm": {
@@ -60,7 +60,7 @@
60
60
  "use-sync-external-store": "^1.0.0"
61
61
  },
62
62
  "devDependencies": {
63
- "@testing-library/react": "^13.0.0",
63
+ "@testing-library/react": "^13.4.0",
64
64
  "@types/jsdom": "^12.2.3",
65
65
  "@types/react": "^17.0.43",
66
66
  "@types/react-dom": "^17.0.14",
@@ -70,7 +70,7 @@
70
70
  "jsdom-global": "^3.0.2",
71
71
  "react": "^18.0.0",
72
72
  "react-dom": "^18.0.0",
73
- "xstate": "5.0.0-alpha.1"
73
+ "xstate": "5.0.0-alpha.3"
74
74
  },
75
75
  "preconstruct": {
76
76
  "entrypoints": [