xstate 5.0.0-beta.29 → 5.0.0-beta.30

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