xstate 5.0.0-beta.47 → 5.0.0-beta.49

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 (43) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  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 +52 -71
  8. package/actors/dist/xstate-actors.development.cjs.js +52 -71
  9. package/actors/dist/xstate-actors.development.esm.js +52 -71
  10. package/actors/dist/xstate-actors.esm.js +52 -71
  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 +20 -17
  14. package/dist/declarations/src/StateMachine.d.ts +13 -14
  15. package/dist/declarations/src/actors/callback.d.ts +2 -5
  16. package/dist/declarations/src/actors/index.d.ts +3 -3
  17. package/dist/declarations/src/actors/promise.d.ts +4 -4
  18. package/dist/declarations/src/createMachine.d.ts +17 -2
  19. package/dist/declarations/src/interpreter.d.ts +30 -5
  20. package/dist/declarations/src/setup.d.ts +21 -5
  21. package/dist/declarations/src/stateUtils.d.ts +1 -1
  22. package/dist/declarations/src/types.d.ts +14 -10
  23. package/dist/declarations/src/utils.d.ts +0 -1
  24. package/dist/{raise-156f5f68.development.esm.js → raise-1873c645.development.esm.js} +31 -9
  25. package/dist/{raise-7dc97582.development.cjs.js → raise-495f4b9f.development.cjs.js} +31 -9
  26. package/dist/{raise-7af24c22.cjs.js → raise-8f9c4a5a.cjs.js} +31 -9
  27. package/dist/{raise-91ec8b4f.esm.js → raise-e4cc6d4f.esm.js} +31 -9
  28. package/dist/{send-7489590c.development.esm.js → send-0a381ca2.development.esm.js} +1 -1
  29. package/dist/{send-ed326064.esm.js → send-22880315.esm.js} +1 -1
  30. package/dist/{send-bc0d3e22.development.cjs.js → send-8d30b415.development.cjs.js} +1 -1
  31. package/dist/{send-401f5c30.cjs.js → send-8ed5c8b2.cjs.js} +1 -1
  32. package/dist/xstate.cjs.js +13 -12
  33. package/dist/xstate.development.cjs.js +13 -12
  34. package/dist/xstate.development.esm.js +15 -14
  35. package/dist/xstate.esm.js +15 -14
  36. package/dist/xstate.umd.min.js +1 -1
  37. package/dist/xstate.umd.min.js.map +1 -1
  38. package/guards/dist/xstate-guards.cjs.js +1 -1
  39. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  40. package/guards/dist/xstate-guards.development.esm.js +1 -1
  41. package/guards/dist/xstate-guards.esm.js +1 -1
  42. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  43. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-156f5f68.development.esm.js';
1
+ import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-1873c645.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
@@ -83,6 +83,7 @@ function fromTransition(transition, initialContext) {
83
83
  };
84
84
  }
85
85
 
86
+ const instanceStates = /* #__PURE__ */new WeakMap();
86
87
  /**
87
88
  * An actor logic creator which returns callback logic as defined by a callback function.
88
89
  *
@@ -143,51 +144,46 @@ function fromTransition(transition, initialContext) {
143
144
  function fromCallback(invokeCallback) {
144
145
  const logic = {
145
146
  config: invokeCallback,
146
- start: (_state, {
147
- self,
148
- system
149
- }) => {
150
- system._relay(self, self, {
151
- type: 'xstate.create'
152
- });
153
- },
154
- transition: (state, event, {
155
- self,
156
- system
157
- }) => {
158
- if (event.type === 'xstate.create') {
159
- const sendBack = eventForParent => {
160
- if (state.status === 'stopped') {
147
+ start: (state, actorScope) => {
148
+ const {
149
+ self,
150
+ system
151
+ } = actorScope;
152
+ const callbackState = {
153
+ receivers: undefined,
154
+ dispose: undefined
155
+ };
156
+ instanceStates.set(self, callbackState);
157
+ callbackState.dispose = invokeCallback({
158
+ input: state.input,
159
+ system,
160
+ self,
161
+ sendBack: event => {
162
+ if (self.getSnapshot().status === 'stopped') {
161
163
  return;
162
164
  }
163
165
  if (self._parent) {
164
- system._relay(self, self._parent, eventForParent);
166
+ system._relay(self, self._parent, event);
165
167
  }
166
- };
167
- const receive = newListener => {
168
- state._receivers.add(newListener);
169
- };
170
- state._dispose = invokeCallback({
171
- input: state.input,
172
- system,
173
- self: self,
174
- sendBack,
175
- receive
176
- });
177
- return state;
178
- }
168
+ },
169
+ receive: listener => {
170
+ callbackState.receivers ??= new Set();
171
+ callbackState.receivers.add(listener);
172
+ }
173
+ });
174
+ },
175
+ transition: (state, event, actorScope) => {
176
+ const callbackState = instanceStates.get(actorScope.self);
179
177
  if (event.type === XSTATE_STOP) {
180
178
  state = {
181
179
  ...state,
182
180
  status: 'stopped',
183
181
  error: undefined
184
182
  };
185
- if (typeof state._dispose === 'function') {
186
- state._dispose();
187
- }
183
+ callbackState.dispose?.();
188
184
  return state;
189
185
  }
190
- state._receivers.forEach(receiver => receiver(event));
186
+ callbackState.receivers?.forEach(receiver => receiver(event));
191
187
  return state;
192
188
  },
193
189
  getInitialState: (_, input) => {
@@ -195,25 +191,18 @@ function fromCallback(invokeCallback) {
195
191
  status: 'active',
196
192
  output: undefined,
197
193
  error: undefined,
198
- input,
199
- _receivers: new Set(),
200
- _dispose: undefined
194
+ input
201
195
  };
202
196
  },
203
- getPersistedState: ({
204
- _dispose,
205
- _receivers,
206
- ...rest
207
- }) => rest,
208
- restoreState: state => ({
209
- _receivers: new Set(),
210
- _dispose: undefined,
211
- ...state
212
- })
197
+ getPersistedState: state => state,
198
+ restoreState: state => state
213
199
  };
214
200
  return logic;
215
201
  }
216
202
 
203
+ const XSTATE_OBSERVABLE_NEXT = 'xstate.observable.next';
204
+ const XSTATE_OBSERVABLE_ERROR = 'xstate.observable.error';
205
+ const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
217
206
  /**
218
207
  * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
219
208
  *
@@ -255,10 +244,6 @@ function fromCallback(invokeCallback) {
255
244
  * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
256
245
  */
257
246
  function fromObservable(observableCreator) {
258
- const nextEventType = '$$xstate.next';
259
- const errorEventType = '$$xstate.error';
260
- const completeEventType = '$$xstate.complete';
261
-
262
247
  // TODO: add event types
263
248
  const logic = {
264
249
  config: observableCreator,
@@ -272,7 +257,7 @@ function fromObservable(observableCreator) {
272
257
  return snapshot;
273
258
  }
274
259
  switch (event.type) {
275
- case nextEventType:
260
+ case XSTATE_OBSERVABLE_NEXT:
276
261
  {
277
262
  const newSnapshot = {
278
263
  ...snapshot,
@@ -280,7 +265,7 @@ function fromObservable(observableCreator) {
280
265
  };
281
266
  return newSnapshot;
282
267
  }
283
- case errorEventType:
268
+ case XSTATE_OBSERVABLE_ERROR:
284
269
  return {
285
270
  ...snapshot,
286
271
  status: 'error',
@@ -288,7 +273,7 @@ function fromObservable(observableCreator) {
288
273
  input: undefined,
289
274
  _subscription: undefined
290
275
  };
291
- case completeEventType:
276
+ case XSTATE_OBSERVABLE_COMPLETE:
292
277
  return {
293
278
  ...snapshot,
294
279
  status: 'done',
@@ -332,19 +317,19 @@ function fromObservable(observableCreator) {
332
317
  }).subscribe({
333
318
  next: value => {
334
319
  system._relay(self, self, {
335
- type: nextEventType,
320
+ type: XSTATE_OBSERVABLE_NEXT,
336
321
  data: value
337
322
  });
338
323
  },
339
324
  error: err => {
340
325
  system._relay(self, self, {
341
- type: errorEventType,
326
+ type: XSTATE_OBSERVABLE_ERROR,
342
327
  data: err
343
328
  });
344
329
  },
345
330
  complete: () => {
346
331
  system._relay(self, self, {
347
- type: completeEventType
332
+ type: XSTATE_OBSERVABLE_COMPLETE
348
333
  });
349
334
  }
350
335
  });
@@ -405,11 +390,7 @@ function fromObservable(observableCreator) {
405
390
  * canvasActor.start();
406
391
  * ```
407
392
  */
408
-
409
393
  function fromEventObservable(lazyObservable) {
410
- const errorEventType = '$$xstate.error';
411
- const completeEventType = '$$xstate.complete';
412
-
413
394
  // TODO: event types
414
395
  const logic = {
415
396
  config: lazyObservable,
@@ -418,7 +399,7 @@ function fromEventObservable(lazyObservable) {
418
399
  return state;
419
400
  }
420
401
  switch (event.type) {
421
- case errorEventType:
402
+ case XSTATE_OBSERVABLE_ERROR:
422
403
  return {
423
404
  ...state,
424
405
  status: 'error',
@@ -426,7 +407,7 @@ function fromEventObservable(lazyObservable) {
426
407
  input: undefined,
427
408
  _subscription: undefined
428
409
  };
429
- case completeEventType:
410
+ case XSTATE_OBSERVABLE_COMPLETE:
430
411
  return {
431
412
  ...state,
432
413
  status: 'done',
@@ -475,13 +456,13 @@ function fromEventObservable(lazyObservable) {
475
456
  },
476
457
  error: err => {
477
458
  system._relay(self, self, {
478
- type: errorEventType,
459
+ type: XSTATE_OBSERVABLE_ERROR,
479
460
  data: err
480
461
  });
481
462
  },
482
463
  complete: () => {
483
464
  system._relay(self, self, {
484
- type: completeEventType
465
+ type: XSTATE_OBSERVABLE_COMPLETE
485
466
  });
486
467
  }
487
468
  });
@@ -498,8 +479,8 @@ function fromEventObservable(lazyObservable) {
498
479
  return logic;
499
480
  }
500
481
 
501
- const resolveEventType = '$$xstate.resolve';
502
- const rejectEventType = '$$xstate.reject';
482
+ const XSTATE_PROMISE_RESOLVE = 'xstate.promise.resolve';
483
+ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
503
484
  /**
504
485
  * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
505
486
  *
@@ -555,7 +536,7 @@ promiseCreator) {
555
536
  return state;
556
537
  }
557
538
  switch (event.type) {
558
- case resolveEventType:
539
+ case XSTATE_PROMISE_RESOLVE:
559
540
  {
560
541
  const resolvedValue = event.data;
561
542
  return {
@@ -565,7 +546,7 @@ promiseCreator) {
565
546
  input: undefined
566
547
  };
567
548
  }
568
- case rejectEventType:
549
+ case XSTATE_PROMISE_REJECT:
569
550
  return {
570
551
  ...state,
571
552
  status: 'error',
@@ -601,7 +582,7 @@ promiseCreator) {
601
582
  return;
602
583
  }
603
584
  system._relay(self, self, {
604
- type: resolveEventType,
585
+ type: XSTATE_PROMISE_RESOLVE,
605
586
  data: response
606
587
  });
607
588
  }, errorData => {
@@ -609,7 +590,7 @@ promiseCreator) {
609
590
  return;
610
591
  }
611
592
  system._relay(self, self, {
612
- type: rejectEventType,
593
+ type: XSTATE_PROMISE_REJECT,
613
594
  data: errorData
614
595
  });
615
596
  });
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-91ec8b4f.esm.js';
1
+ import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-e4cc6d4f.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
@@ -83,6 +83,7 @@ function fromTransition(transition, initialContext) {
83
83
  };
84
84
  }
85
85
 
86
+ const instanceStates = /* #__PURE__ */new WeakMap();
86
87
  /**
87
88
  * An actor logic creator which returns callback logic as defined by a callback function.
88
89
  *
@@ -143,51 +144,46 @@ function fromTransition(transition, initialContext) {
143
144
  function fromCallback(invokeCallback) {
144
145
  const logic = {
145
146
  config: invokeCallback,
146
- start: (_state, {
147
- self,
148
- system
149
- }) => {
150
- system._relay(self, self, {
151
- type: 'xstate.create'
152
- });
153
- },
154
- transition: (state, event, {
155
- self,
156
- system
157
- }) => {
158
- if (event.type === 'xstate.create') {
159
- const sendBack = eventForParent => {
160
- if (state.status === 'stopped') {
147
+ start: (state, actorScope) => {
148
+ const {
149
+ self,
150
+ system
151
+ } = actorScope;
152
+ const callbackState = {
153
+ receivers: undefined,
154
+ dispose: undefined
155
+ };
156
+ instanceStates.set(self, callbackState);
157
+ callbackState.dispose = invokeCallback({
158
+ input: state.input,
159
+ system,
160
+ self,
161
+ sendBack: event => {
162
+ if (self.getSnapshot().status === 'stopped') {
161
163
  return;
162
164
  }
163
165
  if (self._parent) {
164
- system._relay(self, self._parent, eventForParent);
166
+ system._relay(self, self._parent, event);
165
167
  }
166
- };
167
- const receive = newListener => {
168
- state._receivers.add(newListener);
169
- };
170
- state._dispose = invokeCallback({
171
- input: state.input,
172
- system,
173
- self: self,
174
- sendBack,
175
- receive
176
- });
177
- return state;
178
- }
168
+ },
169
+ receive: listener => {
170
+ callbackState.receivers ??= new Set();
171
+ callbackState.receivers.add(listener);
172
+ }
173
+ });
174
+ },
175
+ transition: (state, event, actorScope) => {
176
+ const callbackState = instanceStates.get(actorScope.self);
179
177
  if (event.type === XSTATE_STOP) {
180
178
  state = {
181
179
  ...state,
182
180
  status: 'stopped',
183
181
  error: undefined
184
182
  };
185
- if (typeof state._dispose === 'function') {
186
- state._dispose();
187
- }
183
+ callbackState.dispose?.();
188
184
  return state;
189
185
  }
190
- state._receivers.forEach(receiver => receiver(event));
186
+ callbackState.receivers?.forEach(receiver => receiver(event));
191
187
  return state;
192
188
  },
193
189
  getInitialState: (_, input) => {
@@ -195,25 +191,18 @@ function fromCallback(invokeCallback) {
195
191
  status: 'active',
196
192
  output: undefined,
197
193
  error: undefined,
198
- input,
199
- _receivers: new Set(),
200
- _dispose: undefined
194
+ input
201
195
  };
202
196
  },
203
- getPersistedState: ({
204
- _dispose,
205
- _receivers,
206
- ...rest
207
- }) => rest,
208
- restoreState: state => ({
209
- _receivers: new Set(),
210
- _dispose: undefined,
211
- ...state
212
- })
197
+ getPersistedState: state => state,
198
+ restoreState: state => state
213
199
  };
214
200
  return logic;
215
201
  }
216
202
 
203
+ const XSTATE_OBSERVABLE_NEXT = 'xstate.observable.next';
204
+ const XSTATE_OBSERVABLE_ERROR = 'xstate.observable.error';
205
+ const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
217
206
  /**
218
207
  * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
219
208
  *
@@ -255,10 +244,6 @@ function fromCallback(invokeCallback) {
255
244
  * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
256
245
  */
257
246
  function fromObservable(observableCreator) {
258
- const nextEventType = '$$xstate.next';
259
- const errorEventType = '$$xstate.error';
260
- const completeEventType = '$$xstate.complete';
261
-
262
247
  // TODO: add event types
263
248
  const logic = {
264
249
  config: observableCreator,
@@ -272,7 +257,7 @@ function fromObservable(observableCreator) {
272
257
  return snapshot;
273
258
  }
274
259
  switch (event.type) {
275
- case nextEventType:
260
+ case XSTATE_OBSERVABLE_NEXT:
276
261
  {
277
262
  const newSnapshot = {
278
263
  ...snapshot,
@@ -280,7 +265,7 @@ function fromObservable(observableCreator) {
280
265
  };
281
266
  return newSnapshot;
282
267
  }
283
- case errorEventType:
268
+ case XSTATE_OBSERVABLE_ERROR:
284
269
  return {
285
270
  ...snapshot,
286
271
  status: 'error',
@@ -288,7 +273,7 @@ function fromObservable(observableCreator) {
288
273
  input: undefined,
289
274
  _subscription: undefined
290
275
  };
291
- case completeEventType:
276
+ case XSTATE_OBSERVABLE_COMPLETE:
292
277
  return {
293
278
  ...snapshot,
294
279
  status: 'done',
@@ -332,19 +317,19 @@ function fromObservable(observableCreator) {
332
317
  }).subscribe({
333
318
  next: value => {
334
319
  system._relay(self, self, {
335
- type: nextEventType,
320
+ type: XSTATE_OBSERVABLE_NEXT,
336
321
  data: value
337
322
  });
338
323
  },
339
324
  error: err => {
340
325
  system._relay(self, self, {
341
- type: errorEventType,
326
+ type: XSTATE_OBSERVABLE_ERROR,
342
327
  data: err
343
328
  });
344
329
  },
345
330
  complete: () => {
346
331
  system._relay(self, self, {
347
- type: completeEventType
332
+ type: XSTATE_OBSERVABLE_COMPLETE
348
333
  });
349
334
  }
350
335
  });
@@ -405,11 +390,7 @@ function fromObservable(observableCreator) {
405
390
  * canvasActor.start();
406
391
  * ```
407
392
  */
408
-
409
393
  function fromEventObservable(lazyObservable) {
410
- const errorEventType = '$$xstate.error';
411
- const completeEventType = '$$xstate.complete';
412
-
413
394
  // TODO: event types
414
395
  const logic = {
415
396
  config: lazyObservable,
@@ -418,7 +399,7 @@ function fromEventObservable(lazyObservable) {
418
399
  return state;
419
400
  }
420
401
  switch (event.type) {
421
- case errorEventType:
402
+ case XSTATE_OBSERVABLE_ERROR:
422
403
  return {
423
404
  ...state,
424
405
  status: 'error',
@@ -426,7 +407,7 @@ function fromEventObservable(lazyObservable) {
426
407
  input: undefined,
427
408
  _subscription: undefined
428
409
  };
429
- case completeEventType:
410
+ case XSTATE_OBSERVABLE_COMPLETE:
430
411
  return {
431
412
  ...state,
432
413
  status: 'done',
@@ -475,13 +456,13 @@ function fromEventObservable(lazyObservable) {
475
456
  },
476
457
  error: err => {
477
458
  system._relay(self, self, {
478
- type: errorEventType,
459
+ type: XSTATE_OBSERVABLE_ERROR,
479
460
  data: err
480
461
  });
481
462
  },
482
463
  complete: () => {
483
464
  system._relay(self, self, {
484
- type: completeEventType
465
+ type: XSTATE_OBSERVABLE_COMPLETE
485
466
  });
486
467
  }
487
468
  });
@@ -498,8 +479,8 @@ function fromEventObservable(lazyObservable) {
498
479
  return logic;
499
480
  }
500
481
 
501
- const resolveEventType = '$$xstate.resolve';
502
- const rejectEventType = '$$xstate.reject';
482
+ const XSTATE_PROMISE_RESOLVE = 'xstate.promise.resolve';
483
+ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
503
484
  /**
504
485
  * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
505
486
  *
@@ -555,7 +536,7 @@ promiseCreator) {
555
536
  return state;
556
537
  }
557
538
  switch (event.type) {
558
- case resolveEventType:
539
+ case XSTATE_PROMISE_RESOLVE:
559
540
  {
560
541
  const resolvedValue = event.data;
561
542
  return {
@@ -565,7 +546,7 @@ promiseCreator) {
565
546
  input: undefined
566
547
  };
567
548
  }
568
- case rejectEventType:
549
+ case XSTATE_PROMISE_REJECT:
569
550
  return {
570
551
  ...state,
571
552
  status: 'error',
@@ -601,7 +582,7 @@ promiseCreator) {
601
582
  return;
602
583
  }
603
584
  system._relay(self, self, {
604
- type: resolveEventType,
585
+ type: XSTATE_PROMISE_RESOLVE,
605
586
  data: response
606
587
  });
607
588
  }, errorData => {
@@ -609,7 +590,7 @@ promiseCreator) {
609
590
  return;
610
591
  }
611
592
  system._relay(self, self, {
612
- type: rejectEventType,
593
+ type: XSTATE_PROMISE_REJECT,
613
594
  data: errorData
614
595
  });
615
596
  });
@@ -1,2 +1,2 @@
1
- !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,(function(t){"use strict";class s{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const s={value:t,next:null};if(this._current)return this._last.next=s,void(this._last=s);this._current=s,this._last=s,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const e="xstate.stop";function i(t,s){return{type:`xstate.error.actor.${t}`,data:s}}function r(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const o=t=>{const s=r();s&&s.register(t)};function n(t){setTimeout((()=>{throw t}))}const a="function"==typeof Symbol&&Symbol.observable||"@@observable";let c=0;function h(t,s,e){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:s)?.bind(r),complete:(i?t.complete:e)?.bind(r)}}let u=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const d={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class p{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new s(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this._processingStatus=u.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const i={...d,...e},{clock:r,logger:o,parent:n,syncSnapshot:a,id:p,systemId:_,inspect:l}=i;this.system=n?.system??function(t){const s=new Map,e=new Map,i=new WeakMap,r=new Set,o={_bookId:()=>"x:"+c++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const r=i.get(t);void 0!==r&&(e.delete(r),i.delete(t))},get:t=>e.get(t),_set:(t,s)=>{const r=e.get(t);if(r&&r!==s)throw new Error(`Actor with system ID '${t}' already exists.`);e.set(t,s),i.set(s,t)},inspect:t=>{r.add(t)},_sendInspectionEvent:s=>{const e={...s,rootId:t.sessionId};r.forEach((t=>t.next?.(e)))},_relay:(t,s,e)=>{o._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:s,event:e}),s._send(e)}};return o}(this),l&&!n&&this.system.inspect(h(l)),this.sessionId=this.system._bookId(),this.id=p??this.sessionId,this.logger=o,this.clock=r,this._parent=n,this._syncSnapshot=a,this.options=i,this.src=i.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),_&&(this._systemId=_,this.system._set(_,this)),this._initState(e?.state),_&&"active"!==this._state.status&&this.system._unregister(this)}_initState(t){this._state=t?this.logic.restoreState?this.logic.restoreState(t,this._actorScope):t:this.logic.getInitialState(this._actorScope,this.options?.input)}update(t,s){let e;for(this._state=t;e=this._deferred.shift();)e();for(const s of this.observers)try{s.next?.(t)}catch(t){n(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(r=this.id,o=this._state.output,{type:`xstate.done.actor.${r}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._stopProcedure(),this._error(this._state.error),this._parent&&this.system._relay(this,this._parent,i(this.id,this._state.error))}var r,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:s,snapshot:t})}subscribe(t,s,e){const i=h(t,s,e);if(this._processingStatus!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){n(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this._processingStatus===u.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this._parent.send({type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this._processingStatus=u.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._state.status){case"done":this.update(this._state,t);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorScope)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(i(this.id,t)),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let s,r;try{s=this.logic.transition(this._state,t,this._actorScope)}catch(t){r={err:t}}if(r){const{err:t}=r;return this._stopProcedure(),this._error(t),void this._parent?.send(i(this.id,t))}this.update(s,t),t.type===e&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===u.Stopped?this:(this.mailbox.clear(),this._processingStatus===u.NotStarted?(this._processingStatus=u.Stopped,this):(this.mailbox.enqueue({type:e}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){n(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||n(t));let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){n(t)}}this.observers.clear(),s&&n(t)}_stopProcedure(){if(this._processingStatus!==u.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new s(this._process.bind(this)),this._processingStatus=u.Stopped,this.system._unregister(this),this}_send(t){this._processingStatus!==u.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}delaySend(t){const{event:s,id:e,delay:i}=t,r=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,s)}),i);e&&(this.delayedEventsMap[e]=r)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:o)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedState(t){return this.logic.getPersistedState(this._state,t)}[a](){return this}getSnapshot(){return this._state}}function _(t,s){return{config:t,transition:(s,e,i)=>({...s,context:t(s.context,e,i)}),getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof s?s({input:e}):s}),getPersistedState:t=>t,restoreState:t=>t}}const l="$$xstate.resolve",v="$$xstate.reject";const f=_((t=>{}),void 0);t.createEmptyActor=function(){return new p(f,t);var t},t.fromCallback=function(t){return{config:t,start:(t,{self:s,system:e})=>{e._relay(s,s,{type:"xstate.create"})},transition:(s,i,{self:r,system:o})=>{if("xstate.create"===i.type){const e=t=>{"stopped"!==s.status&&r._parent&&o._relay(r,r._parent,t)},i=t=>{s._receivers.add(t)};return s._dispose=t({input:s.input,system:o,self:r,sendBack:e,receive:i}),s}return i.type===e?("function"==typeof(s={...s,status:"stopped",error:void 0})._dispose&&s._dispose(),s):(s._receivers.forEach((t=>t(i))),s)},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,input:s,_receivers:new Set,_dispose:void 0}),getPersistedState:({_dispose:t,_receivers:s,...e})=>e,restoreState:t=>({_receivers:new Set,_dispose:void 0,...t})}},t.fromEventObservable=function(t){const s="$$xstate.error",i="$$xstate.complete";return{config:t,transition:(t,r)=>{if("active"!==t.status)return t;switch(r.type){case s:return{...t,status:"error",error:r.data,input:void 0,_subscription:void 0};case i:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(e,{self:r,system:o})=>{"done"!==e.status&&(e._subscription=t({input:e.input,system:o,self:r}).subscribe({next:t=>{r._parent&&o._relay(r,r._parent,t)},error:t=>{o._relay(r,r,{type:s,data:t})},complete:()=>{o._relay(r,r,{type:i})}}))},getPersistedState:({_subscription:t,...s})=>s,restoreState:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){const s="$$xstate.next",i="$$xstate.error",r="$$xstate.complete";return{config:t,transition:(t,o,{self:n,id:a,defer:c,system:h})=>{if("active"!==t.status)return t;switch(o.type){case s:return{...t,context:o.data};case i:return{...t,status:"error",error:o.data,input:void 0,_subscription:void 0};case r:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(e,{self:o,system:n})=>{"done"!==e.status&&(e._subscription=t({input:e.input,system:n,self:o}).subscribe({next:t=>{n._relay(o,o,{type:s,data:t})},error:t=>{n._relay(o,o,{type:i,data:t})},complete:()=>{n._relay(o,o,{type:r})}}))},getPersistedState:({_subscription:t,...s})=>s,restoreState:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case l:{const e=s.data;return{...t,status:"done",output:e,input:void 0}}case v:return{...t,status:"error",error:s.data,input:void 0};case e:return{...t,status:"stopped",input:void 0};default:return t}},start:(s,{self:e,system:i})=>{if("active"!==s.status)return;Promise.resolve(t({input:s.input,system:i,self:e})).then((t=>{"active"===e.getSnapshot().status&&i._relay(e,e,{type:l,data:t})}),(t=>{"active"===e.getSnapshot().status&&i._relay(e,e,{type:v,data:t})}))},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=_,Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,(function(t){"use strict";class s{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const s={value:t,next:null};if(this._current)return this._last.next=s,void(this._last=s);this._current=s,this._last=s,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const e="xstate.stop";function i(t,s){return{type:`xstate.error.actor.${t}`,data:s}}function r(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const o=t=>{const s=r();s&&s.register(t)};function n(t){setTimeout((()=>{throw t}))}const a="function"==typeof Symbol&&Symbol.observable||"@@observable";let c=0;function h(t,s,e){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:s)?.bind(r),complete:(i?t.complete:e)?.bind(r)}}let u=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const d={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class p{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new s(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this._processingStatus=u.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const i={...d,...e},{clock:r,logger:o,parent:n,syncSnapshot:a,id:p,systemId:l,inspect:_}=i;this.system=n?.system??function(t){const s=new Map,e=new Map,i=new WeakMap,r=new Set,o={_bookId:()=>"x:"+c++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const r=i.get(t);void 0!==r&&(e.delete(r),i.delete(t))},get:t=>e.get(t),_set:(t,s)=>{const r=e.get(t);if(r&&r!==s)throw new Error(`Actor with system ID '${t}' already exists.`);e.set(t,s),i.set(s,t)},inspect:t=>{r.add(t)},_sendInspectionEvent:s=>{const e={...s,rootId:t.sessionId};r.forEach((t=>t.next?.(e)))},_relay:(t,s,e)=>{o._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:s,event:e}),s._send(e)}};return o}(this),_&&!n&&this.system.inspect(h(_)),this.sessionId=this.system._bookId(),this.id=p??this.sessionId,this.logger=o,this.clock=r,this._parent=n,this._syncSnapshot=a,this.options=i,this.src=i.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),l&&(this._systemId=l,this.system._set(l,this)),this._initState(e?.state),l&&"active"!==this._state.status&&this.system._unregister(this)}_initState(t){this._state=t?this.logic.restoreState?this.logic.restoreState(t,this._actorScope):t:this.logic.getInitialState(this._actorScope,this.options?.input)}update(t,s){let e;for(this._state=t;e=this._deferred.shift();)e();for(const s of this.observers)try{s.next?.(t)}catch(t){n(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(r=this.id,o=this._state.output,{type:`xstate.done.actor.${r}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._stopProcedure(),this._error(this._state.error),this._parent&&this.system._relay(this,this._parent,i(this.id,this._state.error))}var r,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:s,snapshot:t})}subscribe(t,s,e){const i=h(t,s,e);if(this._processingStatus!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){n(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this._processingStatus===u.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this._parent.send({type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this._processingStatus=u.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._state.status){case"done":this.update(this._state,t);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorScope)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(i(this.id,t)),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let s,r;try{s=this.logic.transition(this._state,t,this._actorScope)}catch(t){r={err:t}}if(r){const{err:t}=r;return this._stopProcedure(),this._error(t),void this._parent?.send(i(this.id,t))}this.update(s,t),t.type===e&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===u.Stopped?this:(this.mailbox.clear(),this._processingStatus===u.NotStarted?(this._processingStatus=u.Stopped,this):(this.mailbox.enqueue({type:e}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){n(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||n(t));let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){n(t)}}this.observers.clear(),s&&n(t)}_stopProcedure(){if(this._processingStatus!==u.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new s(this._process.bind(this)),this._processingStatus=u.Stopped,this.system._unregister(this),this}_send(t){this._processingStatus!==u.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}delaySend(t){const{event:s,id:e,delay:i}=t,r=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,s)}),i);e&&(this.delayedEventsMap[e]=r)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:o)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedState(t){return this.logic.getPersistedState(this._state,t)}[a](){return this}getSnapshot(){return this._state}}function l(t,s){return{config:t,transition:(s,e,i)=>({...s,context:t(s.context,e,i)}),getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof s?s({input:e}):s}),getPersistedState:t=>t,restoreState:t=>t}}const _=new WeakMap;const v="xstate.observable.next",f="xstate.observable.error",y="xstate.observable.complete";const b="xstate.promise.resolve",g="xstate.promise.reject";const m=l((t=>{}),void 0);t.createEmptyActor=function(){return new p(m,t);var t},t.fromCallback=function(t){return{config:t,start:(s,e)=>{const{self:i,system:r}=e,o={receivers:void 0,dispose:void 0};_.set(i,o),o.dispose=t({input:s.input,system:r,self:i,sendBack:t=>{"stopped"!==i.getSnapshot().status&&i._parent&&r._relay(i,i._parent,t)},receive:t=>{o.receivers??=new Set,o.receivers.add(t)}})},transition:(t,s,i)=>{const r=_.get(i.self);return s.type===e?(t={...t,status:"stopped",error:void 0},r.dispose?.(),t):(r.receivers?.forEach((t=>t(s))),t)},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedState:t=>t,restoreState:t=>t}},t.fromEventObservable=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case f:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case y:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e}).subscribe({next:t=>{e._parent&&i._relay(e,e._parent,t)},error:t=>{i._relay(e,e,{type:f,data:t})},complete:()=>{i._relay(e,e,{type:y})}}))},getPersistedState:({_subscription:t,...s})=>s,restoreState:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){return{config:t,transition:(t,s,{self:i,id:r,defer:o,system:n})=>{if("active"!==t.status)return t;switch(s.type){case v:return{...t,context:s.data};case f:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case y:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e}).subscribe({next:t=>{i._relay(e,e,{type:v,data:t})},error:t=>{i._relay(e,e,{type:f,data:t})},complete:()=>{i._relay(e,e,{type:y})}}))},getPersistedState:({_subscription:t,...s})=>s,restoreState:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case b:{const e=s.data;return{...t,status:"done",output:e,input:void 0}}case g:return{...t,status:"error",error:s.data,input:void 0};case e:return{...t,status:"stopped",input:void 0};default:return t}},start:(s,{self:e,system:i})=>{if("active"!==s.status)return;Promise.resolve(t({input:s.input,system:i,self:e})).then((t=>{"active"===e.getSnapshot().status&&i._relay(e,e,{type:b,data:t})}),(t=>{"active"===e.getSnapshot().status&&i._relay(e,e,{type:g,data:t})}))},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=l,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actors.umd.min.js.map