@xstate/react 7.0.0-alpha.5 → 7.0.0-alpha.7

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,5 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { Actor, ActorOptions, AnyActorLogic, SnapshotFrom } from 'xstate';
3
+ /** @public */
3
4
  export declare function createActorContext<TLogic extends AnyActorLogic>(actorLogic: TLogic, actorOptions?: ActorOptions<TLogic>): {
4
5
  useSelector: <T>(selector: (snapshot: SnapshotFrom<TLogic>) => T, compare?: (a: T, b: T) => boolean) => T;
5
6
  useActorRef: () => Actor<TLogic>;
@@ -1 +1,2 @@
1
+ /** @public */
1
2
  export declare function shallowEqual(objA: any, objB: any): boolean;
@@ -1,4 +1,5 @@
1
1
  import { Actor, ActorOptions, AnyActorLogic, SnapshotFrom, type ConditionalRequired, type IsNotNever, type RequiredActorOptionsKeys, type RequiredActorOptionsFor } from 'xstate';
2
+ /** @public */
2
3
  export declare function useActor<TLogic extends AnyActorLogic>(logic: TLogic, ...[options]: ConditionalRequired<[
3
4
  options?: ActorOptions<TLogic> & RequiredActorOptionsFor<TLogic>
4
5
  ], IsNotNever<RequiredActorOptionsKeys<TLogic>>>): [SnapshotFrom<TLogic>, Actor<TLogic>['send'], Actor<TLogic>];
@@ -3,6 +3,7 @@ export declare function useIdleActorRef<TLogic extends AnyActorLogic>(logic: TLo
3
3
  options?: ActorOptions<TLogic> & RequiredActorOptionsFor<TLogic>
4
4
  ], IsNotNever<RequiredActorOptionsKeys<TLogic>>>): [Actor<TLogic>, (actorRef: Actor<TLogic>) => void];
5
5
  export declare function useActorLifecycle<TLogic extends AnyActorLogic>(actorRef: Actor<TLogic>, setActorRef: (actorRef: Actor<TLogic>) => void, createReplacement: () => Actor<TLogic>): void;
6
+ /** @public */
6
7
  export declare function useActorRef<TLogic extends AnyActorLogic>(machine: TLogic, ...[options, observerOrListener]: IsNotNever<RequiredActorOptionsKeys<TLogic>> extends true ? [
7
8
  options: ActorOptions<TLogic> & RequiredActorOptionsFor<TLogic>,
8
9
  observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)
@@ -1,5 +1,10 @@
1
1
  import { Actor, ActorOptions, AnyStateMachine, StateFrom, type ConditionalRequired, type IsNotNever, type RequiredActorOptionsKeys, type RequiredActorOptionsFor } from 'xstate';
2
- /** @alias useActor */
2
+ /**
3
+ * @alias useActor
4
+ *
5
+ * @deprecated Use `useActor(machine, options)`.
6
+ * @public
7
+ */
3
8
  export declare function useMachine<TMachine extends AnyStateMachine>(machine: TMachine, ...[options]: ConditionalRequired<[
4
9
  options?: ActorOptions<TMachine> & RequiredActorOptionsFor<TMachine>
5
10
  ], IsNotNever<RequiredActorOptionsKeys<TMachine>>>): [StateFrom<TMachine>, Actor<TMachine>['send'], Actor<TMachine>];
@@ -1,4 +1,5 @@
1
1
  import { AnyActorRef } from 'xstate';
2
+ /** @public */
2
3
  export declare function useSelector<TActor extends Pick<AnyActorRef, 'subscribe' | 'getSnapshot'> | undefined, T>(actor: TActor, selector: (snapshot: TActor extends {
3
4
  getSnapshot(): infer TSnapshot;
4
5
  } ? TSnapshot : undefined) => T, compare?: (a: T, b: T) => boolean): T;
@@ -31,9 +31,19 @@ function _interopNamespace(e) {
31
31
  var React__namespace = /*#__PURE__*/_interopNamespace(React);
32
32
  var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
33
33
 
34
+ function isStateMachine(logic) {
35
+ return typeof logic.provide === 'function' && !!logic.config && !!logic.sources;
36
+ }
37
+
38
+ // The hook updates the sources of its machine on later renders. A per-hook
39
+ // copy keeps those updates from reaching the caller's machine, which may be
40
+ // shared by other actors.
41
+ function ownLogic(logic) {
42
+ return isStateMachine(logic) ? logic.provide({}) : logic;
43
+ }
34
44
  function useIdleActorRef(logic, ...[options]) {
35
45
  const [actorRef, setActorRef] = React.useState(() => {
36
- return xstate.createActor(logic, options);
46
+ return xstate.createActor(ownLogic(logic), options);
37
47
  });
38
48
  // An object whose identity changes only when React Fast Refresh re-renders
39
49
  // this component. Fast Refresh ignores dependency lists while it applies an
@@ -51,7 +61,9 @@ function useIdleActorRef(logic, ...[options]) {
51
61
  refreshSignalRef.current !== refreshSignal;
52
62
  refreshSignalRef.current = refreshSignal;
53
63
  if (logic.config === currentLogic.config) {
54
- currentLogic.sources = logic.sources;
64
+ if (isStateMachine(logic) && isStateMachine(currentLogic)) {
65
+ currentLogic.sources = logic.sources;
66
+ }
55
67
  return;
56
68
  }
57
69
  });
@@ -91,6 +103,8 @@ function useActorLifecycle(actorRef, setActorRef, createReplacement) {
91
103
  };
92
104
  }, [actorRef]);
93
105
  }
106
+
107
+ /** @public */
94
108
  function useActorRef(machine, ...[options, observerOrListener]) {
95
109
  const [actorRef, setActorRef] = useIdleActorRef(machine, options);
96
110
  React.useEffect(() => {
@@ -109,6 +123,8 @@ function useActorRef(machine, ...[options, observerOrListener]) {
109
123
  function defaultCompare(a, b) {
110
124
  return a === b;
111
125
  }
126
+
127
+ /** @public */
112
128
  function useSelector(actor, selector, compare = defaultCompare) {
113
129
  const subscribe = React.useCallback(handleStoreChange => {
114
130
  if (!actor) {
@@ -133,6 +149,7 @@ function useSelector(actor, selector, compare = defaultCompare) {
133
149
  return selectedSnapshot;
134
150
  }
135
151
 
152
+ /** @public */
136
153
  function createActorContext(actorLogic, actorOptions) {
137
154
  const ReactContext = /*#__PURE__*/React__namespace.createContext(null);
138
155
  const OriginalProvider = ReactContext.Provider;
@@ -179,6 +196,8 @@ function is(x, y) {
179
196
  return x !== x && y !== y;
180
197
  }
181
198
  }
199
+
200
+ /** @public */
182
201
  function shallowEqual(objA, objB) {
183
202
  if (is(objA, objB)) return true;
184
203
  if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
@@ -195,6 +214,7 @@ function shallowEqual(objA, objB) {
195
214
  return true;
196
215
  }
197
216
 
217
+ /** @public */
198
218
  function useActor(logic, ...[options]) {
199
219
  const [actorRef, setActorRef] = useIdleActorRef(logic, options);
200
220
  const getSnapshot = React.useCallback(() => {
@@ -218,7 +238,12 @@ function useActor(logic, ...[options]) {
218
238
  return [actorSnapshot, actorRef.send, actorRef];
219
239
  }
220
240
 
221
- /** @alias useActor */
241
+ /**
242
+ * @alias useActor
243
+ *
244
+ * @deprecated Use `useActor(machine, options)`.
245
+ * @public
246
+ */
222
247
  function useMachine(machine, ...[options]) {
223
248
  return useActor(machine, options);
224
249
  }
@@ -31,9 +31,19 @@ function _interopNamespace(e) {
31
31
  var React__namespace = /*#__PURE__*/_interopNamespace(React);
32
32
  var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
33
33
 
34
+ function isStateMachine(logic) {
35
+ return typeof logic.provide === 'function' && !!logic.config && !!logic.sources;
36
+ }
37
+
38
+ // The hook updates the sources of its machine on later renders. A per-hook
39
+ // copy keeps those updates from reaching the caller's machine, which may be
40
+ // shared by other actors.
41
+ function ownLogic(logic) {
42
+ return isStateMachine(logic) ? logic.provide({}) : logic;
43
+ }
34
44
  function useIdleActorRef(logic, ...[options]) {
35
45
  const [actorRef, setActorRef] = React.useState(() => {
36
- return xstate.createActor(logic, options);
46
+ return xstate.createActor(ownLogic(logic), options);
37
47
  });
38
48
  // An object whose identity changes only when React Fast Refresh re-renders
39
49
  // this component. Fast Refresh ignores dependency lists while it applies an
@@ -51,7 +61,9 @@ function useIdleActorRef(logic, ...[options]) {
51
61
  const refreshed = refreshSignalRef.current !== refreshSignal;
52
62
  refreshSignalRef.current = refreshSignal;
53
63
  if (logic.config === currentLogic.config) {
54
- currentLogic.sources = logic.sources;
64
+ if (isStateMachine(logic) && isStateMachine(currentLogic)) {
65
+ currentLogic.sources = logic.sources;
66
+ }
55
67
  return;
56
68
  }
57
69
 
@@ -59,8 +71,9 @@ function useIdleActorRef(logic, ...[options]) {
59
71
  // defines the machine. Keep the running actor and carry its live snapshot
60
72
  // over to the new machine; start a fresh actor if it cannot be carried.
61
73
  if (refreshed) {
62
- if (!xstate._hotSwapActorLogic(actorRef, logic)) {
63
- setActorRef(xstate.createActor(logic, options));
74
+ const nextLogic = ownLogic(logic);
75
+ if (!xstate._hotSwapActorLogic(actorRef, nextLogic)) {
76
+ setActorRef(xstate.createActor(nextLogic, options));
64
77
  }
65
78
  }
66
79
  });
@@ -100,6 +113,8 @@ function useActorLifecycle(actorRef, setActorRef, createReplacement) {
100
113
  };
101
114
  }, [actorRef]);
102
115
  }
116
+
117
+ /** @public */
103
118
  function useActorRef(machine, ...[options, observerOrListener]) {
104
119
  const [actorRef, setActorRef] = useIdleActorRef(machine, options);
105
120
  React.useEffect(() => {
@@ -118,6 +133,8 @@ function useActorRef(machine, ...[options, observerOrListener]) {
118
133
  function defaultCompare(a, b) {
119
134
  return a === b;
120
135
  }
136
+
137
+ /** @public */
121
138
  function useSelector(actor, selector, compare = defaultCompare) {
122
139
  const subscribe = React.useCallback(handleStoreChange => {
123
140
  if (!actor) {
@@ -142,6 +159,7 @@ function useSelector(actor, selector, compare = defaultCompare) {
142
159
  return selectedSnapshot;
143
160
  }
144
161
 
162
+ /** @public */
145
163
  function createActorContext(actorLogic, actorOptions) {
146
164
  const ReactContext = /*#__PURE__*/React__namespace.createContext(null);
147
165
  const OriginalProvider = ReactContext.Provider;
@@ -188,6 +206,8 @@ function is(x, y) {
188
206
  return x !== x && y !== y;
189
207
  }
190
208
  }
209
+
210
+ /** @public */
191
211
  function shallowEqual(objA, objB) {
192
212
  if (is(objA, objB)) return true;
193
213
  if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
@@ -204,6 +224,7 @@ function shallowEqual(objA, objB) {
204
224
  return true;
205
225
  }
206
226
 
227
+ /** @public */
207
228
  function useActor(logic, ...[options]) {
208
229
  if (!!logic && 'send' in logic && typeof logic.send === 'function') {
209
230
  throw new Error(`useActor() expects actor logic (e.g. a machine), but received an ActorRef. Use the useSelector(actorRef, ...) hook instead to read the ActorRef's snapshot.`);
@@ -230,7 +251,12 @@ function useActor(logic, ...[options]) {
230
251
  return [actorSnapshot, actorRef.send, actorRef];
231
252
  }
232
253
 
233
- /** @alias useActor */
254
+ /**
255
+ * @alias useActor
256
+ *
257
+ * @deprecated Use `useActor(machine, options)`.
258
+ * @public
259
+ */
234
260
  function useMachine(machine, ...[options]) {
235
261
  return useActor(machine, options);
236
262
  }
@@ -5,9 +5,19 @@ import { _toObserver, createActor, _hotSwapActorLogic } from 'xstate';
5
5
  import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
6
6
  import { useSyncExternalStore } from 'use-sync-external-store/shim';
7
7
 
8
+ function isStateMachine(logic) {
9
+ return typeof logic.provide === 'function' && !!logic.config && !!logic.sources;
10
+ }
11
+
12
+ // The hook updates the sources of its machine on later renders. A per-hook
13
+ // copy keeps those updates from reaching the caller's machine, which may be
14
+ // shared by other actors.
15
+ function ownLogic(logic) {
16
+ return isStateMachine(logic) ? logic.provide({}) : logic;
17
+ }
8
18
  function useIdleActorRef(logic, ...[options]) {
9
19
  const [actorRef, setActorRef] = useState(() => {
10
- return createActor(logic, options);
20
+ return createActor(ownLogic(logic), options);
11
21
  });
12
22
  // An object whose identity changes only when React Fast Refresh re-renders
13
23
  // this component. Fast Refresh ignores dependency lists while it applies an
@@ -25,7 +35,9 @@ function useIdleActorRef(logic, ...[options]) {
25
35
  const refreshed = refreshSignalRef.current !== refreshSignal;
26
36
  refreshSignalRef.current = refreshSignal;
27
37
  if (logic.config === currentLogic.config) {
28
- currentLogic.sources = logic.sources;
38
+ if (isStateMachine(logic) && isStateMachine(currentLogic)) {
39
+ currentLogic.sources = logic.sources;
40
+ }
29
41
  return;
30
42
  }
31
43
 
@@ -33,8 +45,9 @@ function useIdleActorRef(logic, ...[options]) {
33
45
  // defines the machine. Keep the running actor and carry its live snapshot
34
46
  // over to the new machine; start a fresh actor if it cannot be carried.
35
47
  if (refreshed) {
36
- if (!_hotSwapActorLogic(actorRef, logic)) {
37
- setActorRef(createActor(logic, options));
48
+ const nextLogic = ownLogic(logic);
49
+ if (!_hotSwapActorLogic(actorRef, nextLogic)) {
50
+ setActorRef(createActor(nextLogic, options));
38
51
  }
39
52
  }
40
53
  });
@@ -74,6 +87,8 @@ function useActorLifecycle(actorRef, setActorRef, createReplacement) {
74
87
  };
75
88
  }, [actorRef]);
76
89
  }
90
+
91
+ /** @public */
77
92
  function useActorRef(machine, ...[options, observerOrListener]) {
78
93
  const [actorRef, setActorRef] = useIdleActorRef(machine, options);
79
94
  useEffect(() => {
@@ -92,6 +107,8 @@ function useActorRef(machine, ...[options, observerOrListener]) {
92
107
  function defaultCompare(a, b) {
93
108
  return a === b;
94
109
  }
110
+
111
+ /** @public */
95
112
  function useSelector(actor, selector, compare = defaultCompare) {
96
113
  const subscribe = useCallback(handleStoreChange => {
97
114
  if (!actor) {
@@ -116,6 +133,7 @@ function useSelector(actor, selector, compare = defaultCompare) {
116
133
  return selectedSnapshot;
117
134
  }
118
135
 
136
+ /** @public */
119
137
  function createActorContext(actorLogic, actorOptions) {
120
138
  const ReactContext = /*#__PURE__*/React.createContext(null);
121
139
  const OriginalProvider = ReactContext.Provider;
@@ -162,6 +180,8 @@ function is(x, y) {
162
180
  return x !== x && y !== y;
163
181
  }
164
182
  }
183
+
184
+ /** @public */
165
185
  function shallowEqual(objA, objB) {
166
186
  if (is(objA, objB)) return true;
167
187
  if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
@@ -178,6 +198,7 @@ function shallowEqual(objA, objB) {
178
198
  return true;
179
199
  }
180
200
 
201
+ /** @public */
181
202
  function useActor(logic, ...[options]) {
182
203
  if (!!logic && 'send' in logic && typeof logic.send === 'function') {
183
204
  throw new Error(`useActor() expects actor logic (e.g. a machine), but received an ActorRef. Use the useSelector(actorRef, ...) hook instead to read the ActorRef's snapshot.`);
@@ -204,7 +225,12 @@ function useActor(logic, ...[options]) {
204
225
  return [actorSnapshot, actorRef.send, actorRef];
205
226
  }
206
227
 
207
- /** @alias useActor */
228
+ /**
229
+ * @alias useActor
230
+ *
231
+ * @deprecated Use `useActor(machine, options)`.
232
+ * @public
233
+ */
208
234
  function useMachine(machine, ...[options]) {
209
235
  return useActor(machine, options);
210
236
  }
@@ -5,9 +5,19 @@ import { _toObserver, createActor } from 'xstate';
5
5
  import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
6
6
  import { useSyncExternalStore } from 'use-sync-external-store/shim';
7
7
 
8
+ function isStateMachine(logic) {
9
+ return typeof logic.provide === 'function' && !!logic.config && !!logic.sources;
10
+ }
11
+
12
+ // The hook updates the sources of its machine on later renders. A per-hook
13
+ // copy keeps those updates from reaching the caller's machine, which may be
14
+ // shared by other actors.
15
+ function ownLogic(logic) {
16
+ return isStateMachine(logic) ? logic.provide({}) : logic;
17
+ }
8
18
  function useIdleActorRef(logic, ...[options]) {
9
19
  const [actorRef, setActorRef] = useState(() => {
10
- return createActor(logic, options);
20
+ return createActor(ownLogic(logic), options);
11
21
  });
12
22
  // An object whose identity changes only when React Fast Refresh re-renders
13
23
  // this component. Fast Refresh ignores dependency lists while it applies an
@@ -25,7 +35,9 @@ function useIdleActorRef(logic, ...[options]) {
25
35
  refreshSignalRef.current !== refreshSignal;
26
36
  refreshSignalRef.current = refreshSignal;
27
37
  if (logic.config === currentLogic.config) {
28
- currentLogic.sources = logic.sources;
38
+ if (isStateMachine(logic) && isStateMachine(currentLogic)) {
39
+ currentLogic.sources = logic.sources;
40
+ }
29
41
  return;
30
42
  }
31
43
  });
@@ -65,6 +77,8 @@ function useActorLifecycle(actorRef, setActorRef, createReplacement) {
65
77
  };
66
78
  }, [actorRef]);
67
79
  }
80
+
81
+ /** @public */
68
82
  function useActorRef(machine, ...[options, observerOrListener]) {
69
83
  const [actorRef, setActorRef] = useIdleActorRef(machine, options);
70
84
  useEffect(() => {
@@ -83,6 +97,8 @@ function useActorRef(machine, ...[options, observerOrListener]) {
83
97
  function defaultCompare(a, b) {
84
98
  return a === b;
85
99
  }
100
+
101
+ /** @public */
86
102
  function useSelector(actor, selector, compare = defaultCompare) {
87
103
  const subscribe = useCallback(handleStoreChange => {
88
104
  if (!actor) {
@@ -107,6 +123,7 @@ function useSelector(actor, selector, compare = defaultCompare) {
107
123
  return selectedSnapshot;
108
124
  }
109
125
 
126
+ /** @public */
110
127
  function createActorContext(actorLogic, actorOptions) {
111
128
  const ReactContext = /*#__PURE__*/React.createContext(null);
112
129
  const OriginalProvider = ReactContext.Provider;
@@ -153,6 +170,8 @@ function is(x, y) {
153
170
  return x !== x && y !== y;
154
171
  }
155
172
  }
173
+
174
+ /** @public */
156
175
  function shallowEqual(objA, objB) {
157
176
  if (is(objA, objB)) return true;
158
177
  if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
@@ -169,6 +188,7 @@ function shallowEqual(objA, objB) {
169
188
  return true;
170
189
  }
171
190
 
191
+ /** @public */
172
192
  function useActor(logic, ...[options]) {
173
193
  const [actorRef, setActorRef] = useIdleActorRef(logic, options);
174
194
  const getSnapshot = useCallback(() => {
@@ -192,7 +212,12 @@ function useActor(logic, ...[options]) {
192
212
  return [actorSnapshot, actorRef.send, actorRef];
193
213
  }
194
214
 
195
- /** @alias useActor */
215
+ /**
216
+ * @alias useActor
217
+ *
218
+ * @deprecated Use `useActor(machine, options)`.
219
+ * @public
220
+ */
196
221
  function useMachine(machine, ...[options]) {
197
222
  return useActor(machine, options);
198
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xstate/react",
3
- "version": "7.0.0-alpha.5",
3
+ "version": "7.0.0-alpha.7",
4
4
  "description": "XState tools for React",
5
5
  "keywords": [
6
6
  "state",
@@ -54,12 +54,7 @@
54
54
  },
55
55
  "peerDependencies": {
56
56
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
57
- "xstate": "^6.0.0-alpha.68"
58
- },
59
- "peerDependenciesMeta": {
60
- "xstate": {
61
- "optional": true
62
- }
57
+ "xstate": "^6.0.0-alpha.73"
63
58
  },
64
59
  "dependencies": {
65
60
  "use-isomorphic-layout-effect": "^1.1.2",
@@ -74,7 +69,7 @@
74
69
  "react-dom": "^19.0.0",
75
70
  "rxjs": "^7.8.1",
76
71
  "zod": "^3.25.51",
77
- "xstate": "^6.0.0-alpha.68"
72
+ "xstate": "^6.0.0-alpha.73"
78
73
  },
79
74
  "scripts": {}
80
75
  }