xstate 5.0.0-beta.29 → 5.0.0-beta.31

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 (52) hide show
  1. package/actions/dist/xstate-actions.cjs.js +3 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +3 -3
  3. package/actions/dist/xstate-actions.development.esm.js +3 -3
  4. package/actions/dist/xstate-actions.esm.js +3 -3
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +100 -104
  8. package/actors/dist/xstate-actors.development.cjs.js +100 -104
  9. package/actors/dist/xstate-actors.development.esm.js +100 -104
  10. package/actors/dist/xstate-actors.esm.js +100 -104
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/declarations/src/Machine.d.ts +2 -2
  14. package/dist/declarations/src/State.d.ts +4 -6
  15. package/dist/declarations/src/StateMachine.d.ts +28 -21
  16. package/dist/declarations/src/StateNode.d.ts +3 -3
  17. package/dist/declarations/src/actions/stop.d.ts +1 -1
  18. package/dist/declarations/src/actors/callback.d.ts +8 -8
  19. package/dist/declarations/src/actors/index.d.ts +3 -3
  20. package/dist/declarations/src/actors/observable.d.ts +12 -13
  21. package/dist/declarations/src/actors/promise.d.ts +11 -14
  22. package/dist/declarations/src/actors/transition.d.ts +10 -7
  23. package/dist/declarations/src/index.d.ts +1 -1
  24. package/dist/declarations/src/interpreter.d.ts +4 -4
  25. package/dist/declarations/src/spawn.d.ts +3 -0
  26. package/dist/declarations/src/stateUtils.d.ts +5 -6
  27. package/dist/declarations/src/types.d.ts +74 -69
  28. package/dist/declarations/src/utils.d.ts +4 -4
  29. package/dist/{interpreter-c357bc50.cjs.js → interpreter-05e11c15.cjs.js} +11 -13
  30. package/dist/{interpreter-e2c6a579.development.cjs.js → interpreter-a2236840.development.cjs.js} +12 -14
  31. package/dist/{interpreter-498891b2.esm.js → interpreter-d5fa7ce0.esm.js} +11 -13
  32. package/dist/{interpreter-6e7909c8.development.esm.js → interpreter-e4d2487f.development.esm.js} +12 -14
  33. package/dist/{raise-59f2c242.esm.js → raise-6a68d0cc.esm.js} +61 -32
  34. package/dist/{raise-e778a828.development.esm.js → raise-6fbd4513.development.esm.js} +61 -32
  35. package/dist/{raise-03e57569.cjs.js → raise-90808d65.cjs.js} +61 -32
  36. package/dist/{raise-f751dfac.development.cjs.js → raise-b4bfe138.development.cjs.js} +61 -32
  37. package/dist/{send-f53778f6.development.cjs.js → send-4163d2af.development.cjs.js} +52 -20
  38. package/dist/{send-51717e53.cjs.js → send-72e85cc6.cjs.js} +52 -20
  39. package/dist/{send-42c83fb2.development.esm.js → send-7baeedcb.development.esm.js} +52 -20
  40. package/dist/{send-fff224db.esm.js → send-e5f0f3f6.esm.js} +52 -20
  41. package/dist/xstate.cjs.js +15 -24
  42. package/dist/xstate.development.cjs.js +18 -24
  43. package/dist/xstate.development.esm.js +21 -27
  44. package/dist/xstate.esm.js +18 -27
  45. package/dist/xstate.umd.min.js +1 -1
  46. package/dist/xstate.umd.min.js.map +1 -1
  47. package/guards/dist/xstate-guards.cjs.js +2 -2
  48. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  49. package/guards/dist/xstate-guards.development.esm.js +2 -2
  50. package/guards/dist/xstate-guards.esm.js +2 -2
  51. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  52. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_INIT, j as XSTATE_STOP, d as createActor } from '../../dist/interpreter-6e7909c8.development.esm.js';
1
+ import { X as XSTATE_INIT, j as XSTATE_STOP, d as createActor } from '../../dist/interpreter-e4d2487f.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
@@ -7,21 +7,28 @@ import '../../dev/dist/xstate-dev.development.esm.js';
7
7
  * A transition function is a function that takes the current state and an event and returns the next state.
8
8
  *
9
9
  * @param transition The transition function that returns the next state given the current state and event.
10
- * @param initialState The initial state of the transition function.
10
+ * @param initialContext The initial state of the transition function.
11
11
  * @returns Actor logic
12
12
  */
13
- function fromTransition(transition, initialState) {
13
+ function fromTransition(transition, initialContext) {
14
14
  return {
15
15
  config: transition,
16
16
  transition: (state, event, actorContext) => {
17
- return transition(state, event, actorContext);
17
+ return {
18
+ ...state,
19
+ context: transition(state.context, event, actorContext)
20
+ };
18
21
  },
19
22
  getInitialState: (_, input) => {
20
- return typeof initialState === 'function' ? initialState({
21
- input
22
- }) : initialState;
23
+ return {
24
+ status: 'active',
25
+ output: undefined,
26
+ error: undefined,
27
+ context: typeof initialContext === 'function' ? initialContext({
28
+ input
29
+ }) : initialContext
30
+ };
23
31
  },
24
- getSnapshot: state => state,
25
32
  getPersistedState: state => state,
26
33
  restoreState: state => state
27
34
  };
@@ -44,15 +51,15 @@ function fromCallback(invokeCallback) {
44
51
  }) => {
45
52
  if (event.type === XSTATE_INIT) {
46
53
  const sendBack = eventForParent => {
47
- if (state.canceled) {
54
+ if (state.status === 'stopped') {
48
55
  return;
49
56
  }
50
57
  self._parent?.send(eventForParent);
51
58
  };
52
59
  const receive = newListener => {
53
- state.receivers.add(newListener);
60
+ state._receivers.add(newListener);
54
61
  };
55
- state.dispose = invokeCallback({
62
+ state._dispose = invokeCallback({
56
63
  input: state.input,
57
64
  system,
58
65
  self: self,
@@ -62,30 +69,38 @@ function fromCallback(invokeCallback) {
62
69
  return state;
63
70
  }
64
71
  if (event.type === XSTATE_STOP) {
65
- state.canceled = true;
66
- if (typeof state.dispose === 'function') {
67
- state.dispose();
72
+ state = {
73
+ ...state,
74
+ status: 'stopped',
75
+ error: undefined
76
+ };
77
+ if (typeof state._dispose === 'function') {
78
+ state._dispose();
68
79
  }
69
80
  return state;
70
81
  }
71
- state.receivers.forEach(receiver => receiver(event));
82
+ state._receivers.forEach(receiver => receiver(event));
72
83
  return state;
73
84
  },
74
85
  getInitialState: (_, input) => {
75
86
  return {
76
- canceled: false,
77
- receivers: new Set(),
78
- dispose: undefined,
79
- input
87
+ status: 'active',
88
+ output: undefined,
89
+ error: undefined,
90
+ input,
91
+ _receivers: new Set(),
92
+ _dispose: undefined
80
93
  };
81
94
  },
82
- getSnapshot: () => undefined,
83
95
  getPersistedState: ({
84
- input,
85
- canceled
86
- }) => ({
87
- input,
88
- canceled
96
+ _dispose,
97
+ _receivers,
98
+ ...rest
99
+ }) => rest,
100
+ restoreState: state => ({
101
+ _receivers: new Set(),
102
+ _dispose: undefined,
103
+ ...state
89
104
  })
90
105
  };
91
106
  }
@@ -96,62 +111,57 @@ function fromObservable(observableCreator) {
96
111
  const completeEventType = '$$xstate.complete';
97
112
  return {
98
113
  config: observableCreator,
99
- transition: (state, event, {
114
+ transition: (snapshot, event, {
100
115
  self,
101
116
  id,
102
117
  defer
103
118
  }) => {
104
- if (state.status !== 'active') {
105
- return state;
119
+ if (snapshot.status !== 'active') {
120
+ return snapshot;
106
121
  }
107
122
  switch (event.type) {
108
123
  case nextEventType:
109
- // match the exact timing of events sent by machines
110
- // send actions are not executed immediately
111
- defer(() => {
112
- self._parent?.send({
113
- type: `xstate.snapshot.${id}`,
114
- data: event.data
115
- });
116
- });
117
- return {
118
- ...state,
119
- data: event.data
120
- };
124
+ {
125
+ return {
126
+ ...snapshot,
127
+ context: event.data
128
+ };
129
+ }
121
130
  case errorEventType:
122
131
  return {
123
- ...state,
132
+ ...snapshot,
124
133
  status: 'error',
134
+ error: event.data,
125
135
  input: undefined,
126
- data: event.data,
127
- // TODO: if we keep this as `data` we should reflect this in the type
128
- subscription: undefined
136
+ _subscription: undefined
129
137
  };
130
138
  case completeEventType:
131
139
  return {
132
- ...state,
140
+ ...snapshot,
133
141
  status: 'done',
134
142
  input: undefined,
135
- subscription: undefined
143
+ _subscription: undefined
136
144
  };
137
145
  case XSTATE_STOP:
138
- state.subscription.unsubscribe();
146
+ snapshot._subscription.unsubscribe();
139
147
  return {
140
- ...state,
141
- status: 'canceled',
148
+ ...snapshot,
149
+ status: 'stopped',
142
150
  input: undefined,
143
- subscription: undefined
151
+ _subscription: undefined
144
152
  };
145
153
  default:
146
- return state;
154
+ return snapshot;
147
155
  }
148
156
  },
149
157
  getInitialState: (_, input) => {
150
158
  return {
151
- subscription: undefined,
152
159
  status: 'active',
153
- data: undefined,
154
- input
160
+ output: undefined,
161
+ error: undefined,
162
+ context: undefined,
163
+ input,
164
+ _subscription: undefined
155
165
  };
156
166
  },
157
167
  start: (state, {
@@ -162,7 +172,7 @@ function fromObservable(observableCreator) {
162
172
  // Do not restart a completed observable
163
173
  return;
164
174
  }
165
- state.subscription = observableCreator({
175
+ state._subscription = observableCreator({
166
176
  input: state.input,
167
177
  system,
168
178
  self
@@ -186,20 +196,13 @@ function fromObservable(observableCreator) {
186
196
  }
187
197
  });
188
198
  },
189
- getSnapshot: state => state.data,
190
199
  getPersistedState: ({
191
- status,
192
- data,
193
- input
194
- }) => ({
195
- status,
196
- data,
197
- input
198
- }),
199
- getStatus: state => state,
200
+ _subscription,
201
+ ...state
202
+ }) => state,
200
203
  restoreState: state => ({
201
204
  ...state,
202
- subscription: undefined
205
+ _subscription: undefined
203
206
  })
204
207
  };
205
208
  }
@@ -229,25 +232,24 @@ function fromEventObservable(lazyObservable) {
229
232
  return {
230
233
  ...state,
231
234
  status: 'error',
235
+ error: event.data,
232
236
  input: undefined,
233
- data: event.data,
234
- // TODO: if we keep this as `data` we should reflect this in the type
235
- subscription: undefined
237
+ _subscription: undefined
236
238
  };
237
239
  case completeEventType:
238
240
  return {
239
241
  ...state,
240
242
  status: 'done',
241
243
  input: undefined,
242
- subscription: undefined
244
+ _subscription: undefined
243
245
  };
244
246
  case XSTATE_STOP:
245
- state.subscription.unsubscribe();
247
+ state._subscription.unsubscribe();
246
248
  return {
247
249
  ...state,
248
- status: 'canceled',
250
+ status: 'stopped',
249
251
  input: undefined,
250
- subscription: undefined
252
+ _subscription: undefined
251
253
  };
252
254
  default:
253
255
  return state;
@@ -255,10 +257,12 @@ function fromEventObservable(lazyObservable) {
255
257
  },
256
258
  getInitialState: (_, input) => {
257
259
  return {
258
- subscription: undefined,
259
260
  status: 'active',
260
- data: undefined,
261
- input
261
+ output: undefined,
262
+ error: undefined,
263
+ context: undefined,
264
+ input,
265
+ _subscription: undefined
262
266
  };
263
267
  },
264
268
  start: (state, {
@@ -269,7 +273,7 @@ function fromEventObservable(lazyObservable) {
269
273
  // Do not restart a completed observable
270
274
  return;
271
275
  }
272
- state.subscription = lazyObservable({
276
+ state._subscription = lazyObservable({
273
277
  input: state.input,
274
278
  system,
275
279
  self
@@ -290,20 +294,13 @@ function fromEventObservable(lazyObservable) {
290
294
  }
291
295
  });
292
296
  },
293
- getSnapshot: _ => undefined,
294
297
  getPersistedState: ({
295
- status,
296
- data,
297
- input
298
- }) => ({
299
- status,
300
- data,
301
- input
302
- }),
303
- getStatus: state => state,
298
+ _subscription,
299
+ ...state
300
+ }) => state,
304
301
  restoreState: state => ({
305
302
  ...state,
306
- subscription: undefined
303
+ _subscription: undefined
307
304
  })
308
305
  };
309
306
  }
@@ -322,24 +319,26 @@ promiseCreator) {
322
319
  }
323
320
  switch (event.type) {
324
321
  case resolveEventType:
325
- return {
326
- ...state,
327
- status: 'done',
328
- data: event.data,
329
- input: undefined
330
- };
322
+ {
323
+ const resolvedValue = event.data;
324
+ return {
325
+ ...state,
326
+ status: 'done',
327
+ output: resolvedValue,
328
+ input: undefined
329
+ };
330
+ }
331
331
  case rejectEventType:
332
332
  return {
333
333
  ...state,
334
334
  status: 'error',
335
- data: event.data,
336
- // TODO: if we keep this as `data` we should reflect this in the type
335
+ error: event.data,
337
336
  input: undefined
338
337
  };
339
338
  case XSTATE_STOP:
340
339
  return {
341
340
  ...state,
342
- status: 'canceled',
341
+ status: 'stopped',
343
342
  input: undefined
344
343
  };
345
344
  default:
@@ -361,8 +360,7 @@ promiseCreator) {
361
360
  self
362
361
  }));
363
362
  resolvedPromise.then(response => {
364
- // TODO: remove this condition once dead letter queue lands
365
- if (self._state.status !== 'active') {
363
+ if (self.getSnapshot().status !== 'active') {
366
364
  return;
367
365
  }
368
366
  self.send({
@@ -370,8 +368,7 @@ promiseCreator) {
370
368
  data: response
371
369
  });
372
370
  }, errorData => {
373
- // TODO: remove this condition once dead letter queue lands
374
- if (self._state.status !== 'active') {
371
+ if (self.getSnapshot().status !== 'active') {
375
372
  return;
376
373
  }
377
374
  self.send({
@@ -383,12 +380,11 @@ promiseCreator) {
383
380
  getInitialState: (_, input) => {
384
381
  return {
385
382
  status: 'active',
386
- data: undefined,
383
+ output: undefined,
384
+ error: undefined,
387
385
  input
388
386
  };
389
387
  },
390
- getSnapshot: state => state.data,
391
- getStatus: state => state,
392
388
  getPersistedState: state => state,
393
389
  restoreState: state => state
394
390
  };