xstate 4.7.5 → 4.8.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.
@@ -1,7 +1,7 @@
1
- import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, DoneEvent, Unsubscribable, MachineOptions, SCXML, EventData, Observer, Spawnable } from './types';
1
+ import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, DoneEvent, Unsubscribable, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
2
2
  import { State } from './State';
3
3
  import { Actor } from './Actor';
4
- export declare type StateListener<TContext, TEvent extends EventObject> = (state: State<TContext, TEvent>, event: TEvent) => void;
4
+ export declare type StateListener<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = any> = (state: State<TContext, TEvent, any, TTypestate>, event: TEvent) => void;
5
5
  export declare type ContextListener<TContext = DefaultContext> = (context: TContext, prevContext: TContext | undefined) => void;
6
6
  export declare type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
7
7
  export declare type Listener = () => void;
@@ -14,8 +14,8 @@ interface SpawnOptions {
14
14
  autoForward?: boolean;
15
15
  sync?: boolean;
16
16
  }
17
- export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject> implements Actor<State<TContext, TEvent>, TEvent> {
18
- machine: StateMachine<TContext, TStateSchema, TEvent>;
17
+ export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = any> implements Actor<State<TContext, TEvent>, TEvent> {
18
+ machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
19
19
  /**
20
20
  * The default interpreter options:
21
21
  *
@@ -62,7 +62,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
62
62
  * @param machine The machine to be interpreted
63
63
  * @param options Interpreter options
64
64
  */
65
- constructor(machine: StateMachine<TContext, TStateSchema, TEvent>, options?: Partial<InterpreterOptions>, sessionId?: string);
65
+ constructor(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>);
66
66
  get initialState(): State<TContext, TEvent>;
67
67
  get state(): State<TContext, TEvent>;
68
68
  static interpret: typeof interpret;
@@ -74,9 +74,9 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
74
74
  */
75
75
  execute(state: State<TContext, TEvent>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
76
76
  private update;
77
- onTransition(listener: StateListener<TContext, TEvent>): Interpreter<TContext, TStateSchema, TEvent>;
78
- subscribe(observer: Observer<State<TContext, TEvent>>): Unsubscribable;
79
- subscribe(nextListener?: (state: State<TContext, TEvent>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Unsubscribable;
77
+ onTransition(listener: StateListener<TContext, TEvent, TTypestate>): this;
78
+ subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Unsubscribable;
79
+ subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Unsubscribable;
80
80
  /**
81
81
  * Adds an event listener that is notified whenever an event is sent to the running interpreter.
82
82
  * @param listener The event listener
@@ -110,7 +110,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
110
110
  /**
111
111
  * Alias for Interpreter.prototype.start
112
112
  */
113
- init: (initialState?: string | import("./types").StateValueMap | State<TContext, TEvent, any, any> | undefined) => Interpreter<TContext, TStateSchema, TEvent>;
113
+ init: (initialState?: string | import("./types").StateValueMap | State<TContext, TEvent, any, any> | undefined) => Interpreter<TContext, TStateSchema, TEvent, any>;
114
114
  /**
115
115
  * Starts the interpreter from the given state, or the initial state.
116
116
  * @param initialState The state to start the statechart from
@@ -178,6 +178,6 @@ export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnO
178
178
  * @param machine The machine to interpret
179
179
  * @param options Interpreter options
180
180
  */
181
- export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject>(machine: StateMachine<TContext, TStateSchema, TEvent>, options?: Partial<InterpreterOptions>): Interpreter<TContext, TStateSchema, TEvent>;
181
+ export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = any>(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
182
182
  export {};
183
183
  //# sourceMappingURL=interpreter.d.ts.map
@@ -82,7 +82,7 @@ var Interpreter = /** @class */ (function () {
82
82
  * @param machine The machine to be interpreted
83
83
  * @param options Interpreter options
84
84
  */
85
- function Interpreter(machine, options, sessionId) {
85
+ function Interpreter(machine, options) {
86
86
  var _this = this;
87
87
  if (options === void 0) { options = Interpreter.defaultOptions; }
88
88
  this.machine = machine;
@@ -183,8 +183,7 @@ var Interpreter = /** @class */ (function () {
183
183
  this.scheduler = new scheduler_1.Scheduler({
184
184
  deferEvents: this.options.deferEvents
185
185
  });
186
- this.sessionId =
187
- sessionId !== undefined ? sessionId : registry_1.registry.register(this);
186
+ this.sessionId = registry_1.registry.bookId();
188
187
  }
189
188
  Object.defineProperty(Interpreter.prototype, "initialState", {
190
189
  get: function () {
@@ -421,6 +420,7 @@ var Interpreter = /** @class */ (function () {
421
420
  // Do not restart the service if it is already started
422
421
  return this;
423
422
  }
423
+ registry_1.registry.register(this.sessionId, this);
424
424
  this.initialized = true;
425
425
  this._status = InterpreterStatus.Running;
426
426
  var resolvedState = initialState === undefined
@@ -522,6 +522,7 @@ var Interpreter = /** @class */ (function () {
522
522
  this.scheduler.clear();
523
523
  this.initialized = false;
524
524
  this._status = InterpreterStatus.Stopped;
525
+ registry_1.registry.free(this.sessionId);
525
526
  return this;
526
527
  };
527
528
  Interpreter.prototype.batch = function (events) {
package/lib/json.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { StateNode, ActionObject, Guard, InvokeDefinition } from './';
2
+ interface JSONFunction {
3
+ $function: string;
4
+ }
5
+ export declare function stringifyFunction(fn: Function): JSONFunction;
6
+ interface TransitionConfig {
7
+ target: string[];
8
+ source: string;
9
+ actions: Array<ActionObject<any, any>>;
10
+ cond: Guard<any, any> | undefined;
11
+ eventType: string;
12
+ }
13
+ interface StateNodeConfig {
14
+ type: StateNode['type'];
15
+ id: string;
16
+ key: string;
17
+ initial?: string;
18
+ entry: Array<ActionObject<any, any>>;
19
+ exit: Array<ActionObject<any, any>>;
20
+ on: {
21
+ [key: string]: TransitionConfig[];
22
+ };
23
+ invoke: Array<InvokeDefinition<any, any>>;
24
+ states: Record<string, StateNodeConfig>;
25
+ }
26
+ export declare function machineToJSON(stateNode: StateNode): StateNodeConfig;
27
+ export declare function stringify(machine: StateNode): string;
28
+ export declare function parse(machineString: string): StateNodeConfig;
29
+ export declare function jsonify<T extends Record<string, any>>(value: T): T;
30
+ export {};
31
+ //# sourceMappingURL=json.d.ts.map
package/lib/json.js ADDED
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var utils_1 = require("./utils");
4
+ // tslint:disable-next-line:ban-types
5
+ function stringifyFunction(fn) {
6
+ return {
7
+ $function: fn.toString()
8
+ };
9
+ }
10
+ exports.stringifyFunction = stringifyFunction;
11
+ function getStateNodeId(stateNode) {
12
+ return "#" + stateNode.id;
13
+ }
14
+ // derive config from machine
15
+ function machineToJSON(stateNode) {
16
+ var config = {
17
+ type: stateNode.type,
18
+ initial: stateNode.initial === undefined ? undefined : String(stateNode.initial),
19
+ id: stateNode.id,
20
+ key: stateNode.key,
21
+ entry: stateNode.onEntry,
22
+ exit: stateNode.onExit,
23
+ on: utils_1.mapValues(stateNode.on, function (transition) {
24
+ return transition.map(function (t) {
25
+ return {
26
+ target: t.target ? t.target.map(getStateNodeId) : [],
27
+ source: getStateNodeId(t.source),
28
+ actions: t.actions,
29
+ cond: t.cond,
30
+ eventType: t.eventType
31
+ };
32
+ });
33
+ }),
34
+ invoke: stateNode.invoke,
35
+ states: {}
36
+ };
37
+ Object.values(stateNode.states).forEach(function (sn) {
38
+ config.states[sn.key] = machineToJSON(sn);
39
+ });
40
+ return config;
41
+ }
42
+ exports.machineToJSON = machineToJSON;
43
+ function stringify(machine) {
44
+ return JSON.stringify(machineToJSON(machine), function (_, value) {
45
+ if (utils_1.isFunction(value)) {
46
+ return { $function: value.toString() };
47
+ }
48
+ return value;
49
+ });
50
+ }
51
+ exports.stringify = stringify;
52
+ function parse(machineString) {
53
+ var config = JSON.parse(machineString, function (_, value) {
54
+ if (typeof value === 'object' && '$function' in value) {
55
+ return new Function(value.value);
56
+ }
57
+ return value;
58
+ });
59
+ return config;
60
+ }
61
+ exports.parse = parse;
62
+ function jsonify(value) {
63
+ Object.defineProperty(value, 'toJSON', {
64
+ value: function () {
65
+ return utils_1.mapValues(value, function (subValue) {
66
+ if (utils_1.isFunction(subValue)) {
67
+ return stringifyFunction(subValue);
68
+ }
69
+ else if (typeof subValue === 'object' && !Array.isArray(subValue)) {
70
+ // mostly for assignments
71
+ return utils_1.mapValues(subValue, function (subSubValue) {
72
+ if (utils_1.isFunction(subSubValue)) {
73
+ return stringifyFunction(subSubValue);
74
+ }
75
+ return subSubValue;
76
+ });
77
+ }
78
+ return subValue;
79
+ });
80
+ }
81
+ });
82
+ return value;
83
+ }
84
+ exports.jsonify = jsonify;
package/lib/registry.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Actor } from './Actor';
2
2
  export interface Registry {
3
- register(actor: Actor): string;
3
+ bookId(): string;
4
+ register(id: string, actor: Actor): string;
4
5
  get(id: string): Actor | undefined;
5
- lookup(actor: Actor): string | undefined;
6
+ free(id: string): void;
6
7
  }
7
8
  export declare const registry: Registry;
8
9
  //# sourceMappingURL=registry.d.ts.map
package/lib/registry.js CHANGED
@@ -1,19 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var children = new Map();
4
- var idMap = new Map();
5
4
  var sessionIdIndex = 0;
6
5
  exports.registry = {
7
- register: function (actor) {
8
- var id = "x:" + sessionIdIndex++;
6
+ bookId: function () {
7
+ return "x:" + sessionIdIndex++;
8
+ },
9
+ register: function (id, actor) {
9
10
  children.set(id, actor);
10
- idMap.set(actor, id);
11
11
  return id;
12
12
  },
13
13
  get: function (id) {
14
14
  return children.get(id);
15
15
  },
16
- lookup: function (actorRef) {
17
- return idMap.get(actorRef);
16
+ free: function (id) {
17
+ children.delete(id);
18
18
  }
19
19
  };
package/lib/types.d.ts CHANGED
@@ -50,7 +50,7 @@ export interface AssignMeta<TContext, TEvent extends EventObject> {
50
50
  _event: SCXML.Event<TEvent>;
51
51
  }
52
52
  export declare type ActionFunction<TContext, TEvent extends EventObject> = (context: TContext, event: TEvent, meta: ActionMeta<TContext, TEvent>) => any | void;
53
- export declare type Action<TContext, TEvent extends EventObject> = ActionType | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | AssignAction<Required<TContext>, TEvent> | SendAction<TContext, TEvent> | RaiseAction<TEvent>;
53
+ export declare type Action<TContext, TEvent extends EventObject> = ActionType | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | AssignAction<Required<TContext>, TEvent> | SendAction<TContext, TEvent> | RaiseAction<AnyEventObject>;
54
54
  export declare type Actions<TContext, TEvent extends EventObject> = SingleOrArray<Action<TContext, TEvent>>;
55
55
  export declare type StateKey = string | State<any>;
56
56
  export interface StateValueMap {
@@ -124,7 +124,7 @@ export declare type InvokeCallback = (callback: Sender<any>, onReceive: Receiver
124
124
  * @param context The current machine `context`
125
125
  * @param event The event that invoked the service
126
126
  */
127
- export declare type InvokeCreator<TContext, TFinalContext = any> = (context: TContext, event: AnyEventObject) => PromiseLike<TFinalContext> | StateMachine<TFinalContext, any, any> | Subscribable<any> | InvokeCallback;
127
+ export declare type InvokeCreator<TContext, TEvent = AnyEventObject, TFinalContext = any> = (context: TContext, event: TEvent) => PromiseLike<TFinalContext> | StateMachine<TFinalContext, any, any> | Subscribable<any> | InvokeCallback;
128
128
  export interface InvokeDefinition<TContext, TEvent extends EventObject> extends ActivityDefinition<TContext, TEvent> {
129
129
  /**
130
130
  * The source of the machine to be invoked, or the machine itself.
@@ -196,7 +196,7 @@ export declare type InvokeConfig<TContext, TEvent extends EventObject> = {
196
196
  /**
197
197
  * The source of the machine to be invoked, or the machine itself.
198
198
  */
199
- src: string | StateMachine<any, any, any> | InvokeCreator<any, any>;
199
+ src: string | StateMachine<any, any, any> | InvokeCreator<TContext, TEvent, any>;
200
200
  /**
201
201
  * If `true`, events sent to the parent service will be forwarded to the invoked service.
202
202
  *
@@ -345,14 +345,15 @@ export interface StateNodeDefinition<TContext, TStateSchema extends StateSchema,
345
345
  id: string;
346
346
  version: string | undefined;
347
347
  key: string;
348
+ context: TContext;
348
349
  type: 'atomic' | 'compound' | 'parallel' | 'final' | 'history';
349
350
  initial: StateNodeConfig<TContext, TStateSchema, TEvent>['initial'];
350
351
  history: boolean | 'shallow' | 'deep' | undefined;
351
352
  states: StatesDefinition<TContext, TStateSchema, TEvent>;
352
353
  on: TransitionDefinitionMap<TContext, TEvent>;
353
354
  transitions: Array<TransitionDefinition<TContext, TEvent>>;
354
- onEntry: Array<ActionObject<TContext, TEvent>>;
355
- onExit: Array<ActionObject<TContext, TEvent>>;
355
+ entry: Array<ActionObject<TContext, TEvent>>;
356
+ exit: Array<ActionObject<TContext, TEvent>>;
356
357
  activities: Array<ActivityDefinition<TContext, TEvent>>;
357
358
  meta: any;
358
359
  order: number;
@@ -422,7 +423,7 @@ export interface HistoryStateNode<TContext> extends StateNode<TContext> {
422
423
  history: 'shallow' | 'deep';
423
424
  target: StateValue | undefined;
424
425
  }
425
- export interface StateMachine<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TState extends Typestate<TContext> = Typestate<TContext>> extends StateNode<TContext, TStateSchema, TEvent, TState> {
426
+ export interface StateMachine<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TTypestate extends Typestate<TContext> = any> extends StateNode<TContext, TStateSchema, TEvent, TTypestate> {
426
427
  id: string;
427
428
  states: StateNode<TContext, TStateSchema, TEvent>['states'];
428
429
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xstate",
3
- "version": "4.7.5",
3
+ "version": "4.8.0",
4
4
  "description": "Finite State Machines and Statecharts for the Modern Web.",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -66,7 +66,7 @@
66
66
  "rxjs": "^6.5.1",
67
67
  "ts-jest": "^24.1.9",
68
68
  "tslib": "^1.10.0",
69
- "typescript": "^3.7.2",
69
+ "typescript": "^3.7.5",
70
70
  "xml-js": "^1.6.11"
71
71
  }
72
72
  }