xstate 4.23.1 → 4.24.0

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/types.d.ts CHANGED
@@ -2,6 +2,8 @@ import { StateNode } from './StateNode';
2
2
  import { State } from './State';
3
3
  import { Interpreter, Clock } from './interpreter';
4
4
  import { Model } from './model.types';
5
+ declare type AnyFunction = (...args: any[]) => any;
6
+ declare type ReturnTypeOrValue<T> = T extends AnyFunction ? ReturnType<T> : T;
5
7
  export declare type EventType = string;
6
8
  export declare type ActionType = string;
7
9
  export declare type MetaObject = Record<string, any>;
@@ -17,20 +19,22 @@ export interface EventObject {
17
19
  export interface AnyEventObject extends EventObject {
18
20
  [key: string]: any;
19
21
  }
20
- /**
21
- * The full definition of an action, with a string `type` and an
22
- * `exec` implementation function.
23
- */
24
- export interface ActionObject<TContext, TEvent extends EventObject> {
22
+ export interface BaseActionObject {
25
23
  /**
26
24
  * The type of action that is executed.
27
25
  */
28
26
  type: string;
27
+ [other: string]: any;
28
+ }
29
+ /**
30
+ * The full definition of an action, with a string `type` and an
31
+ * `exec` implementation function.
32
+ */
33
+ export interface ActionObject<TContext, TEvent extends EventObject> extends BaseActionObject {
29
34
  /**
30
35
  * The implementation for executing the action.
31
36
  */
32
37
  exec?: ActionFunction<TContext, TEvent>;
33
- [other: string]: any;
34
38
  }
35
39
  export declare type DefaultContext = Record<string, any> | undefined;
36
40
  export declare type EventData = Record<string, any> & {
@@ -40,8 +44,8 @@ export declare type EventData = Record<string, any> & {
40
44
  * The specified string event types or the specified event objects.
41
45
  */
42
46
  export declare type Event<TEvent extends EventObject> = TEvent['type'] | TEvent;
43
- export interface ActionMeta<TContext, TEvent extends EventObject> extends StateMeta<TContext, TEvent> {
44
- action: ActionObject<TContext, TEvent>;
47
+ export interface ActionMeta<TContext, TEvent extends EventObject, TAction extends ActionObject<TContext, TEvent> = ActionObject<TContext, TEvent>> extends StateMeta<TContext, TEvent> {
48
+ action: TAction;
45
49
  _event: SCXML.Event<TEvent>;
46
50
  }
47
51
  export interface AssignMeta<TContext, TEvent extends EventObject> {
@@ -49,12 +53,22 @@ export interface AssignMeta<TContext, TEvent extends EventObject> {
49
53
  action: AssignAction<TContext, TEvent>;
50
54
  _event: SCXML.Event<TEvent>;
51
55
  }
52
- export declare type ActionFunction<TContext, TEvent extends EventObject> = (context: TContext, event: TEvent, meta: ActionMeta<TContext, TEvent>) => void;
56
+ export declare type ActionFunction<TContext, TEvent extends EventObject, TAction extends ActionObject<TContext, TEvent> = ActionObject<TContext, TEvent>> = (context: TContext, event: TEvent, meta: ActionMeta<TContext, TEvent, TAction>) => void;
53
57
  export interface ChooseConditon<TContext, TEvent extends EventObject> {
54
58
  cond?: Condition<TContext, TEvent>;
55
59
  actions: Actions<TContext, TEvent>;
56
60
  }
57
61
  export declare type Action<TContext, TEvent extends EventObject> = ActionType | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent>;
62
+ /**
63
+ * Extracts action objects that have no extra properties.
64
+ */
65
+ declare type SimpleActionsOf<T extends BaseActionObject> = ActionObject<any, any> extends T ? T : ExtractWithSimpleSupport<T>;
66
+ /**
67
+ * Events that do not require payload
68
+ */
69
+ export declare type SimpleEventsOf<TEvent extends EventObject> = ExtractWithSimpleSupport<TEvent>;
70
+ export declare type BaseAction<TContext, TEvent extends EventObject, TAction extends BaseActionObject> = SimpleActionsOf<TAction>['type'] | TAction | RaiseAction<any> | SendAction<TContext, TEvent, any> | AssignAction<TContext, TEvent> | LogAction<TContext, TEvent> | CancelAction | StopAction<TContext, TEvent> | ChooseAction<TContext, TEvent> | ActionFunction<TContext, TEvent>;
71
+ export declare type BaseActions<TContext, TEvent extends EventObject, TAction extends BaseActionObject> = SingleOrArray<BaseAction<TContext, TEvent, TAction>>;
58
72
  export declare type Actions<TContext, TEvent extends EventObject> = SingleOrArray<Action<TContext, TEvent>>;
59
73
  export declare type StateKey = string | State<any>;
60
74
  export interface StateValueMap {
@@ -122,13 +136,17 @@ declare type ExcludeType<A> = {
122
136
  declare type ExtractExtraParameters<A, T> = A extends {
123
137
  type: T;
124
138
  } ? ExcludeType<A> : never;
125
- declare type ExtractSimple<A> = A extends any ? {} extends ExcludeType<A> ? A : never : never;
139
+ declare type ExtractWithSimpleSupport<T extends {
140
+ type: string;
141
+ }> = T extends any ? {
142
+ type: T['type'];
143
+ } extends T ? T : never : never;
126
144
  declare type NeverIfEmpty<T> = {} extends T ? never : T;
127
145
  export interface PayloadSender<TEvent extends EventObject> {
128
146
  /**
129
147
  * Send an event object or just the event type, if the event has no other payload
130
148
  */
131
- (event: TEvent | ExtractSimple<TEvent>['type']): void;
149
+ (event: TEvent | ExtractWithSimpleSupport<TEvent>['type']): void;
132
150
  /**
133
151
  * Send an event type and its payload
134
152
  */
@@ -194,8 +212,8 @@ export declare type SingleOrArray<T> = T[] | T;
194
212
  export declare type StateNodesConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> = {
195
213
  [K in keyof TStateSchema['states']]: StateNode<TContext, TStateSchema['states'][K], TEvent>;
196
214
  };
197
- export declare type StatesConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> = {
198
- [K in keyof TStateSchema['states']]: StateNodeConfig<TContext, TStateSchema['states'][K], TEvent>;
215
+ export declare type StatesConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject> = {
216
+ [K in keyof TStateSchema['states']]: StateNodeConfig<TContext, TStateSchema['states'][K], TEvent, TAction>;
199
217
  };
200
218
  export declare type StatesDefinition<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> = {
201
219
  [K in keyof TStateSchema['states']]: StateNodeDefinition<TContext, TStateSchema['states'][K], TEvent>;
@@ -261,7 +279,7 @@ export interface InvokeConfig<TContext, TEvent extends EventObject> {
261
279
  */
262
280
  onError?: string | SingleOrArray<TransitionConfig<TContext, DoneInvokeEvent<any>>>;
263
281
  }
264
- export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> {
282
+ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject> {
265
283
  /**
266
284
  * The relative key of the state node, which represents its location in the overall state value.
267
285
  * This is automatically determined by the configuration shape via the key where it was defined.
@@ -285,12 +303,6 @@ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEv
285
303
  * - `'final'` - final state node
286
304
  */
287
305
  type?: 'atomic' | 'compound' | 'parallel' | 'final' | 'history';
288
- /**
289
- * The initial context (extended state) of the machine.
290
- *
291
- * Can be an object or a function that returns an object.
292
- */
293
- context?: TContext | (() => TContext);
294
306
  /**
295
307
  * Indicates whether the state node is a history state node, and what
296
308
  * type of history:
@@ -300,7 +312,7 @@ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEv
300
312
  /**
301
313
  * The mapping of state node keys to their state node configurations (recursive).
302
314
  */
303
- states?: StatesConfig<TContext, TStateSchema, TEvent> | undefined;
315
+ states?: StatesConfig<TContext, TStateSchema, TEvent, TAction> | undefined;
304
316
  /**
305
317
  * The services to invoke upon entering this state node. These services will be stopped upon exiting this state node.
306
318
  */
@@ -318,7 +330,7 @@ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEv
318
330
  /**
319
331
  * The action(s) to be executed upon entering the state node.
320
332
  */
321
- entry?: Actions<TContext, TEvent>;
333
+ entry?: BaseActions<TContext, TEvent, TAction>;
322
334
  /**
323
335
  * The action(s) to be executed upon exiting the state node.
324
336
  *
@@ -328,7 +340,7 @@ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEv
328
340
  /**
329
341
  * The action(s) to be executed upon exiting the state node.
330
342
  */
331
- exit?: Actions<TContext, TEvent>;
343
+ exit?: BaseActions<TContext, TEvent, TAction>;
332
344
  /**
333
345
  * The potential transition(s) to be taken upon reaching a final child state node.
334
346
  *
@@ -348,6 +360,8 @@ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEv
348
360
  /**
349
361
  * The activities to be started upon entering the state node,
350
362
  * and stopped upon exiting the state node.
363
+ *
364
+ * @deprecated Use `invoke` instead.
351
365
  */
352
366
  activities?: SingleOrArray<Activity<TContext, TEvent>>;
353
367
  /**
@@ -406,6 +420,9 @@ export interface StateNodeDefinition<TContext, TStateSchema extends StateSchema,
406
420
  transitions: Array<TransitionDefinition<TContext, TEvent>>;
407
421
  entry: Array<ActionObject<TContext, TEvent>>;
408
422
  exit: Array<ActionObject<TContext, TEvent>>;
423
+ /**
424
+ * @deprecated
425
+ */
409
426
  activities: Array<ActivityDefinition<TContext, TEvent>>;
410
427
  meta: any;
411
428
  order: number;
@@ -432,13 +449,20 @@ export interface FinalStateNodeConfig<TContext, TEvent extends EventObject> exte
432
449
  data?: Mapper<TContext, TEvent, any> | PropertyMapper<TContext, TEvent, any>;
433
450
  }
434
451
  export declare type SimpleOrStateNodeConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> = AtomicStateNodeConfig<TContext, TEvent> | StateNodeConfig<TContext, TStateSchema, TEvent>;
435
- export declare type ActionFunctionMap<TContext, TEvent extends EventObject> = Record<string, ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent>>;
452
+ export declare type ActionFunctionMap<TContext, TEvent extends EventObject, TAction extends ActionObject<TContext, TEvent> = ActionObject<TContext, TEvent>> = {
453
+ [K in TAction['type']]?: ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent, TAction extends {
454
+ type: K;
455
+ } ? TAction : never>;
456
+ };
436
457
  export declare type DelayFunctionMap<TContext, TEvent extends EventObject> = Record<string, DelayConfig<TContext, TEvent>>;
437
458
  export declare type ServiceConfig<TContext, TEvent extends EventObject = AnyEventObject> = string | StateMachine<any, any, any> | InvokeCreator<TContext, TEvent>;
438
459
  export declare type DelayConfig<TContext, TEvent extends EventObject> = number | DelayExpr<TContext, TEvent>;
439
- export interface MachineOptions<TContext, TEvent extends EventObject> {
460
+ export interface MachineOptions<TContext, TEvent extends EventObject, TAction extends ActionObject<TContext, TEvent> = ActionObject<TContext, TEvent>> {
440
461
  guards: Record<string, ConditionPredicate<TContext, TEvent>>;
441
- actions: ActionFunctionMap<TContext, TEvent>;
462
+ actions: ActionFunctionMap<TContext, TEvent, TAction>;
463
+ /**
464
+ * @deprecated Use `services` instead.
465
+ */
442
466
  activities: Record<string, ActivityConfig<TContext, TEvent>>;
443
467
  services: Record<string, ServiceConfig<TContext, TEvent>>;
444
468
  delays: DelayFunctionMap<TContext, TEvent>;
@@ -451,7 +475,7 @@ export interface MachineOptions<TContext, TEvent extends EventObject> {
451
475
  */
452
476
  _key?: string;
453
477
  }
454
- export interface MachineConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> extends StateNodeConfig<TContext, TStateSchema, TEvent> {
478
+ export interface MachineConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = ActionObject<TContext, TEvent>> extends StateNodeConfig<TContext, TStateSchema, TEvent, TAction> {
455
479
  /**
456
480
  * The initial context (extended state)
457
481
  */
@@ -495,13 +519,13 @@ export interface HistoryStateNode<TContext> extends StateNode<TContext> {
495
519
  export interface StateMachine<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TTypestate extends Typestate<TContext> = {
496
520
  value: any;
497
521
  context: TContext;
498
- }> extends StateNode<TContext, TStateSchema, TEvent, TTypestate> {
522
+ }, _TAction extends ActionObject<TContext, TEvent> = ActionObject<TContext, TEvent>> extends StateNode<TContext, TStateSchema, TEvent, TTypestate> {
499
523
  id: string;
500
524
  states: StateNode<TContext, TStateSchema, TEvent>['states'];
501
- withConfig(options: Partial<MachineOptions<TContext, TEvent>>, context?: TContext): StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
502
- withContext(context: TContext): StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
525
+ withConfig(options: Partial<MachineOptions<TContext, TEvent>>, context?: TContext | (() => TContext)): StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
526
+ withContext(context: TContext | (() => TContext)): StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
503
527
  }
504
- export declare type StateFrom<TMachine extends StateMachine<any, any, any>> = ReturnType<TMachine['transition']>;
528
+ export declare type StateFrom<T extends StateMachine<any, any, any, any> | ((...args: any[]) => StateMachine<any, any, any, any>)> = T extends StateMachine<any, any, any> ? ReturnType<T['transition']> : T extends (...args: any[]) => StateMachine<any, any, any> ? ReturnType<ReturnType<T>['transition']> : never;
505
529
  export interface ActionMap<TContext, TEvent extends EventObject> {
506
530
  onEntry: Array<Action<TContext, TEvent>>;
507
531
  actions: Array<Action<TContext, TEvent>>;
@@ -768,6 +792,9 @@ export interface StateConfig<TContext, TEvent extends EventObject> {
768
792
  historyValue?: HistoryValue | undefined;
769
793
  history?: State<TContext, TEvent>;
770
794
  actions?: Array<ActionObject<TContext, TEvent>>;
795
+ /**
796
+ * @deprecated
797
+ */
771
798
  activities?: ActivityMap;
772
799
  meta?: any;
773
800
  events?: TEvent[];
@@ -776,6 +803,7 @@ export interface StateConfig<TContext, TEvent extends EventObject> {
776
803
  children: Record<string, ActorRef<any>>;
777
804
  done?: boolean;
778
805
  tags?: Set<string>;
806
+ machine?: StateMachine<TContext, any, TEvent, any>;
779
807
  }
780
808
  export interface StateSchema<TC = any> {
781
809
  meta?: any;
@@ -904,14 +932,15 @@ export interface ActorRef<TEvent extends EventObject, TEmitted = any> extends Su
904
932
  * @deprecated Use `ActorRef` instead.
905
933
  */
906
934
  export declare type SpawnedActorRef<TEvent extends EventObject, TEmitted = any> = ActorRef<TEvent, TEmitted>;
907
- export declare type ActorRefFrom<T extends StateMachine<any, any, any> | Promise<any> | Behavior<any>> = T extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate> ? ActorRef<TEvent, State<TContext, TEvent, any, TTypestate>> & {
935
+ export declare type ActorRefWithDeprecatedState<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext>> = ActorRef<TEvent, State<TContext, TEvent, any, TTypestate>> & {
908
936
  /**
909
937
  * @deprecated Use `.getSnapshot()` instead.
910
938
  */
911
939
  state: State<TContext, TEvent, any, TTypestate>;
912
- } : T extends Promise<infer U> ? ActorRef<never, U> : T extends Behavior<infer TEvent1, infer TEmitted> ? ActorRef<TEvent1, TEmitted> : never;
940
+ };
941
+ export declare type ActorRefFrom<T> = T extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate> ? ActorRefWithDeprecatedState<TContext, TEvent, TTypestate> : T extends (...args: any[]) => StateMachine<infer TContext, any, infer TEvent, infer TTypestate> ? ActorRefWithDeprecatedState<TContext, TEvent, TTypestate> : T extends Promise<infer U> ? ActorRef<never, U> : T extends Behavior<infer TEvent1, infer TEmitted> ? ActorRef<TEvent1, TEmitted> : T extends (...args: any[]) => Behavior<infer TEvent1, infer TEmitted> ? ActorRef<TEvent1, TEmitted> : never;
913
942
  export declare type AnyInterpreter = Interpreter<any, any, any, any>;
914
- export declare type InterpreterFrom<T extends StateMachine<any, any, any, any>> = T extends StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate> : never;
943
+ export declare type InterpreterFrom<T extends StateMachine<any, any, any, any> | ((...args: any[]) => StateMachine<any, any, any, any>)> = T extends StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate> : T extends (...args: any[]) => StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate> : never;
915
944
  export interface ActorContext<TEvent extends EventObject, TEmitted> {
916
945
  parent?: ActorRef<any, any>;
917
946
  self: ActorRef<TEvent, TEmitted>;
@@ -923,7 +952,8 @@ export interface Behavior<TEvent extends EventObject, TEmitted = any> {
923
952
  initialState: TEmitted;
924
953
  start?: (actorCtx: ActorContext<TEvent, TEmitted>) => TEmitted;
925
954
  }
926
- export declare type EventFrom<T> = T extends StateMachine<any, any, infer TEvent, any> ? TEvent : T extends Model<any, infer TEvent, any> ? TEvent : T extends State<any, infer TEvent, any, any> ? TEvent : T extends Interpreter<any, any, infer TEvent, any> ? TEvent : never;
927
- export declare type ContextFrom<T> = T extends StateMachine<infer TContext, any, any, any> ? TContext : T extends Model<infer TContext, any, any> ? TContext : T extends State<infer TContext, any, any, any> ? TContext : T extends Interpreter<infer TContext, any, any, any> ? TContext : never;
955
+ export declare type EmittedFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends ActorRef<infer _, infer TEmitted> ? TEmitted : R extends Behavior<infer _, infer TEmitted> ? TEmitted : R extends ActorContext<infer _, infer TEmitted> ? TEmitted : never : never;
956
+ export declare type EventFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer _, infer __, infer TEvent, infer ____> ? TEvent : R extends Model<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends State<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends Interpreter<infer _, infer __, infer TEvent, infer ____> ? TEvent : never : never;
957
+ export declare type ContextFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, infer _, infer __, infer ___> ? TContext : R extends Model<infer TContext, infer _, infer __, infer ___> ? TContext : R extends State<infer TContext, infer _, infer __, infer ___> ? TContext : R extends Interpreter<infer TContext, infer _, infer __, infer ___> ? TContext : never : never;
928
958
  export {};
929
959
  //# sourceMappingURL=types.d.ts.map
package/es/utils.js CHANGED
@@ -545,7 +545,7 @@ function evaluateGuard(machine, guard, context, _event, state) {
545
545
  }; // TODO: do not hardcode!
546
546
 
547
547
  if (guard.type === DEFAULT_GUARD_TYPE) {
548
- return guard.predicate(context, _event.data, guardMeta);
548
+ return ((guards === null || guards === void 0 ? void 0 : guards[guard.name]) || guard.predicate)(context, _event.data, guardMeta);
549
549
  }
550
550
 
551
551
  var condFn = guards[guard.type];
package/lib/Machine.d.ts CHANGED
@@ -1,18 +1,18 @@
1
- import { StateMachine, MachineOptions, DefaultContext, MachineConfig, StateSchema, EventObject, AnyEventObject, Typestate, EventFrom } from './types';
2
- import { Model, ModelContextFrom } from './model.types';
1
+ import { StateMachine, MachineOptions, DefaultContext, MachineConfig, StateSchema, EventObject, AnyEventObject, Typestate, EventFrom, BaseActionObject } from './types';
2
+ import { Model, ModelContextFrom, ModelActionsFrom } from './model.types';
3
3
  /**
4
4
  * @deprecated Use `createMachine(...)` instead.
5
5
  */
6
6
  export declare function Machine<TContext = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, any, TEvent>;
7
7
  export declare function Machine<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, TStateSchema, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, TStateSchema, TEvent>;
8
- export declare function createMachine<TModel extends Model<any, any, any>, TContext = ModelContextFrom<TModel>, TEvent extends EventObject = EventFrom<TModel>, TTypestate extends Typestate<TContext> = {
8
+ export declare function createMachine<TModel extends Model<any, any, any, any>, TContext = ModelContextFrom<TModel>, TEvent extends EventObject = EventFrom<TModel>, TTypestate extends Typestate<TContext> = {
9
9
  value: any;
10
10
  context: TContext;
11
- }>(config: MachineConfig<TContext, any, TEvent> & {
11
+ }, TAction extends BaseActionObject = ModelActionsFrom<TModel>>(config: MachineConfig<TContext, any, TEvent, TAction> & {
12
12
  context: TContext;
13
- }, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
13
+ }, options?: Partial<MachineOptions<TContext, TEvent, TAction>>): StateMachine<TContext, any, TEvent, TTypestate>;
14
14
  export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> = {
15
15
  value: any;
16
16
  context: TContext;
17
- }>(config: TContext extends Model<any, any, any> ? never : MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
17
+ }>(config: TContext extends Model<any, any, any, any> ? never : MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
18
18
  //# sourceMappingURL=Machine.d.ts.map
package/lib/State.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { StateValue, ActivityMap, EventObject, HistoryValue, ActionObject, EventType, StateConfig, SCXML, StateSchema, TransitionDefinition, Typestate, ActorRef } from './types';
1
+ import { StateValue, ActivityMap, EventObject, HistoryValue, ActionObject, EventType, StateConfig, SCXML, StateSchema, TransitionDefinition, Typestate, ActorRef, StateMachine, SimpleEventsOf } from './types';
2
2
  import { StateNode } from './StateNode';
3
3
  export declare function stateValuesEqual(a: StateValue | undefined, b: StateValue | undefined): boolean;
4
4
  export declare function isState<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
@@ -51,6 +51,7 @@ export declare class State<TContext, TEvent extends EventObject = EventObject, T
51
51
  */
52
52
  children: Record<string, ActorRef<any>>;
53
53
  tags: Set<string>;
54
+ machine: StateMachine<TContext, any, TEvent, TTypestate> | undefined;
54
55
  /**
55
56
  * Creates a new State instance for the given `stateValue` and `context`.
56
57
  * @param stateValue
@@ -87,7 +88,7 @@ export declare class State<TContext, TEvent extends EventObject = EventObject, T
87
88
  * @param delimiter The character(s) that separate each subpath in the string state node path.
88
89
  */
89
90
  toStrings(stateValue?: StateValue, delimiter?: string): string[];
90
- toJSON(): Omit<this, "configuration" | "transitions" | "tags"> & {
91
+ toJSON(): Omit<this, "configuration" | "transitions" | "tags" | "machine"> & {
91
92
  tags: string[];
92
93
  };
93
94
  /**
@@ -105,5 +106,11 @@ export declare class State<TContext, TEvent extends EventObject = EventObject, T
105
106
  * @param tag
106
107
  */
107
108
  hasTag(tag: string): boolean;
109
+ /**
110
+ * Determines whether sending the `event` will cause a transition.
111
+ * @param event The event to test
112
+ * @returns Whether the event will cause a transition
113
+ */
114
+ can(event: TEvent | SimpleEventsOf<TEvent>['type']): boolean;
108
115
  }
109
116
  //# sourceMappingURL=State.d.ts.map
package/lib/State.js CHANGED
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('./_virtual/_tslib.js');
6
6
  var constants = require('./constants.js');
7
+ var environment = require('./environment.js');
7
8
  var utils = require('./utils.js');
8
9
  var stateUtils = require('./stateUtils.js');
9
10
  var actions = require('./actions.js');
@@ -93,7 +94,8 @@ function () {
93
94
  this.transitions = config.transitions;
94
95
  this.children = config.children;
95
96
  this.done = !!config.done;
96
- this.tags = (_a = config.tags) !== null && _a !== void 0 ? _a : new Set();
97
+ this.tags = (_a = Array.isArray(config.tags) ? new Set(config.tags) : config.tags) !== null && _a !== void 0 ? _a : new Set();
98
+ this.machine = config.machine;
97
99
  Object.defineProperty(this, 'nextEvents', {
98
100
  get: function () {
99
101
  return stateUtils.nextEvents(_this.configuration);
@@ -221,7 +223,8 @@ function () {
221
223
  configuration = _a.configuration,
222
224
  transitions = _a.transitions,
223
225
  tags = _a.tags,
224
- jsonValues = _tslib.__rest(_a, ["configuration", "transitions", "tags"]);
226
+ machine = _a.machine,
227
+ jsonValues = _tslib.__rest(_a, ["configuration", "transitions", "tags", "machine"]);
225
228
 
226
229
  return _tslib.__assign(_tslib.__assign({}, jsonValues), {
227
230
  tags: Array.from(tags)
@@ -245,6 +248,22 @@ function () {
245
248
  State.prototype.hasTag = function (tag) {
246
249
  return this.tags.has(tag);
247
250
  };
251
+ /**
252
+ * Determines whether sending the `event` will cause a transition.
253
+ * @param event The event to test
254
+ * @returns Whether the event will cause a transition
255
+ */
256
+
257
+
258
+ State.prototype.can = function (event) {
259
+ var _a;
260
+
261
+ if (environment.IS_PRODUCTION) {
262
+ utils.warn(!!this.machine, "state.can(...) used outside of a machine-created State object; this will always return false.");
263
+ }
264
+
265
+ return !!((_a = this.machine) === null || _a === void 0 ? void 0 : _a.transition(this, event).changed);
266
+ };
248
267
 
249
268
  return State;
250
269
  }();
@@ -130,13 +130,13 @@ declare class StateNode<TContext = any, TStateSchema extends StateSchema = any,
130
130
  * @param options Options (actions, guards, activities, services) to recursively merge with the existing options.
131
131
  * @param context Custom context (will override predefined context)
132
132
  */
133
- withConfig(options: Partial<MachineOptions<TContext, TEvent>>, context?: TContext): StateNode<TContext, TStateSchema, TEvent, TTypestate>;
133
+ withConfig(options: Partial<MachineOptions<TContext, TEvent>>, context?: TContext | (() => TContext)): StateNode<TContext, TStateSchema, TEvent, TTypestate>;
134
134
  /**
135
135
  * Clones this state machine with custom context.
136
136
  *
137
137
  * @param context Custom context (will override predefined context, not recursive)
138
138
  */
139
- withContext(context: TContext): StateNode<TContext, TStateSchema, TEvent, TTypestate>;
139
+ withContext(context: TContext | (() => TContext)): StateNode<TContext, TStateSchema, TEvent, TTypestate>;
140
140
  get context(): TContext;
141
141
  /**
142
142
  * The well-structured state node definition.
package/lib/StateNode.js CHANGED
@@ -59,7 +59,7 @@ function () {
59
59
  var _this = this;
60
60
 
61
61
  if (_context === void 0) {
62
- _context = config.context;
62
+ _context = 'context' in config ? config.context : undefined;
63
63
  }
64
64
 
65
65
  var _a;
@@ -928,7 +928,8 @@ function () {
928
928
  transitions: stateTransition.transitions,
929
929
  children: children,
930
930
  done: isDone,
931
- tags: currentState === null || currentState === void 0 ? void 0 : currentState.tags
931
+ tags: currentState === null || currentState === void 0 ? void 0 : currentState.tags,
932
+ machine: this
932
933
  });
933
934
  var didUpdateContext = currentContext !== updatedContext;
934
935
  nextState.changed = _event.name === actionTypes.update || didUpdateContext; // Dispose of penultimate histories to prevent memory leaks
package/lib/actions.d.ts CHANGED
@@ -9,7 +9,7 @@ export declare const initEvent: SCXML.Event<{
9
9
  }>;
10
10
  export declare function getActionFunction<TContext, TEvent extends EventObject>(actionType: ActionType, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | undefined;
11
11
  export declare function toActionObject<TContext, TEvent extends EventObject>(action: Action<TContext, TEvent>, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent>;
12
- export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: SingleOrArray<Action<TContext, TEvent>> | undefined, actionFunctionMap?: ActionFunctionMap<TContext, TEvent> | undefined) => ActionObject<TContext, TEvent>[];
12
+ export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: SingleOrArray<Action<TContext, TEvent>> | undefined, actionFunctionMap?: ActionFunctionMap<TContext, TEvent, ActionObject<TContext, TEvent>> | undefined) => ActionObject<TContext, TEvent>[];
13
13
  export declare function toActivityDefinition<TContext, TEvent extends EventObject>(action: string | ActivityDefinition<TContext, TEvent>): ActivityDefinition<TContext, TEvent>;
14
14
  /**
15
15
  * Raises an event. This places the event in the internal event queue, so that
package/lib/actions.js CHANGED
@@ -56,13 +56,6 @@ function toActionObject(action, actionFunctionMap) {
56
56
  }
57
57
  }
58
58
 
59
- Object.defineProperty(actionObject, 'toString', {
60
- value: function () {
61
- return actionObject.type;
62
- },
63
- enumerable: false,
64
- configurable: true
65
- });
66
59
  return actionObject;
67
60
  }
68
61
  var toActionObjects = function (action, actionFunctionMap) {
@@ -898,9 +898,13 @@ function () {
898
898
  };
899
899
 
900
900
  Interpreter.prototype.removeChild = function (childId) {
901
+ var _a;
902
+
901
903
  this.children.delete(childId);
902
- this.forwardTo.delete(childId);
903
- delete this.state.children[childId];
904
+ this.forwardTo.delete(childId); // this.state might not exist at the time this is called,
905
+ // such as when a child is added then removed while initializing the state
906
+
907
+ (_a = this.state) === null || _a === void 0 ? true : delete _a.children[childId];
904
908
  };
905
909
 
906
910
  Interpreter.prototype.stopChild = function (childId) {