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
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_INIT, j as XSTATE_STOP, d as createActor } from '../../dist/interpreter-498891b2.esm.js';
1
+ import { X as XSTATE_INIT, j as XSTATE_STOP, d as createActor } from '../../dist/interpreter-b8f53c4b.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
@@ -7,21 +7,28 @@ import '../../dev/dist/xstate-dev.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,31 +69,34 @@ 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
89
- })
96
+ _dispose,
97
+ _receivers,
98
+ ...rest
99
+ }) => rest
90
100
  };
91
101
  }
92
102
 
@@ -96,62 +106,66 @@ function fromObservable(observableCreator) {
96
106
  const completeEventType = '$$xstate.complete';
97
107
  return {
98
108
  config: observableCreator,
99
- transition: (state, event, {
109
+ transition: (snapshot, event, {
100
110
  self,
101
111
  id,
102
112
  defer
103
113
  }) => {
104
- if (state.status !== 'active') {
105
- return state;
114
+ if (snapshot.status !== 'active') {
115
+ return snapshot;
106
116
  }
107
117
  switch (event.type) {
108
118
  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
119
+ {
120
+ const newSnapshot = {
121
+ ...snapshot,
122
+ context: event.data
123
+ };
124
+ // match the exact timing of events sent by machines
125
+ // send actions are not executed immediately
126
+ defer(() => {
127
+ self._parent?.send({
128
+ type: `xstate.snapshot.${id}`,
129
+ data: newSnapshot
130
+ });
115
131
  });
116
- });
117
- return {
118
- ...state,
119
- data: event.data
120
- };
132
+ return newSnapshot;
133
+ }
121
134
  case errorEventType:
122
135
  return {
123
- ...state,
136
+ ...snapshot,
124
137
  status: 'error',
138
+ error: event.data,
125
139
  input: undefined,
126
- data: event.data,
127
- // TODO: if we keep this as `data` we should reflect this in the type
128
- subscription: undefined
140
+ _subscription: undefined
129
141
  };
130
142
  case completeEventType:
131
143
  return {
132
- ...state,
144
+ ...snapshot,
133
145
  status: 'done',
134
146
  input: undefined,
135
- subscription: undefined
147
+ _subscription: undefined
136
148
  };
137
149
  case XSTATE_STOP:
138
- state.subscription.unsubscribe();
150
+ snapshot._subscription.unsubscribe();
139
151
  return {
140
- ...state,
141
- status: 'canceled',
152
+ ...snapshot,
153
+ status: 'stopped',
142
154
  input: undefined,
143
- subscription: undefined
155
+ _subscription: undefined
144
156
  };
145
157
  default:
146
- return state;
158
+ return snapshot;
147
159
  }
148
160
  },
149
161
  getInitialState: (_, input) => {
150
162
  return {
151
- subscription: undefined,
152
163
  status: 'active',
153
- data: undefined,
154
- input
164
+ output: undefined,
165
+ error: undefined,
166
+ context: undefined,
167
+ input,
168
+ _subscription: undefined
155
169
  };
156
170
  },
157
171
  start: (state, {
@@ -162,7 +176,7 @@ function fromObservable(observableCreator) {
162
176
  // Do not restart a completed observable
163
177
  return;
164
178
  }
165
- state.subscription = observableCreator({
179
+ state._subscription = observableCreator({
166
180
  input: state.input,
167
181
  system,
168
182
  self
@@ -186,20 +200,13 @@ function fromObservable(observableCreator) {
186
200
  }
187
201
  });
188
202
  },
189
- getSnapshot: state => state.data,
190
203
  getPersistedState: ({
191
- status,
192
- data,
193
- input
194
- }) => ({
195
- status,
196
- data,
197
- input
198
- }),
199
- getStatus: state => state,
204
+ _subscription,
205
+ ...state
206
+ }) => state,
200
207
  restoreState: state => ({
201
208
  ...state,
202
- subscription: undefined
209
+ _subscription: undefined
203
210
  })
204
211
  };
205
212
  }
@@ -229,25 +236,24 @@ function fromEventObservable(lazyObservable) {
229
236
  return {
230
237
  ...state,
231
238
  status: 'error',
239
+ error: event.data,
232
240
  input: undefined,
233
- data: event.data,
234
- // TODO: if we keep this as `data` we should reflect this in the type
235
- subscription: undefined
241
+ _subscription: undefined
236
242
  };
237
243
  case completeEventType:
238
244
  return {
239
245
  ...state,
240
246
  status: 'done',
241
247
  input: undefined,
242
- subscription: undefined
248
+ _subscription: undefined
243
249
  };
244
250
  case XSTATE_STOP:
245
- state.subscription.unsubscribe();
251
+ state._subscription.unsubscribe();
246
252
  return {
247
253
  ...state,
248
- status: 'canceled',
254
+ status: 'stopped',
249
255
  input: undefined,
250
- subscription: undefined
256
+ _subscription: undefined
251
257
  };
252
258
  default:
253
259
  return state;
@@ -255,10 +261,12 @@ function fromEventObservable(lazyObservable) {
255
261
  },
256
262
  getInitialState: (_, input) => {
257
263
  return {
258
- subscription: undefined,
259
264
  status: 'active',
260
- data: undefined,
261
- input
265
+ output: undefined,
266
+ error: undefined,
267
+ context: undefined,
268
+ input,
269
+ _subscription: undefined
262
270
  };
263
271
  },
264
272
  start: (state, {
@@ -269,7 +277,7 @@ function fromEventObservable(lazyObservable) {
269
277
  // Do not restart a completed observable
270
278
  return;
271
279
  }
272
- state.subscription = lazyObservable({
280
+ state._subscription = lazyObservable({
273
281
  input: state.input,
274
282
  system,
275
283
  self
@@ -290,20 +298,13 @@ function fromEventObservable(lazyObservable) {
290
298
  }
291
299
  });
292
300
  },
293
- getSnapshot: _ => undefined,
294
301
  getPersistedState: ({
295
- status,
296
- data,
297
- input
298
- }) => ({
299
- status,
300
- data,
301
- input
302
- }),
303
- getStatus: state => state,
302
+ _subscription,
303
+ ...state
304
+ }) => state,
304
305
  restoreState: state => ({
305
306
  ...state,
306
- subscription: undefined
307
+ _subscription: undefined
307
308
  })
308
309
  };
309
310
  }
@@ -322,24 +323,26 @@ promiseCreator) {
322
323
  }
323
324
  switch (event.type) {
324
325
  case resolveEventType:
325
- return {
326
- ...state,
327
- status: 'done',
328
- data: event.data,
329
- input: undefined
330
- };
326
+ {
327
+ const resolvedValue = event.data;
328
+ return {
329
+ ...state,
330
+ status: 'done',
331
+ output: resolvedValue,
332
+ input: undefined
333
+ };
334
+ }
331
335
  case rejectEventType:
332
336
  return {
333
337
  ...state,
334
338
  status: 'error',
335
- data: event.data,
336
- // TODO: if we keep this as `data` we should reflect this in the type
339
+ error: event.data,
337
340
  input: undefined
338
341
  };
339
342
  case XSTATE_STOP:
340
343
  return {
341
344
  ...state,
342
- status: 'canceled',
345
+ status: 'stopped',
343
346
  input: undefined
344
347
  };
345
348
  default:
@@ -361,8 +364,7 @@ promiseCreator) {
361
364
  self
362
365
  }));
363
366
  resolvedPromise.then(response => {
364
- // TODO: remove this condition once dead letter queue lands
365
- if (self._state.status !== 'active') {
367
+ if (self.getSnapshot().status !== 'active') {
366
368
  return;
367
369
  }
368
370
  self.send({
@@ -370,8 +372,7 @@ promiseCreator) {
370
372
  data: response
371
373
  });
372
374
  }, errorData => {
373
- // TODO: remove this condition once dead letter queue lands
374
- if (self._state.status !== 'active') {
375
+ if (self.getSnapshot().status !== 'active') {
375
376
  return;
376
377
  }
377
378
  self.send({
@@ -383,12 +384,11 @@ promiseCreator) {
383
384
  getInitialState: (_, input) => {
384
385
  return {
385
386
  status: 'active',
386
- data: undefined,
387
+ output: undefined,
388
+ error: undefined,
387
389
  input
388
390
  };
389
391
  },
390
- getSnapshot: state => state.data,
391
- getStatus: state => state,
392
392
  getPersistedState: state => state,
393
393
  restoreState: state => state
394
394
  };
@@ -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)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}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),t===this._current&&(this._current=this._current.next)}this._last=null}}const e="xstate.init",i="xstate.stop";function r(t,s){return{type:`xstate.error.actor.${t}`,data:s}}function o(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const n=t=>{const s=o();s&&s.register(t)};function a(t){setTimeout((()=>{throw t}))}const c="function"==typeof Symbol&&Symbol.observable||"@@observable";let d=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const h={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class u{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.status=d.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=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={...h,...e},{clock:r,logger:o,parent:n,id:a,systemId:c}=i;this.system=n?.system??function(){let t=0;const s=new Map,e=new Map,i=new WeakMap;return{_bookId:()=>"x:"+t++,_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)}}}(),c&&(this._systemId=c,this.system._set(c,this)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=o,this.clock=r,this._parent=n,this.options=i,this.src=i.src,this.ref=this,this._actorContext={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._initState()}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const s=this.getSnapshot();let e;for(;e=this._deferred.shift();)e();for(const t of this.observers)try{t.next?.(s)}catch(t){a(t)}const i=this.logic.getStatus?.(t);switch(i?.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(o=this.id,n=i.data,{type:`xstate.done.actor.${o}`,output:n}),this._parent?.send(this._doneEvent);break;case"error":this._stopProcedure(),this._error(i.data),this._parent?.send(r(this.id,i.data))}var o,n}subscribe(t,s,e){const i=function(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)}}(t,s,e);if(this.status!==d.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this.status===d.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=d.Running;const t=this.logic.getStatus?.(this._state);switch(t?.status){case"done":this.update(this._state);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorContext)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(r(this.id,t)),this}return this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let s,e;try{s=this.logic.transition(this._state,t,this._actorContext)}catch(t){e={err:t}}if(e){const{err:t}=e;return this._stopProcedure(),this._error(t),void this._parent?.send(r(this.id,t))}this.update(s),t.type===i&&(this._stopProcedure(),this._complete())}_stop(){return this.status===d.Stopped?this:(this.mailbox.clear(),this.status===d.NotStarted?(this.status=d.Stopped,this):(this.mailbox.enqueue({type:i}),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){a(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||a(t));let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){a(t)}}this.observers.clear(),s&&a(t)}_stopProcedure(){if(this.status!==d.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.status=d.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);this.status!==d.Stopped&&this.mailbox.enqueue(t)}delaySend({event:t,id:s,delay:e,to:i}){const r=this.clock.setTimeout((()=>{i?i.send(t):this.send(t)}),e);s&&(this.delayedEventsMap[s]=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:n)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[c](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function p(t,s){return{config:t,transition:(s,e,i)=>t(s,e,i),getInitialState:(t,e)=>"function"==typeof s?s({input:e}):s,getSnapshot:t=>t,getPersistedState:t=>t,restoreState:t=>t}}const l="$$xstate.resolve",f="$$xstate.reject";const _=p((t=>{}),void 0);t.createEmptyActor=function(){return new u(_,t);var t},t.fromCallback=function(t){return{config:t,start:(t,{self:s})=>{s.send({type:e})},transition:(s,r,{self:o,id:n,system:a})=>{if(r.type===e){const e=t=>{s.canceled||o._parent?.send(t)},i=t=>{s.receivers.add(t)};return s.dispose=t({input:s.input,system:a,self:o,sendBack:e,receive:i}),s}return r.type===i?(s.canceled=!0,"function"==typeof s.dispose&&s.dispose(),s):(s.receivers.forEach((t=>t(r))),s)},getInitialState:(t,s)=>({canceled:!1,receivers:new Set,dispose:void 0,input:s}),getSnapshot:()=>{},getPersistedState:({input:t,canceled:s})=>({input:t,canceled:s})}},t.fromEventObservable=function(t){const s="$$xstate.error",e="$$xstate.complete";return{config:t,transition:(t,r)=>{if("active"!==t.status)return t;switch(r.type){case s:return{...t,status:"error",input:void 0,data:r.data,subscription:void 0};case e:return{...t,status:"done",input:void 0,subscription:void 0};case i:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,s)=>({subscription:void 0,status:"active",data:void 0,input:s}),start:(i,{self:r,system:o})=>{"done"!==i.status&&(i.subscription=t({input:i.input,system:o,self:r}).subscribe({next:t=>{r._parent?.send(t)},error:t=>{r.send({type:s,data:t})},complete:()=>{r.send({type:e})}}))},getSnapshot:t=>{},getPersistedState:({status:t,data:s,input:e})=>({status:t,data:s,input:e}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromObservable=function(t){const s="$$xstate.next",e="$$xstate.error",r="$$xstate.complete";return{config:t,transition:(t,o,{self:n,id:a,defer:c})=>{if("active"!==t.status)return t;switch(o.type){case s:return c((()=>{n._parent?.send({type:`xstate.snapshot.${a}`,data:o.data})})),{...t,data:o.data};case e:return{...t,status:"error",input:void 0,data:o.data,subscription:void 0};case r:return{...t,status:"done",input:void 0,subscription:void 0};case i:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,s)=>({subscription:void 0,status:"active",data:void 0,input:s}),start:(i,{self:o,system:n})=>{"done"!==i.status&&(i.subscription=t({input:i.input,system:n,self:o}).subscribe({next:t=>{o.send({type:s,data:t})},error:t=>{o.send({type:e,data:t})},complete:()=>{o.send({type:r})}}))},getSnapshot:t=>t.data,getPersistedState:({status:t,data:s,input:e})=>({status:t,data:s,input:e}),getStatus:t=>t,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:return{...t,status:"done",data:s.data,input:void 0};case f:return{...t,status:"error",data:s.data,input:void 0};case i:return{...t,status:"canceled",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._state.status&&e.send({type:l,data:t})}),(t=>{"active"===e._state.status&&e.send({type:f,data:t})}))},getInitialState:(t,s)=>({status:"active",data:void 0,input:s}),getSnapshot:t=>t.data,getStatus:t=>t,getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=p,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)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}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),t===this._current&&(this._current=this._current.next)}this._last=null}}const e="xstate.init",i="xstate.stop";function r(t,s){return{type:`xstate.error.actor.${t}`,data:s}}function o(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const n=t=>{const s=o();s&&s.register(t)};function a(t){setTimeout((()=>{throw t}))}const c="function"==typeof Symbol&&Symbol.observable||"@@observable";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 h{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.status=u.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=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,id:a,systemId:c}=i;this.system=n?.system??function(){let t=0;const s=new Map,e=new Map,i=new WeakMap;return{_bookId:()=>"x:"+t++,_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)}}}(),c&&(this._systemId=c,this.system._set(c,this)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=o,this.clock=r,this._parent=n,this.options=i,this.src=i.src,this.ref=this,this._actorContext={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._initState()}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){let s;for(this._state=t;s=this._deferred.shift();)s();for(const s of this.observers)try{s.next?.(t)}catch(t){a(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(e=this.id,i=this._state.output,{type:`xstate.done.actor.${e}`,output:i}),this._parent?.send(this._doneEvent);break;case"error":this._stopProcedure(),this._error(this._state.error),this._parent?.send(r(this.id,this._state.error))}var e,i}subscribe(t,s,e){const i=function(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)}}(t,s,e);if(this.status!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this.status===u.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=u.Running;switch(this._state.status){case"done":this.update(this._state);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorContext)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(r(this.id,t)),this}return this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let s,e;try{s=this.logic.transition(this._state,t,this._actorContext)}catch(t){e={err:t}}if(e){const{err:t}=e;return this._stopProcedure(),this._error(t),void this._parent?.send(r(this.id,t))}this.update(s),t.type===i&&(this._stopProcedure(),this._complete())}_stop(){return this.status===u.Stopped?this:(this.mailbox.clear(),this.status===u.NotStarted?(this.status=u.Stopped,this):(this.mailbox.enqueue({type:i}),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){a(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||a(t));let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){a(t)}}this.observers.clear(),s&&a(t)}_stopProcedure(){if(this.status!==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.status=u.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);this.status!==u.Stopped&&this.mailbox.enqueue(t)}delaySend({event:t,id:s,delay:e,to:i}){const r=this.clock.setTimeout((()=>{i?i.send(t):this.send(t)}),e);s&&(this.delayedEventsMap[s]=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:n)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[c](){return this}getSnapshot(){return this._state}}function p(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",_="$$xstate.reject";const v=p((t=>{}),void 0);t.createEmptyActor=function(){return new h(v,t);var t},t.fromCallback=function(t){return{config:t,start:(t,{self:s})=>{s.send({type:e})},transition:(s,r,{self:o,id:n,system:a})=>{if(r.type===e){const e=t=>{"stopped"!==s.status&&o._parent?.send(t)},i=t=>{s._receivers.add(t)};return s._dispose=t({input:s.input,system:a,self:o,sendBack:e,receive:i}),s}return r.type===i?("function"==typeof(s={...s,status:"stopped",error:void 0})._dispose&&s._dispose(),s):(s._receivers.forEach((t=>t(r))),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}},t.fromEventObservable=function(t){const s="$$xstate.error",e="$$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 e:return{...t,status:"done",input:void 0,_subscription:void 0};case i: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:(i,{self:r,system:o})=>{"done"!==i.status&&(i._subscription=t({input:i.input,system:o,self:r}).subscribe({next:t=>{r._parent?.send(t)},error:t=>{r.send({type:s,data:t})},complete:()=>{r.send({type:e})}}))},getPersistedState:({_subscription:t,...s})=>s,restoreState:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){const s="$$xstate.next",e="$$xstate.error",r="$$xstate.complete";return{config:t,transition:(t,o,{self:n,id:a,defer:c})=>{if("active"!==t.status)return t;switch(o.type){case s:{const s={...t,context:o.data};return c((()=>{n._parent?.send({type:`xstate.snapshot.${a}`,data:s})})),s}case e: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 i: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:(i,{self:o,system:n})=>{"done"!==i.status&&(i._subscription=t({input:i.input,system:n,self:o}).subscribe({next:t=>{o.send({type:s,data:t})},error:t=>{o.send({type:e,data:t})},complete:()=>{o.send({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 _:return{...t,status:"error",error:s.data,input:void 0};case i: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&&e.send({type:l,data:t})}),(t=>{"active"===e.getSnapshot().status&&e.send({type:_,data:t})}))},getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=p,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actors.umd.min.js.map