xstate 4.20.1 → 4.23.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.
Files changed (92) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/dist/xstate.interpreter.js +1 -1
  3. package/dist/xstate.js +1 -1
  4. package/dist/xstate.web.js +2 -2
  5. package/es/Actor.js +17 -5
  6. package/es/Machine.d.ts +6 -3
  7. package/es/Machine.js +3 -6
  8. package/es/State.js +1 -3
  9. package/es/StateNode.d.ts +4 -3
  10. package/es/StateNode.js +34 -20
  11. package/es/_virtual/_tslib.js +59 -73
  12. package/es/actionTypes.js +3 -2
  13. package/es/actions.d.ts +1 -1
  14. package/es/actions.js +51 -37
  15. package/es/behaviors.d.ts +37 -0
  16. package/es/behaviors.js +65 -0
  17. package/es/constants.js +2 -1
  18. package/es/devTools.js +1 -1
  19. package/es/environment.js +2 -1
  20. package/es/index.js +3 -1
  21. package/es/interpreter.d.ts +10 -3
  22. package/es/interpreter.js +44 -22
  23. package/es/invokeUtils.js +4 -3
  24. package/es/mapState.js +1 -1
  25. package/es/match.js +1 -1
  26. package/es/model.d.ts +2 -36
  27. package/es/model.types.d.ts +37 -0
  28. package/es/registry.js +2 -1
  29. package/es/scheduler.js +2 -1
  30. package/es/schema.js +1 -1
  31. package/es/serviceScope.js +1 -3
  32. package/es/stateUtils.js +1 -9
  33. package/es/types.d.ts +26 -5
  34. package/es/types.js +1 -1
  35. package/es/utils.d.ts +3 -2
  36. package/es/utils.js +4 -40
  37. package/lib/Actor.d.ts +25 -25
  38. package/lib/Actor.js +85 -66
  39. package/lib/Machine.d.ts +17 -14
  40. package/lib/Machine.js +14 -14
  41. package/lib/SimulatedClock.d.ts +16 -16
  42. package/lib/State.d.ts +108 -108
  43. package/lib/State.js +246 -236
  44. package/lib/StateNode.d.ts +279 -278
  45. package/lib/StateNode.js +1535 -1339
  46. package/lib/_virtual/_tslib.js +81 -0
  47. package/lib/actionTypes.d.ts +19 -19
  48. package/lib/actionTypes.js +43 -23
  49. package/lib/actions.d.ts +138 -138
  50. package/lib/actions.js +465 -387
  51. package/lib/behaviors.d.ts +37 -0
  52. package/lib/behaviors.js +69 -0
  53. package/lib/constants.d.ts +5 -5
  54. package/lib/constants.js +13 -7
  55. package/lib/devTools.d.ts +15 -15
  56. package/lib/devTools.js +37 -26
  57. package/lib/each.d.ts +3 -3
  58. package/lib/environment.d.ts +1 -1
  59. package/lib/environment.js +7 -4
  60. package/lib/index.d.ts +30 -30
  61. package/lib/index.js +67 -57
  62. package/lib/interpreter.d.ts +205 -198
  63. package/lib/interpreter.js +1306 -1054
  64. package/lib/invoke.d.ts +10 -10
  65. package/lib/invokeUtils.d.ts +6 -6
  66. package/lib/invokeUtils.js +40 -37
  67. package/lib/json.d.ts +30 -30
  68. package/lib/mapState.d.ts +3 -3
  69. package/lib/mapState.js +31 -32
  70. package/lib/match.d.ts +8 -8
  71. package/lib/match.js +33 -47
  72. package/lib/model.d.ts +4 -38
  73. package/lib/model.js +5 -1
  74. package/lib/model.types.d.ts +37 -0
  75. package/lib/model.types.js +2 -0
  76. package/lib/patterns.d.ts +13 -13
  77. package/lib/registry.d.ts +8 -8
  78. package/lib/registry.js +21 -18
  79. package/lib/scheduler.d.ts +16 -16
  80. package/lib/scheduler.js +79 -70
  81. package/lib/schema.d.ts +1 -1
  82. package/lib/schema.js +6 -4
  83. package/lib/scxml.d.ts +5 -5
  84. package/lib/serviceScope.d.ts +3 -3
  85. package/lib/serviceScope.js +16 -12
  86. package/lib/stateUtils.d.ts +14 -14
  87. package/lib/stateUtils.js +231 -199
  88. package/lib/types.d.ts +928 -907
  89. package/lib/types.js +29 -29
  90. package/lib/utils.d.ts +68 -67
  91. package/lib/utils.js +530 -529
  92. package/package.json +6 -6
package/lib/invoke.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- export interface InvokedPromiseOptions {
2
- id?: string;
3
- }
4
- export interface PromiseMachineSchema {
5
- states: {
6
- pending: {};
7
- resolved: {};
8
- rejected: {};
9
- };
10
- }
1
+ export interface InvokedPromiseOptions {
2
+ id?: string;
3
+ }
4
+ export interface PromiseMachineSchema {
5
+ states: {
6
+ pending: {};
7
+ resolved: {};
8
+ rejected: {};
9
+ };
10
+ }
11
11
  //# sourceMappingURL=invoke.d.ts.map
@@ -1,7 +1,7 @@
1
- import { EventObject, InvokeConfig, InvokeDefinition, InvokeSourceDefinition } from './types';
2
- export declare function toInvokeSource(src: string | InvokeSourceDefinition): InvokeSourceDefinition;
3
- export declare function toInvokeDefinition<TContext, TEvent extends EventObject>(invokeConfig: InvokeConfig<TContext, TEvent> & {
4
- src: string | InvokeSourceDefinition;
5
- id: string;
6
- }): InvokeDefinition<TContext, TEvent>;
1
+ import { EventObject, InvokeConfig, InvokeDefinition, InvokeSourceDefinition } from './types';
2
+ export declare function toInvokeSource(src: string | InvokeSourceDefinition): InvokeSourceDefinition;
3
+ export declare function toInvokeDefinition<TContext, TEvent extends EventObject>(invokeConfig: InvokeConfig<TContext, TEvent> & {
4
+ src: string | InvokeSourceDefinition;
5
+ id: string;
6
+ }): InvokeDefinition<TContext, TEvent>;
7
7
  //# sourceMappingURL=invokeUtils.d.ts.map
@@ -1,42 +1,45 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __rest = (this && this.__rest) || function (s, e) {
14
- var t = {};
15
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
- t[p] = s[p];
17
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
- t[p[i]] = s[p[i]];
21
- }
22
- return t;
23
- };
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.toInvokeDefinition = exports.toInvokeSource = void 0;
26
- var actions_1 = require("./actions");
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+ require('./environment.js');
7
+ require('./utils.js');
8
+ require('./types.js');
9
+ var actionTypes = require('./actionTypes.js');
10
+
27
11
  function toInvokeSource(src) {
28
- if (typeof src === 'string') {
29
- var simpleSrc = { type: src };
30
- simpleSrc.toString = function () { return src; }; // v4 compat - TODO: remove in v5
31
- return simpleSrc;
32
- }
33
- return src;
12
+ if (typeof src === 'string') {
13
+ var simpleSrc = {
14
+ type: src
15
+ };
16
+
17
+ simpleSrc.toString = function () {
18
+ return src;
19
+ }; // v4 compat - TODO: remove in v5
20
+
21
+
22
+ return simpleSrc;
23
+ }
24
+
25
+ return src;
34
26
  }
35
- exports.toInvokeSource = toInvokeSource;
36
27
  function toInvokeDefinition(invokeConfig) {
37
- return __assign(__assign({ type: actions_1.actionTypes.invoke }, invokeConfig), { toJSON: function () {
38
- var onDone = invokeConfig.onDone, onError = invokeConfig.onError, invokeDef = __rest(invokeConfig, ["onDone", "onError"]);
39
- return __assign(__assign({}, invokeDef), { type: actions_1.actionTypes.invoke, src: toInvokeSource(invokeConfig.src) });
40
- } });
28
+ return _tslib.__assign(_tslib.__assign({
29
+ type: actionTypes.invoke
30
+ }, invokeConfig), {
31
+ toJSON: function () {
32
+ var onDone = invokeConfig.onDone,
33
+ onError = invokeConfig.onError,
34
+ invokeDef = _tslib.__rest(invokeConfig, ["onDone", "onError"]);
35
+
36
+ return _tslib.__assign(_tslib.__assign({}, invokeDef), {
37
+ type: actionTypes.invoke,
38
+ src: toInvokeSource(invokeConfig.src)
39
+ });
40
+ }
41
+ });
41
42
  }
43
+
42
44
  exports.toInvokeDefinition = toInvokeDefinition;
45
+ exports.toInvokeSource = toInvokeSource;
package/lib/json.d.ts CHANGED
@@ -1,31 +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 {};
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
31
  //# sourceMappingURL=json.d.ts.map
package/lib/mapState.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare function mapState(stateMap: {
2
- [stateId: string]: any;
3
- }, stateId: string): any;
1
+ export declare function mapState(stateMap: {
2
+ [stateId: string]: any;
3
+ }, stateId: string): any;
4
4
  //# sourceMappingURL=mapState.d.ts.map
package/lib/mapState.js CHANGED
@@ -1,37 +1,36 @@
1
- "use strict";
2
- var __values = (this && this.__values) || function(o) {
3
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
- if (m) return m.call(o);
5
- if (o && typeof o.length === "number") return {
6
- next: function () {
7
- if (o && i >= o.length) o = void 0;
8
- return { value: o && o[i++], done: !o };
9
- }
10
- };
11
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.mapState = void 0;
15
- var utils_1 = require("./utils");
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+ var utils = require('./utils.js');
7
+
16
8
  function mapState(stateMap, stateId) {
17
- var e_1, _a;
18
- var foundStateId;
19
- try {
20
- for (var _b = __values(utils_1.keys(stateMap)), _c = _b.next(); !_c.done; _c = _b.next()) {
21
- var mappedStateId = _c.value;
22
- if (utils_1.matchesState(mappedStateId, stateId) &&
23
- (!foundStateId || stateId.length > foundStateId.length)) {
24
- foundStateId = mappedStateId;
25
- }
26
- }
9
+ var e_1, _a;
10
+
11
+ var foundStateId;
12
+
13
+ try {
14
+ for (var _b = _tslib.__values(utils.keys(stateMap)), _c = _b.next(); !_c.done; _c = _b.next()) {
15
+ var mappedStateId = _c.value;
16
+
17
+ if (utils.matchesState(mappedStateId, stateId) && (!foundStateId || stateId.length > foundStateId.length)) {
18
+ foundStateId = mappedStateId;
19
+ }
27
20
  }
28
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
29
- finally {
30
- try {
31
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
32
- }
33
- finally { if (e_1) throw e_1.error; }
21
+ } catch (e_1_1) {
22
+ e_1 = {
23
+ error: e_1_1
24
+ };
25
+ } finally {
26
+ try {
27
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
28
+ } finally {
29
+ if (e_1) throw e_1.error;
34
30
  }
35
- return stateMap[foundStateId];
31
+ }
32
+
33
+ return stateMap[foundStateId];
36
34
  }
35
+
37
36
  exports.mapState = mapState;
package/lib/match.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { State } from './State';
2
- import { StateValue, EventObject } from './types';
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> = [
5
- StateValue,
6
- ValueFromStateGetter<T, TContext, TEvent>
7
- ];
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;
1
+ import { State } from './State';
2
+ import { StateValue, EventObject } from './types';
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> = [
5
+ StateValue,
6
+ ValueFromStateGetter<T, TContext, TEvent>
7
+ ];
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;
9
9
  //# sourceMappingURL=match.d.ts.map
package/lib/match.js CHANGED
@@ -1,52 +1,38 @@
1
- "use strict";
2
- var __values = (this && this.__values) || function(o) {
3
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
- if (m) return m.call(o);
5
- if (o && typeof o.length === "number") return {
6
- next: function () {
7
- if (o && i >= o.length) o = void 0;
8
- return { value: o && o[i++], done: !o };
9
- }
10
- };
11
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.matchState = void 0;
31
- var State_1 = require("./State");
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+ var State = require('./State.js');
7
+
32
8
  function matchState(state, patterns, defaultValue) {
33
- var e_1, _a;
34
- var resolvedState = State_1.State.from(state, state instanceof State_1.State ? state.context : undefined);
35
- try {
36
- for (var patterns_1 = __values(patterns), patterns_1_1 = patterns_1.next(); !patterns_1_1.done; patterns_1_1 = patterns_1.next()) {
37
- var _b = __read(patterns_1_1.value, 2), stateValue = _b[0], getValue = _b[1];
38
- if (resolvedState.matches(stateValue)) {
39
- return getValue(resolvedState);
40
- }
41
- }
9
+ var e_1, _a;
10
+
11
+ var resolvedState = State.State.from(state, state instanceof State.State ? state.context : undefined);
12
+
13
+ try {
14
+ for (var patterns_1 = _tslib.__values(patterns), patterns_1_1 = patterns_1.next(); !patterns_1_1.done; patterns_1_1 = patterns_1.next()) {
15
+ var _b = _tslib.__read(patterns_1_1.value, 2),
16
+ stateValue = _b[0],
17
+ getValue = _b[1];
18
+
19
+ if (resolvedState.matches(stateValue)) {
20
+ return getValue(resolvedState);
21
+ }
42
22
  }
43
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
44
- finally {
45
- try {
46
- if (patterns_1_1 && !patterns_1_1.done && (_a = patterns_1.return)) _a.call(patterns_1);
47
- }
48
- finally { if (e_1) throw e_1.error; }
23
+ } catch (e_1_1) {
24
+ e_1 = {
25
+ error: e_1_1
26
+ };
27
+ } finally {
28
+ try {
29
+ if (patterns_1_1 && !patterns_1_1.done && (_a = patterns_1.return)) _a.call(patterns_1);
30
+ } finally {
31
+ if (e_1) throw e_1.error;
49
32
  }
50
- return defaultValue(resolvedState);
33
+ }
34
+
35
+ return defaultValue(resolvedState);
51
36
  }
37
+
52
38
  exports.matchState = matchState;
package/lib/model.d.ts CHANGED
@@ -1,39 +1,5 @@
1
- import type { AssignAction, Assigner, PropertyAssigner, ExtractEvent, EventObject } from './types';
2
- declare type AnyFunction = (...args: any[]) => any;
3
- declare type Cast<A1 extends any, A2 extends any> = A1 extends A2 ? A1 : A2;
4
- declare type Compute<A extends any> = {
5
- [K in keyof A]: A[K];
6
- } & unknown;
7
- declare type Prop<T, K> = K extends keyof T ? T[K] : never;
8
- export interface Model<TContext, TEvent extends EventObject, TModelCreators = void> {
9
- initialContext: TContext;
10
- assign: <TEventType extends TEvent['type'] = TEvent['type']>(assigner: Assigner<TContext, ExtractEvent<TEvent, TEventType>> | PropertyAssigner<TContext, ExtractEvent<TEvent, TEventType>>, eventType?: TEventType) => AssignAction<TContext, ExtractEvent<TEvent, TEventType>>;
11
- events: Prop<TModelCreators, 'events'>;
12
- reset: () => AssignAction<TContext, any>;
13
- }
14
- export declare type ModelContextFrom<TModel extends Model<any, any, any>> = TModel extends Model<infer TContext, any, any> ? TContext : never;
15
- export declare type ModelEventsFrom<TModel extends Model<any, any, any>> = TModel extends Model<any, infer TEvent, any> ? TEvent : never;
16
- declare type EventCreator<Self extends AnyFunction, Return = ReturnType<Self>> = Return extends object ? Return extends {
17
- type: any;
18
- } ? "An event creator can't return an object with a type property" : Self : 'An event creator must return an object';
19
- declare type EventCreators<Self> = {
20
- [K in keyof Self]: Self[K] extends AnyFunction ? EventCreator<Self[K]> : 'An event creator must be a function';
21
- };
22
- declare type ModelCreators<Self> = {
23
- events: EventCreators<Prop<Self, 'events'>>;
24
- };
25
- declare type FinalEventCreators<Self> = {
26
- [K in keyof Self]: Self[K] extends AnyFunction ? (...args: Parameters<Self[K]>) => Compute<ReturnType<Self[K]> & {
27
- type: K;
28
- }> : never;
29
- };
30
- declare type FinalModelCreators<Self> = {
31
- events: FinalEventCreators<Prop<Self, 'events'>>;
32
- };
33
- declare type EventFromEventCreators<EventCreators> = {
34
- [K in keyof EventCreators]: EventCreators[K] extends AnyFunction ? ReturnType<EventCreators[K]> : never;
35
- }[keyof EventCreators];
36
- export declare function createModel<TContext, TEvent extends EventObject>(initialContext: TContext): Model<TContext, TEvent, void>;
37
- export declare function createModel<TContext, TModelCreators extends ModelCreators<TModelCreators>, TFinalModelCreators = FinalModelCreators<TModelCreators>>(initialContext: TContext, creators: TModelCreators): Model<TContext, Cast<EventFromEventCreators<Prop<TFinalModelCreators, 'events'>>, EventObject>, TFinalModelCreators>;
38
- export {};
1
+ import type { EventObject } from './types';
2
+ import { Cast, EventFromEventCreators, FinalModelCreators, Model, ModelCreators, Prop } from './model.types';
3
+ export declare function createModel<TContext, TEvent extends EventObject>(initialContext: TContext): Model<TContext, TEvent, void>;
4
+ export declare function createModel<TContext, TModelCreators extends ModelCreators<TModelCreators>, TFinalModelCreators = FinalModelCreators<TModelCreators>>(initialContext: TContext, creators: TModelCreators): Model<TContext, Cast<EventFromEventCreators<Prop<TFinalModelCreators, 'events'>>, EventObject>, TFinalModelCreators>;
39
5
  //# sourceMappingURL=model.d.ts.map
package/lib/model.js CHANGED
@@ -34,6 +34,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.createModel = void 0;
36
36
  var actions_1 = require("./actions");
37
+ var Machine_1 = require("./Machine");
37
38
  var utils_1 = require("./utils");
38
39
  function createModel(initialContext, creators) {
39
40
  var eventCreators = creators === null || creators === void 0 ? void 0 : creators.events;
@@ -49,7 +50,10 @@ function createModel(initialContext, creators) {
49
50
  return (__assign(__assign({}, fn.apply(void 0, __spreadArray([], __read(args)))), { type: eventType }));
50
51
  }; })
51
52
  : undefined),
52
- reset: function () { return actions_1.assign(initialContext); }
53
+ reset: function () { return actions_1.assign(initialContext); },
54
+ createMachine: function (config, implementations) {
55
+ return Machine_1.createMachine('context' in config ? config : __assign(__assign({}, config), { context: initialContext }), implementations);
56
+ }
53
57
  };
54
58
  return model;
55
59
  }
@@ -0,0 +1,37 @@
1
+ import { EventObject, Assigner, ExtractEvent, PropertyAssigner, AssignAction, MachineConfig, MachineOptions, StateMachine } from './types';
2
+ export declare type AnyFunction = (...args: any[]) => any;
3
+ export declare type Cast<A1 extends any, A2 extends any> = A1 extends A2 ? A1 : A2;
4
+ export declare type Compute<A extends any> = {
5
+ [K in keyof A]: A[K];
6
+ } & unknown;
7
+ export declare type Prop<T, K> = K extends keyof T ? T[K] : never;
8
+ export interface Model<TContext, TEvent extends EventObject, TModelCreators = void> {
9
+ initialContext: TContext;
10
+ assign: <TEventType extends TEvent['type'] = TEvent['type']>(assigner: Assigner<TContext, ExtractEvent<TEvent, TEventType>> | PropertyAssigner<TContext, ExtractEvent<TEvent, TEventType>>, eventType?: TEventType) => AssignAction<TContext, ExtractEvent<TEvent, TEventType>>;
11
+ events: Prop<TModelCreators, 'events'>;
12
+ reset: () => AssignAction<TContext, any>;
13
+ createMachine: (config: MachineConfig<TContext, any, TEvent>, implementations?: Partial<MachineOptions<TContext, TEvent>>) => StateMachine<TContext, any, TEvent>;
14
+ }
15
+ export declare type ModelContextFrom<TModel extends Model<any, any, any>> = TModel extends Model<infer TContext, any, any> ? TContext : never;
16
+ export declare type ModelEventsFrom<TModel extends Model<any, any, any>> = TModel extends Model<any, infer TEvent, any> ? TEvent : never;
17
+ export declare type EventCreator<Self extends AnyFunction, Return = ReturnType<Self>> = Return extends object ? Return extends {
18
+ type: any;
19
+ } ? "An event creator can't return an object with a type property" : Self : 'An event creator must return an object';
20
+ export declare type EventCreators<Self> = {
21
+ [K in keyof Self]: Self[K] extends AnyFunction ? EventCreator<Self[K]> : 'An event creator must be a function';
22
+ };
23
+ export declare type ModelCreators<Self> = {
24
+ events: EventCreators<Prop<Self, 'events'>>;
25
+ };
26
+ export declare type FinalEventCreators<Self> = {
27
+ [K in keyof Self]: Self[K] extends AnyFunction ? (...args: Parameters<Self[K]>) => Compute<ReturnType<Self[K]> & {
28
+ type: K;
29
+ }> : never;
30
+ };
31
+ export declare type FinalModelCreators<Self> = {
32
+ events: FinalEventCreators<Prop<Self, 'events'>>;
33
+ };
34
+ export declare type EventFromEventCreators<EventCreators> = {
35
+ [K in keyof EventCreators]: EventCreators[K] extends AnyFunction ? ReturnType<EventCreators[K]> : never;
36
+ }[keyof EventCreators];
37
+ //# sourceMappingURL=model.types.d.ts.map
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/patterns.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { AtomicStateNodeConfig, StatesConfig, Event, EventObject, StateSchema } from './types';
2
- export declare function toggle<TEventType extends string = string>(onState: string, offState: string, eventType: TEventType): Record<string, AtomicStateNodeConfig<any, {
3
- type: TEventType;
4
- }>>;
5
- interface SequencePatternOptions<TEvent extends EventObject> {
6
- nextEvent: Event<TEvent> | undefined;
7
- prevEvent: Event<TEvent> | undefined;
8
- }
9
- export declare function sequence<TStateSchema extends StateSchema, TEvent extends EventObject>(items: Array<keyof TStateSchema['states']>, options?: Partial<SequencePatternOptions<TEvent>>): {
10
- initial: keyof TStateSchema['states'];
11
- states: StatesConfig<any, TStateSchema, TEvent>;
12
- };
13
- export {};
1
+ import { AtomicStateNodeConfig, StatesConfig, Event, EventObject, StateSchema } from './types';
2
+ export declare function toggle<TEventType extends string = string>(onState: string, offState: string, eventType: TEventType): Record<string, AtomicStateNodeConfig<any, {
3
+ type: TEventType;
4
+ }>>;
5
+ interface SequencePatternOptions<TEvent extends EventObject> {
6
+ nextEvent: Event<TEvent> | undefined;
7
+ prevEvent: Event<TEvent> | undefined;
8
+ }
9
+ export declare function sequence<TStateSchema extends StateSchema, TEvent extends EventObject>(items: Array<keyof TStateSchema['states']>, options?: Partial<SequencePatternOptions<TEvent>>): {
10
+ initial: keyof TStateSchema['states'];
11
+ states: StatesConfig<any, TStateSchema, TEvent>;
12
+ };
13
+ export {};
14
14
  //# sourceMappingURL=patterns.d.ts.map
package/lib/registry.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Actor } from './Actor';
2
- export interface Registry {
3
- bookId(): string;
4
- register(id: string, actor: Actor): string;
5
- get(id: string): Actor | undefined;
6
- free(id: string): void;
7
- }
8
- export declare const registry: Registry;
1
+ import { Actor } from './Actor';
2
+ export interface Registry {
3
+ bookId(): string;
4
+ register(id: string, actor: Actor): string;
5
+ get(id: string): Actor | undefined;
6
+ free(id: string): void;
7
+ }
8
+ export declare const registry: Registry;
9
9
  //# sourceMappingURL=registry.d.ts.map
package/lib/registry.js CHANGED
@@ -1,20 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.registry = void 0;
4
- var children = new Map();
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var children = /*#__PURE__*/new Map();
5
6
  var sessionIdIndex = 0;
6
- exports.registry = {
7
- bookId: function () {
8
- return "x:" + sessionIdIndex++;
9
- },
10
- register: function (id, actor) {
11
- children.set(id, actor);
12
- return id;
13
- },
14
- get: function (id) {
15
- return children.get(id);
16
- },
17
- free: function (id) {
18
- children.delete(id);
19
- }
7
+ var registry = {
8
+ bookId: function () {
9
+ return "x:" + sessionIdIndex++;
10
+ },
11
+ register: function (id, actor) {
12
+ children.set(id, actor);
13
+ return id;
14
+ },
15
+ get: function (id) {
16
+ return children.get(id);
17
+ },
18
+ free: function (id) {
19
+ children.delete(id);
20
+ }
20
21
  };
22
+
23
+ exports.registry = registry;
@@ -1,17 +1,17 @@
1
- interface SchedulerOptions {
2
- deferEvents: boolean;
3
- }
4
- export declare class Scheduler {
5
- private processingEvent;
6
- private queue;
7
- private initialized;
8
- private options;
9
- constructor(options?: Partial<SchedulerOptions>);
10
- initialize(callback?: () => void): void;
11
- schedule(task: () => void): void;
12
- clear(): void;
13
- private flushEvents;
14
- private process;
15
- }
16
- export {};
1
+ interface SchedulerOptions {
2
+ deferEvents: boolean;
3
+ }
4
+ export declare class Scheduler {
5
+ private processingEvent;
6
+ private queue;
7
+ private initialized;
8
+ private options;
9
+ constructor(options?: Partial<SchedulerOptions>);
10
+ initialize(callback?: () => void): void;
11
+ schedule(task: () => void): void;
12
+ clear(): void;
13
+ private flushEvents;
14
+ private process;
15
+ }
16
+ export {};
17
17
  //# sourceMappingURL=scheduler.d.ts.map