xstate 4.13.0 → 4.15.1

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.
package/es/interpreter.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { __values, __assign, __spread } from './_virtual/_tslib.js';
2
2
  import { IS_PRODUCTION } from './environment.js';
3
- import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isPromiseLike, isObservable, isMachine, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, uniqueId } from './utils.js';
3
+ import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isPromiseLike, isObservable, isMachine, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, uniqueId, toObserver } from './utils.js';
4
4
  import { ActionTypes, SpecialTargets } from './types.js';
5
5
  import { isInFinalState } from './stateUtils.js';
6
6
  import { errorPlatform, log, stop, start, cancel, send, update, error as error$1 } from './actionTypes.js';
7
7
  import { doneInvoke, initEvent, getActionFunction, error } from './actions.js';
8
8
  import { isState, State, bindActionToState } from './State.js';
9
9
  import { provide, consume } from './serviceScope.js';
10
- import { isActor, createDeferredActor } from './Actor.js';
10
+ import { isSpawnedActor, createDeferredActor } from './Actor.js';
11
11
  import { Scheduler } from './scheduler.js';
12
12
  import { registry } from './registry.js';
13
13
  import { registerService } from './devTools.js';
@@ -55,7 +55,7 @@ function () {
55
55
  */
56
56
 
57
57
  this.initialized = false;
58
- this._status = InterpreterStatus.NotStarted;
58
+ this.status = InterpreterStatus.NotStarted;
59
59
  this.children = new Map();
60
60
  this.forwardTo = new Set();
61
61
  /**
@@ -82,7 +82,7 @@ function () {
82
82
 
83
83
  var _event = toSCXMLEvent(toEventObject(event, payload));
84
84
 
85
- if (_this._status === InterpreterStatus.Stopped) {
85
+ if (_this.status === InterpreterStatus.Stopped) {
86
86
  // do nothing
87
87
  if (!IS_PRODUCTION) {
88
88
  warn(false, "Event \"" + _event.name + "\" was sent to stopped service \"" + _this.machine.id + "\". This service has already reached its final state, and will not transition.\nEvent: " + JSON.stringify(_event.data));
@@ -91,12 +91,7 @@ function () {
91
91
  return _this.state;
92
92
  }
93
93
 
94
- if (_this._status === InterpreterStatus.NotStarted && _this.options.deferEvents) {
95
- // tslint:disable-next-line:no-console
96
- if (!IS_PRODUCTION) {
97
- warn(false, "Event \"" + _event.name + "\" was sent to uninitialized service \"" + _this.machine.id + "\" and is deferred. Make sure .start() is called for this service.\nEvent: " + JSON.stringify(_event.data));
98
- }
99
- } else if (_this._status !== InterpreterStatus.Running) {
94
+ if (_this.status !== InterpreterStatus.Running && !_this.options.deferEvents) {
100
95
  throw new Error("Event \"" + _event.name + "\" was sent to uninitialized service \"" + _this.machine.id + "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.\nEvent: " + JSON.stringify(_event.data));
101
96
  }
102
97
 
@@ -115,7 +110,7 @@ function () {
115
110
 
116
111
  this.sendTo = function (event, to) {
117
112
  var isParent = _this.parent && (to === SpecialTargets.Parent || _this.parent.id === to);
118
- var target = isParent ? _this.parent : isActor(to) ? to : _this.children.get(to) || registry.get(to);
113
+ var target = isParent ? _this.parent : isString(to) ? _this.children.get(to) || registry.get(to) : isActor(to) ? to : undefined;
119
114
 
120
115
  if (!target) {
121
116
  if (!isParent) {
@@ -179,7 +174,7 @@ function () {
179
174
  Object.defineProperty(Interpreter.prototype, "state", {
180
175
  get: function () {
181
176
  if (!IS_PRODUCTION) {
182
- warn(this._status !== InterpreterStatus.NotStarted, "Attempted to read state from uninitialized service '" + this.id + "'. Make sure the service is started first.");
177
+ warn(this.status !== InterpreterStatus.NotStarted, "Attempted to read state from uninitialized service '" + this.id + "'. Make sure the service is started first.");
183
178
  }
184
179
 
185
180
  return this._state;
@@ -227,9 +222,13 @@ function () {
227
222
 
228
223
  if (this.options.execute) {
229
224
  this.execute(this.state);
230
- } // Dev tools
225
+ } // Update children
231
226
 
232
227
 
228
+ this.children.forEach(function (child) {
229
+ _this.state.children[child.id] = child;
230
+ }); // Dev tools
231
+
233
232
  if (this.devTools) {
234
233
  this.devTools.send(_event.data, state);
235
234
  } // Execute listeners
@@ -328,7 +327,7 @@ function () {
328
327
  Interpreter.prototype.onTransition = function (listener) {
329
328
  this.listeners.add(listener); // Send current state to listener
330
329
 
331
- if (this._status === InterpreterStatus.Running) {
330
+ if (this.status === InterpreterStatus.Running) {
332
331
  listener(this.state, this.state.event);
333
332
  }
334
333
 
@@ -359,7 +358,7 @@ function () {
359
358
 
360
359
  this.listeners.add(listener); // Send current state to listener
361
360
 
362
- if (this._status === InterpreterStatus.Running) {
361
+ if (this.status === InterpreterStatus.Running) {
363
362
  listener(this.state);
364
363
  }
365
364
 
@@ -448,14 +447,14 @@ function () {
448
447
  Interpreter.prototype.start = function (initialState) {
449
448
  var _this = this;
450
449
 
451
- if (this._status === InterpreterStatus.Running) {
450
+ if (this.status === InterpreterStatus.Running) {
452
451
  // Do not restart the service if it is already started
453
452
  return this;
454
453
  }
455
454
 
456
455
  registry.register(this.sessionId, this);
457
456
  this.initialized = true;
458
- this._status = InterpreterStatus.Running;
457
+ this.status = InterpreterStatus.Running;
459
458
  var resolvedState = initialState === undefined ? this.initialState : provide(this, function () {
460
459
  return isState(initialState) ? _this.machine.resolveState(initialState) : _this.machine.resolveState(State.from(initialState, _this.machine.context));
461
460
  });
@@ -479,6 +478,8 @@ function () {
479
478
  Interpreter.prototype.stop = function () {
480
479
  var e_6, _a, e_7, _b, e_8, _c, e_9, _d, e_10, _e;
481
480
 
481
+ var _this = this;
482
+
482
483
  try {
483
484
  for (var _f = __values(this.listeners), _g = _f.next(); !_g.done; _g = _f.next()) {
484
485
  var listener = _g.value;
@@ -547,8 +548,29 @@ function () {
547
548
  } finally {
548
549
  if (e_9) throw e_9.error;
549
550
  }
550
- } // Stop all children
551
+ }
551
552
 
553
+ this.state.configuration.forEach(function (stateNode) {
554
+ var e_11, _a;
555
+
556
+ try {
557
+ for (var _b = __values(stateNode.definition.exit), _c = _b.next(); !_c.done; _c = _b.next()) {
558
+ var action = _c.value;
559
+
560
+ _this.exec(action, _this.state);
561
+ }
562
+ } catch (e_11_1) {
563
+ e_11 = {
564
+ error: e_11_1
565
+ };
566
+ } finally {
567
+ try {
568
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
569
+ } finally {
570
+ if (e_11) throw e_11.error;
571
+ }
572
+ }
573
+ }); // Stop all children
552
574
 
553
575
  this.children.forEach(function (child) {
554
576
  if (isFunction(child.stop)) {
@@ -576,7 +598,7 @@ function () {
576
598
 
577
599
  this.scheduler.clear();
578
600
  this.initialized = false;
579
- this._status = InterpreterStatus.Stopped;
601
+ this.status = InterpreterStatus.Stopped;
580
602
  registry.free(this.sessionId);
581
603
  return this;
582
604
  };
@@ -584,18 +606,18 @@ function () {
584
606
  Interpreter.prototype.batch = function (events) {
585
607
  var _this = this;
586
608
 
587
- if (this._status === InterpreterStatus.NotStarted && this.options.deferEvents) {
609
+ if (this.status === InterpreterStatus.NotStarted && this.options.deferEvents) {
588
610
  // tslint:disable-next-line:no-console
589
611
  if (!IS_PRODUCTION) {
590
612
  warn(false, events.length + " event(s) were sent to uninitialized service \"" + this.machine.id + "\" and are deferred. Make sure .start() is called for this service.\nEvent: " + JSON.stringify(event));
591
613
  }
592
- } else if (this._status !== InterpreterStatus.Running) {
614
+ } else if (this.status !== InterpreterStatus.Running) {
593
615
  throw new Error( // tslint:disable-next-line:max-line-length
594
616
  events.length + " event(s) were sent to uninitialized service \"" + this.machine.id + "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.");
595
617
  }
596
618
 
597
619
  this.scheduler.schedule(function () {
598
- var e_11, _a;
620
+ var e_12, _a;
599
621
 
600
622
  var nextState = _this.state;
601
623
  var batchChanged = false;
@@ -621,15 +643,15 @@ function () {
621
643
 
622
644
  _loop_1(event_1);
623
645
  }
624
- } catch (e_11_1) {
625
- e_11 = {
626
- error: e_11_1
646
+ } catch (e_12_1) {
647
+ e_12 = {
648
+ error: e_12_1
627
649
  };
628
650
  } finally {
629
651
  try {
630
652
  if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
631
653
  } finally {
632
- if (e_11) throw e_11.error;
654
+ if (e_12) throw e_12.error;
633
655
  }
634
656
  }
635
657
 
@@ -676,7 +698,7 @@ function () {
676
698
  };
677
699
 
678
700
  Interpreter.prototype.forward = function (event) {
679
- var e_12, _a;
701
+ var e_13, _a;
680
702
 
681
703
  try {
682
704
  for (var _b = __values(this.forwardTo), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -689,15 +711,15 @@ function () {
689
711
 
690
712
  child.send(event);
691
713
  }
692
- } catch (e_12_1) {
693
- e_12 = {
694
- error: e_12_1
714
+ } catch (e_13_1) {
715
+ e_13 = {
716
+ error: e_13_1
695
717
  };
696
718
  } finally {
697
719
  try {
698
720
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
699
721
  } finally {
700
- if (e_12) throw e_12.error;
722
+ if (e_13) throw e_13.error;
701
723
  }
702
724
  }
703
725
  };
@@ -809,14 +831,14 @@ function () {
809
831
  }) : serviceCreator;
810
832
 
811
833
  if (isPromiseLike(source)) {
812
- this.state.children[id] = this.spawnPromise(Promise.resolve(source), id);
834
+ this.spawnPromise(Promise.resolve(source), id);
813
835
  } else if (isFunction(source)) {
814
- this.state.children[id] = this.spawnCallback(source, id);
836
+ this.spawnCallback(source, id);
815
837
  } else if (isObservable(source)) {
816
- this.state.children[id] = this.spawnObservable(source, id);
838
+ this.spawnObservable(source, id);
817
839
  } else if (isMachine(source)) {
818
840
  // TODO: try/catch here
819
- this.state.children[id] = this.spawnMachine(resolvedData ? source.withContext(resolvedData) : source, {
841
+ this.spawnMachine(resolvedData ? source.withContext(resolvedData) : source, {
820
842
  id: id,
821
843
  autoForward: autoForward
822
844
  });
@@ -882,7 +904,7 @@ function () {
882
904
  return this.spawnPromise(Promise.resolve(entity), name);
883
905
  } else if (isFunction(entity)) {
884
906
  return this.spawnCallback(entity, name);
885
- } else if (isActor(entity)) {
907
+ } else if (isSpawnedActor(entity)) {
886
908
  return this.spawnActor(entity);
887
909
  } else if (isObservable(entity)) {
888
910
  return this.spawnObservable(entity, name);
@@ -981,25 +1003,26 @@ function () {
981
1003
  return void 0;
982
1004
  },
983
1005
  subscribe: function (next, handleError, complete) {
1006
+ var observer = toObserver(next, handleError, complete);
984
1007
  var unsubscribed = false;
985
1008
  promise.then(function (response) {
986
1009
  if (unsubscribed) {
987
1010
  return;
988
1011
  }
989
1012
 
990
- next && next(response);
1013
+ observer.next(response);
991
1014
 
992
1015
  if (unsubscribed) {
993
1016
  return;
994
1017
  }
995
1018
 
996
- complete && complete();
1019
+ observer.complete();
997
1020
  }, function (err) {
998
1021
  if (unsubscribed) {
999
1022
  return;
1000
1023
  }
1001
1024
 
1002
- handleError(err);
1025
+ observer.error(err);
1003
1026
  });
1004
1027
  return {
1005
1028
  unsubscribe: function () {
@@ -1223,10 +1246,10 @@ function () {
1223
1246
  deferEvents: true,
1224
1247
  clock: {
1225
1248
  setTimeout: function (fn, ms) {
1226
- return global.setTimeout.call(null, fn, ms);
1249
+ return setTimeout(fn, ms);
1227
1250
  },
1228
1251
  clearTimeout: function (id) {
1229
- return global.clearTimeout.call(null, id);
1252
+ return clearTimeout(id);
1230
1253
  }
1231
1254
  },
1232
1255
  logger: global.console.log.bind(console),
@@ -1278,4 +1301,4 @@ function interpret(machine, options) {
1278
1301
  return interpreter;
1279
1302
  }
1280
1303
 
1281
- export { Interpreter, interpret, spawn };
1304
+ export { Interpreter, InterpreterStatus, interpret, spawn };
package/es/match.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { State } from './State';
2
2
  import { StateValue, EventObject } from './types';
3
3
  export declare type ValueFromStateGetter<T, TContext, TEvent extends EventObject> = (state: State<TContext, TEvent>) => T;
4
- export declare type StatePatternTuple<T, TContext, TEvent extends EventObject> = [StateValue, ValueFromStateGetter<T, TContext, TEvent>];
4
+ export declare type StatePatternTuple<T, TContext, TEvent extends EventObject> = [
5
+ StateValue,
6
+ ValueFromStateGetter<T, TContext, TEvent>
7
+ ];
5
8
  export declare function matchState<T, TContext, TEvent extends EventObject>(state: State<TContext, TEvent> | StateValue, patterns: Array<StatePatternTuple<T, TContext, TEvent>>, defaultValue: ValueFromStateGetter<T, TContext, TEvent>): T;
6
9
  //# sourceMappingURL=match.d.ts.map
package/es/types.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { StateNode } from './StateNode';
2
2
  import { State } from './State';
3
3
  import { Interpreter, Clock } from './interpreter';
4
- import { Actor } from './Actor';
5
4
  export declare type EventType = string;
6
5
  export declare type ActionType = string;
7
6
  export declare type MetaObject = Record<string, any>;
@@ -116,6 +115,24 @@ export interface ActivityDefinition<TContext, TEvent extends EventObject> extend
116
115
  type: string;
117
116
  }
118
117
  export declare type Sender<TEvent extends EventObject> = (event: Event<TEvent>) => void;
118
+ declare type ExcludeType<A> = {
119
+ [K in Exclude<keyof A, 'type'>]: A[K];
120
+ };
121
+ declare type ExtractExtraParameters<A, T> = A extends {
122
+ type: T;
123
+ } ? ExcludeType<A> : never;
124
+ declare type ExtractSimple<A> = A extends any ? {} extends ExcludeType<A> ? A : never : never;
125
+ declare type NeverIfEmpty<T> = {} extends T ? never : T;
126
+ export interface PayloadSender<TEvent extends EventObject> {
127
+ /**
128
+ * Send an event object or just the event type, if the event has no other payload
129
+ */
130
+ (event: TEvent | ExtractSimple<TEvent>['type']): void;
131
+ /**
132
+ * Send an event type and its payload
133
+ */
134
+ <K extends TEvent['type']>(eventType: K, payload: NeverIfEmpty<ExtractExtraParameters<TEvent, K>>): void;
135
+ }
119
136
  export declare type Receiver<TEvent extends EventObject> = (listener: (event: TEvent) => void) => void;
120
137
  export declare type InvokeCallback = (callback: Sender<any>, onReceive: Receiver<EventObject>) => any;
121
138
  export interface InvokeMeta {
@@ -205,7 +222,7 @@ export interface InvokeSourceDefinition {
205
222
  [key: string]: any;
206
223
  type: string;
207
224
  }
208
- export declare type InvokeConfig<TContext, TEvent extends EventObject> = {
225
+ export interface InvokeConfig<TContext, TEvent extends EventObject> {
209
226
  /**
210
227
  * The unique identifier for the invoked machine. If not specified, this
211
228
  * will be the machine's own `id`, or the URL (from `src`).
@@ -242,7 +259,7 @@ export declare type InvokeConfig<TContext, TEvent extends EventObject> = {
242
259
  * The transition to take upon the invoked child machine sending an error event.
243
260
  */
244
261
  onError?: string | SingleOrArray<TransitionConfig<TContext, DoneInvokeEvent<any>>>;
245
- };
262
+ }
246
263
  export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> {
247
264
  /**
248
265
  * The relative key of the state node, which represents its location in the overall state value.
@@ -541,7 +558,7 @@ export interface NullEvent {
541
558
  }
542
559
  export interface ActivityActionObject<TContext, TEvent extends EventObject> extends ActionObject<TContext, TEvent> {
543
560
  type: ActionTypes.Start | ActionTypes.Stop;
544
- activity: ActivityDefinition<TContext, TEvent>;
561
+ activity: ActivityDefinition<TContext, TEvent> | undefined;
545
562
  exec: ActionFunction<TContext, TEvent> | undefined;
546
563
  }
547
564
  export interface InvokeActionObject<TContext, TEvent extends EventObject> extends ActivityActionObject<TContext, TEvent> {
@@ -557,18 +574,32 @@ export interface LogActionObject<TContext, TEvent extends EventObject> extends L
557
574
  value: any;
558
575
  }
559
576
  export interface SendAction<TContext, TEvent extends EventObject, TSentEvent extends EventObject> extends ActionObject<TContext, TEvent> {
560
- to: string | number | Actor | ExprWithMeta<TContext, TEvent, string | number | Actor> | undefined;
577
+ to: string | number | ActorRef<any> | ExprWithMeta<TContext, TEvent, string | number | ActorRef<any>> | undefined;
561
578
  event: TSentEvent | SendExpr<TContext, TEvent, TSentEvent>;
562
579
  delay?: number | string | DelayExpr<TContext, TEvent>;
563
580
  id: string | number;
564
581
  }
565
582
  export interface SendActionObject<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject> extends SendAction<TContext, TEvent, TSentEvent> {
566
- to: string | number | Actor | undefined;
583
+ to: string | number | ActorRef<any> | undefined;
567
584
  _event: SCXML.Event<TSentEvent>;
568
585
  event: TSentEvent;
569
586
  delay?: number;
570
587
  id: string | number;
571
588
  }
589
+ export interface StopAction<TContext, TEvent extends EventObject> extends ActionObject<TContext, TEvent> {
590
+ type: ActionTypes.Stop;
591
+ activity: string | {
592
+ id: string;
593
+ } | Expr<TContext, TEvent, string | {
594
+ id: string;
595
+ }>;
596
+ }
597
+ export interface StopActionObject {
598
+ type: ActionTypes.Stop;
599
+ activity: {
600
+ id: string;
601
+ };
602
+ }
572
603
  export declare type Expr<TContext, TEvent extends EventObject, T> = (context: TContext, event: TEvent) => T;
573
604
  export declare type ExprWithMeta<TContext, TEvent extends EventObject, T> = (context: TContext, event: TEvent, meta: SCXMLEventMeta<TEvent>) => T;
574
605
  export declare type SendExpr<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject> = ExprWithMeta<TContext, TEvent, TSentEvent>;
@@ -579,7 +610,7 @@ export declare enum SpecialTargets {
579
610
  export interface SendActionOptions<TContext, TEvent extends EventObject> {
580
611
  id?: string | number;
581
612
  delay?: number | string | DelayExpr<TContext, TEvent>;
582
- to?: string | ExprWithMeta<TContext, TEvent, string | number | Actor>;
613
+ to?: string | ExprWithMeta<TContext, TEvent, string | number | ActorRef<any>>;
583
614
  }
584
615
  export interface CancelAction extends ActionObject<any, any> {
585
616
  sendId: string | number;
@@ -620,6 +651,7 @@ export interface TransitionDefinition<TContext, TEvent extends EventObject> exte
620
651
  actions: Array<ActionObject<TContext, TEvent>>;
621
652
  cond?: Guard<TContext, TEvent>;
622
653
  eventType: TEvent['type'] | NullEvent['type'] | '*';
654
+ meta?: Record<string, any>;
623
655
  };
624
656
  }
625
657
  export declare type TransitionDefinitionMap<TContext, TEvent extends EventObject> = {
@@ -709,7 +741,7 @@ export interface StateConfig<TContext, TEvent extends EventObject> {
709
741
  events?: TEvent[];
710
742
  configuration: Array<StateNode<TContext, any, TEvent>>;
711
743
  transitions: Array<TransitionDefinition<TContext, TEvent>>;
712
- children: Record<string, Actor>;
744
+ children: Record<string, ActorRef<any>>;
713
745
  done?: boolean;
714
746
  }
715
747
  export interface StateSchema<TC = any> {
@@ -809,17 +841,29 @@ export declare namespace SCXML {
809
841
  $$type: 'scxml';
810
842
  }
811
843
  }
812
- export interface Unsubscribable {
813
- unsubscribe(): void;
814
- }
815
- export interface Subscribable<T> {
816
- subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Unsubscribable;
817
- }
818
844
  export interface Observer<T> {
819
845
  next: (value: T) => void;
820
846
  error: (err: any) => void;
821
847
  complete: () => void;
822
848
  }
849
+ export interface Subscription {
850
+ unsubscribe(): void;
851
+ }
852
+ export interface Subscribable<T> {
853
+ subscribe(observer: Observer<T>): Subscription;
854
+ subscribe(next: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;
855
+ }
823
856
  export declare type Spawnable = StateMachine<any, any, any> | Promise<any> | InvokeCallback | Subscribable<any>;
857
+ export interface ActorRef<TEvent extends EventObject, TEmitted = any> extends Subscribable<TEmitted> {
858
+ send: Sender<TEvent>;
859
+ }
860
+ export interface SpawnedActorRef<TEvent extends EventObject, TEmitted = any> extends ActorRef<TEvent, TEmitted> {
861
+ id: string;
862
+ stop?: () => void;
863
+ toJSON?: () => any;
864
+ }
865
+ export declare type ActorRefFrom<T extends StateMachine<any, any, any>> = T extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate> ? SpawnedActorRef<TEvent, State<TContext, TEvent, any, TTypestate>> & {
866
+ state: State<TContext, TEvent, any, TTypestate>;
867
+ } : never;
824
868
  export {};
825
869
  //# sourceMappingURL=types.d.ts.map
package/es/utils.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Event, StateValue, ActionType, Action, EventObject, PropertyMapper, Mapper, EventType, HistoryValue, AssignAction, Condition, Subscribable, StateMachine, ConditionPredicate, SCXML, StateLike, EventData, TransitionConfig, TransitionConfigTarget, NullEvent, SingleOrArray, Guard, InvokeSourceDefinition } from './types';
2
2
  import { StateNode } from './StateNode';
3
- import { State } from '.';
3
+ import { Observer, State } from '.';
4
4
  import { Actor } from './Actor';
5
5
  export declare function keys<T extends object>(value: T): Array<keyof T & string>;
6
6
  export declare function matchesState(parentStateId: StateValue, childStateId: StateValue, delimiter?: string): boolean;
@@ -66,4 +66,5 @@ export declare function normalizeTarget<TContext, TEvent extends EventObject>(ta
66
66
  export declare function reportUnhandledExceptionOnInvocation(originalError: any, currentError: any, id: string): void;
67
67
  export declare function evaluateGuard<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent>, guard: Guard<TContext, TEvent>, context: TContext, _event: SCXML.Event<TEvent>, state: State<TContext, TEvent>): boolean;
68
68
  export declare function toInvokeSource(src: string | InvokeSourceDefinition): InvokeSourceDefinition;
69
+ export declare function toObserver<T>(nextHandler: Observer<T> | ((value: T) => void), errorHandler?: (error: any) => void, completionHandler?: () => void): Observer<T>;
69
70
  //# sourceMappingURL=utils.d.ts.map
package/es/utils.js CHANGED
@@ -494,6 +494,10 @@ function isMachine(value) {
494
494
  }
495
495
  }
496
496
 
497
+ function isActor(value) {
498
+ return !!value && typeof value.send === 'function';
499
+ }
500
+
497
501
  var uniqueId = /*#__PURE__*/function () {
498
502
  var currentId = 0;
499
503
  return function () {
@@ -597,4 +601,20 @@ function toInvokeSource(src) {
597
601
  return src;
598
602
  }
599
603
 
600
- export { evaluateGuard, flatten, getEventType, isArray, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
604
+ function toObserver(nextHandler, errorHandler, completionHandler) {
605
+ if (typeof nextHandler === 'object') {
606
+ return nextHandler;
607
+ }
608
+
609
+ var noop = function () {
610
+ return void 0;
611
+ };
612
+
613
+ return {
614
+ next: nextHandler,
615
+ error: errorHandler || noop,
616
+ complete: completionHandler || noop
617
+ };
618
+ }
619
+
620
+ export { evaluateGuard, flatten, getEventType, isActor, isArray, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toObserver, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
package/lib/Actor.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EventObject, Subscribable, InvokeDefinition, AnyEventObject, StateMachine, Spawnable, SCXML } from './types';
2
+ import { ActorRef, SpawnedActorRef } from '.';
2
3
  export interface Actor<TContext = any, TEvent extends EventObject = AnyEventObject> extends Subscribable<TContext> {
3
4
  id: string;
4
5
  send: (event: TEvent) => any;
@@ -19,5 +20,6 @@ export declare function createNullActor(id: string): Actor;
19
20
  */
20
21
  export declare function createInvocableActor<TC, TE extends EventObject>(invokeDefinition: InvokeDefinition<TC, TE>, machine: StateMachine<TC, any, TE>, context: TC, _event: SCXML.Event<TE>): Actor;
21
22
  export declare function createDeferredActor(entity: Spawnable, id: string, data?: any): Actor;
22
- export declare function isActor(item: any): item is Actor;
23
+ export declare function isActor(item: any): item is ActorRef<any>;
24
+ export declare function isSpawnedActor(item: any): item is SpawnedActorRef<any>;
23
25
  //# sourceMappingURL=Actor.d.ts.map
package/lib/Actor.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isActor = exports.createDeferredActor = exports.createInvocableActor = exports.createNullActor = void 0;
3
+ exports.isSpawnedActor = exports.isActor = exports.createDeferredActor = exports.createInvocableActor = exports.createNullActor = void 0;
4
4
  var utils_1 = require("./utils");
5
5
  var serviceScope = require("./serviceScope");
6
6
  function createNullActor(id) {
@@ -55,3 +55,7 @@ function isActor(item) {
55
55
  }
56
56
  }
57
57
  exports.isActor = isActor;
58
+ function isSpawnedActor(item) {
59
+ return isActor(item) && 'id' in item;
60
+ }
61
+ exports.isSpawnedActor = isSpawnedActor;
package/lib/State.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { StateValue, ActivityMap, EventObject, HistoryValue, ActionObject, EventType, StateConfig, SCXML, StateSchema, TransitionDefinition, Typestate } from './types';
1
+ import { StateValue, ActivityMap, EventObject, HistoryValue, ActionObject, EventType, StateConfig, SCXML, StateSchema, TransitionDefinition, Typestate, ActorRef } from './types';
2
2
  import { StateNode } from './StateNode';
3
- import { Actor } from './Actor';
4
3
  export declare function stateValuesEqual(a: StateValue | undefined, b: StateValue | undefined): boolean;
5
4
  export declare function isState<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
6
5
  value: any;
@@ -50,7 +49,7 @@ export declare class State<TContext, TEvent extends EventObject = EventObject, T
50
49
  /**
51
50
  * An object mapping actor IDs to spawned actors/invoked services.
52
51
  */
53
- children: Record<string, Actor>;
52
+ children: Record<string, ActorRef<any>>;
54
53
  /**
55
54
  * Creates a new State instance for the given `stateValue` and `context`.
56
55
  * @param stateValue
package/lib/StateNode.js CHANGED
@@ -792,8 +792,9 @@ var StateNode = /** @class */ (function () {
792
792
  types_1.SpecialTargets.Internal);
793
793
  }), 2), raisedEvents = _c[0], nonRaisedActions = _c[1];
794
794
  var invokeActions = resolvedActions.filter(function (action) {
795
+ var _a;
795
796
  return (action.type === actionTypes.start &&
796
- action.activity.type ===
797
+ ((_a = action.activity) === null || _a === void 0 ? void 0 : _a.type) ===
797
798
  actionTypes.invoke);
798
799
  });
799
800
  var children = invokeActions.reduce(function (acc, action) {
@@ -882,7 +883,6 @@ var StateNode = /** @class */ (function () {
882
883
  : undefined);
883
884
  maybeNextState.changed = changed;
884
885
  // Preserve original history after raised events
885
- maybeNextState.historyValue = nextState.historyValue;
886
886
  maybeNextState.history = history;
887
887
  return maybeNextState;
888
888
  };
package/lib/actions.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseConditon, ChooseAction, AnyEventObject } from './types';
1
+ import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseConditon, ChooseAction, AnyEventObject, Expr } from './types';
2
2
  import * as actionTypes from './actionTypes';
3
3
  import { State } from './State';
4
4
  import { StateNode } from './StateNode';
5
+ import { StopAction, StopActionObject } from '.';
5
6
  export { actionTypes };
6
7
  export declare const initEvent: SCXML.Event<{
7
8
  type: ActionTypes;
@@ -77,9 +78,12 @@ export declare function start<TContext, TEvent extends EventObject>(activity: st
77
78
  /**
78
79
  * Stops an activity.
79
80
  *
80
- * @param activity The activity to stop.
81
+ * @param actorRef The activity to stop.
81
82
  */
82
- export declare function stop<TContext, TEvent extends EventObject>(activity: string | ActivityDefinition<TContext, TEvent>): ActivityActionObject<TContext, TEvent>;
83
+ export declare function stop<TContext, TEvent extends EventObject>(actorRef: string | ActivityDefinition<TContext, TEvent> | Expr<TContext, TEvent, string | {
84
+ id: string;
85
+ }>): StopAction<TContext, TEvent>;
86
+ export declare function resolveStop<TContext, TEvent extends EventObject>(action: StopAction<TContext, TEvent>, context: TContext, _event: SCXML.Event<TEvent>): StopActionObject;
83
87
  /**
84
88
  * Updates the current context of the machine.
85
89
  *