xstate 4.20.0 → 4.23.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/CHANGELOG.md +205 -0
- package/dist/xstate.interpreter.js +1 -1
- package/dist/xstate.js +1 -1
- package/dist/xstate.web.js +2 -2
- package/es/Actor.d.ts +6 -5
- package/es/Actor.js +22 -7
- package/es/Machine.d.ts +10 -5
- package/es/Machine.js +3 -6
- package/es/State.js +3 -5
- package/es/StateNode.d.ts +4 -3
- package/es/StateNode.js +34 -28
- package/es/_virtual/_tslib.js +59 -73
- package/es/actionTypes.js +3 -2
- package/es/actions.d.ts +1 -1
- package/es/actions.js +51 -37
- package/es/behaviors.d.ts +37 -0
- package/es/behaviors.js +65 -0
- package/es/constants.js +2 -1
- package/es/devTools.js +1 -1
- package/es/environment.js +2 -1
- package/es/index.js +3 -1
- package/es/interpreter.d.ts +15 -8
- package/es/interpreter.js +48 -22
- package/es/invokeUtils.js +4 -3
- package/es/mapState.js +1 -1
- package/es/match.js +1 -1
- package/es/model.d.ts +2 -36
- package/es/model.types.d.ts +37 -0
- package/es/registry.js +2 -1
- package/es/scheduler.js +2 -1
- package/es/schema.js +1 -1
- package/es/serviceScope.js +1 -3
- package/es/stateUtils.d.ts +1 -0
- package/es/stateUtils.js +14 -8
- package/es/types.d.ts +35 -9
- package/es/types.js +1 -1
- package/es/utils.d.ts +3 -2
- package/es/utils.js +4 -40
- package/lib/Actor.d.ts +25 -24
- package/lib/Actor.js +87 -53
- package/lib/Machine.d.ts +17 -12
- package/lib/Machine.js +14 -14
- package/lib/SimulatedClock.d.ts +16 -16
- package/lib/State.d.ts +108 -108
- package/lib/State.js +246 -236
- package/lib/StateNode.d.ts +279 -278
- package/lib/StateNode.js +1535 -1350
- package/lib/_virtual/_tslib.js +81 -0
- package/lib/actionTypes.d.ts +19 -19
- package/lib/actionTypes.js +43 -23
- package/lib/actions.d.ts +138 -138
- package/lib/actions.js +465 -387
- package/lib/behaviors.d.ts +37 -0
- package/lib/behaviors.js +69 -0
- package/lib/constants.d.ts +5 -5
- package/lib/constants.js +13 -7
- package/lib/devTools.d.ts +15 -15
- package/lib/devTools.js +37 -26
- package/lib/each.d.ts +3 -3
- package/lib/environment.d.ts +1 -1
- package/lib/environment.js +7 -4
- package/lib/index.d.ts +30 -30
- package/lib/index.js +67 -57
- package/lib/interpreter.d.ts +205 -198
- package/lib/interpreter.js +1307 -1052
- package/lib/invoke.d.ts +10 -10
- package/lib/invokeUtils.d.ts +6 -6
- package/lib/invokeUtils.js +40 -37
- package/lib/json.d.ts +30 -30
- package/lib/mapState.d.ts +3 -3
- package/lib/mapState.js +31 -32
- package/lib/match.d.ts +8 -8
- package/lib/match.js +33 -47
- package/lib/model.d.ts +4 -38
- package/lib/model.js +5 -1
- package/lib/model.types.d.ts +37 -0
- package/lib/model.types.js +2 -0
- package/lib/patterns.d.ts +13 -13
- package/lib/registry.d.ts +8 -8
- package/lib/registry.js +21 -18
- package/lib/scheduler.d.ts +16 -16
- package/lib/scheduler.js +79 -70
- package/lib/schema.d.ts +1 -1
- package/lib/schema.js +6 -4
- package/lib/scxml.d.ts +5 -5
- package/lib/serviceScope.d.ts +3 -3
- package/lib/serviceScope.js +16 -12
- package/lib/stateUtils.d.ts +14 -13
- package/lib/stateUtils.js +232 -190
- package/lib/types.d.ts +928 -902
- package/lib/types.js +29 -29
- package/lib/utils.d.ts +68 -67
- package/lib/utils.js +530 -529
- package/package.json +5 -5
package/es/interpreter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { __values, __assign, __spreadArray, __read } from './_virtual/_tslib.js';
|
|
2
2
|
import { IS_PRODUCTION } from './environment.js';
|
|
3
|
-
import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isPromiseLike, isObservable,
|
|
3
|
+
import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isMachine, isPromiseLike, isObservable, isBehavior, 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';
|
|
@@ -11,6 +11,8 @@ import { isSpawnedActor, createDeferredActor } from './Actor.js';
|
|
|
11
11
|
import { Scheduler } from './scheduler.js';
|
|
12
12
|
import { registry } from './registry.js';
|
|
13
13
|
import { registerService, getGlobal } from './devTools.js';
|
|
14
|
+
import { spawnBehavior } from './behaviors.js';
|
|
15
|
+
|
|
14
16
|
var DEFAULT_SPAWN_OPTIONS = {
|
|
15
17
|
sync: false,
|
|
16
18
|
autoForward: false
|
|
@@ -830,24 +832,32 @@ function () {
|
|
|
830
832
|
}
|
|
831
833
|
|
|
832
834
|
var resolvedData = data ? mapContext(data, context, _event) : undefined;
|
|
835
|
+
|
|
836
|
+
if (typeof serviceCreator === 'string') {
|
|
837
|
+
// TODO: warn
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
|
|
833
841
|
var source = isFunction(serviceCreator) ? serviceCreator(context, _event.data, {
|
|
834
842
|
data: resolvedData,
|
|
835
843
|
src: invokeSource
|
|
836
844
|
}) : serviceCreator;
|
|
837
845
|
|
|
838
|
-
if (
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
846
|
+
if (!source) {
|
|
847
|
+
// TODO: warn?
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
var options = void 0;
|
|
852
|
+
|
|
853
|
+
if (isMachine(source)) {
|
|
854
|
+
source = resolvedData ? source.withContext(resolvedData) : source;
|
|
855
|
+
options = {
|
|
848
856
|
autoForward: autoForward
|
|
849
|
-
}
|
|
850
|
-
}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
this.spawn(source, id, options);
|
|
851
861
|
} else {
|
|
852
862
|
this.spawnActivity(activity);
|
|
853
863
|
}
|
|
@@ -910,13 +920,15 @@ function () {
|
|
|
910
920
|
} else if (isFunction(entity)) {
|
|
911
921
|
return this.spawnCallback(entity, name);
|
|
912
922
|
} else if (isSpawnedActor(entity)) {
|
|
913
|
-
return this.spawnActor(entity);
|
|
923
|
+
return this.spawnActor(entity, name);
|
|
914
924
|
} else if (isObservable(entity)) {
|
|
915
925
|
return this.spawnObservable(entity, name);
|
|
916
926
|
} else if (isMachine(entity)) {
|
|
917
927
|
return this.spawnMachine(entity, __assign(__assign({}, options), {
|
|
918
928
|
id: name
|
|
919
929
|
}));
|
|
930
|
+
} else if (isBehavior(entity)) {
|
|
931
|
+
return this.spawnBehavior(entity, name);
|
|
920
932
|
} else {
|
|
921
933
|
throw new Error("Unable to spawn entity \"" + name + "\" of type \"" + typeof entity + "\".");
|
|
922
934
|
}
|
|
@@ -962,11 +974,20 @@ function () {
|
|
|
962
974
|
return actor;
|
|
963
975
|
};
|
|
964
976
|
|
|
977
|
+
Interpreter.prototype.spawnBehavior = function (behavior, id) {
|
|
978
|
+
var actorRef = spawnBehavior(behavior, {
|
|
979
|
+
id: id,
|
|
980
|
+
parent: this
|
|
981
|
+
});
|
|
982
|
+
this.children.set(id, actorRef);
|
|
983
|
+
return actorRef;
|
|
984
|
+
};
|
|
985
|
+
|
|
965
986
|
Interpreter.prototype.spawnPromise = function (promise, id) {
|
|
966
987
|
var _this = this;
|
|
967
988
|
|
|
968
989
|
var canceled = false;
|
|
969
|
-
var resolvedData
|
|
990
|
+
var resolvedData;
|
|
970
991
|
promise.then(function (response) {
|
|
971
992
|
if (!canceled) {
|
|
972
993
|
resolvedData = response;
|
|
@@ -1060,7 +1081,7 @@ function () {
|
|
|
1060
1081
|
var canceled = false;
|
|
1061
1082
|
var receivers = new Set();
|
|
1062
1083
|
var listeners = new Set();
|
|
1063
|
-
var emitted
|
|
1084
|
+
var emitted;
|
|
1064
1085
|
|
|
1065
1086
|
var receive = function (e) {
|
|
1066
1087
|
emitted = e;
|
|
@@ -1131,7 +1152,7 @@ function () {
|
|
|
1131
1152
|
Interpreter.prototype.spawnObservable = function (source, id) {
|
|
1132
1153
|
var _this = this;
|
|
1133
1154
|
|
|
1134
|
-
var emitted
|
|
1155
|
+
var emitted;
|
|
1135
1156
|
var subscription = source.subscribe(function (value) {
|
|
1136
1157
|
emitted = value;
|
|
1137
1158
|
|
|
@@ -1175,8 +1196,8 @@ function () {
|
|
|
1175
1196
|
return actor;
|
|
1176
1197
|
};
|
|
1177
1198
|
|
|
1178
|
-
Interpreter.prototype.spawnActor = function (actor) {
|
|
1179
|
-
this.children.set(
|
|
1199
|
+
Interpreter.prototype.spawnActor = function (actor, name) {
|
|
1200
|
+
this.children.set(name, actor);
|
|
1180
1201
|
return actor;
|
|
1181
1202
|
};
|
|
1182
1203
|
|
|
@@ -1260,9 +1281,15 @@ function () {
|
|
|
1260
1281
|
|
|
1261
1282
|
Interpreter.prototype[symbolObservable] = function () {
|
|
1262
1283
|
return this;
|
|
1263
|
-
};
|
|
1284
|
+
}; // this gets stripped by Babel to avoid having "undefined" property in environments without this non-standard Symbol
|
|
1285
|
+
// it has to be here to be included in the generated .d.ts
|
|
1286
|
+
|
|
1264
1287
|
|
|
1265
1288
|
Interpreter.prototype.getSnapshot = function () {
|
|
1289
|
+
if (this.status === InterpreterStatus.NotStarted) {
|
|
1290
|
+
return this.initialState;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1266
1293
|
return this._state;
|
|
1267
1294
|
};
|
|
1268
1295
|
/**
|
|
@@ -1328,10 +1355,9 @@ function spawn(entity, nameOrOptions) {
|
|
|
1328
1355
|
* @param options Interpreter options
|
|
1329
1356
|
*/
|
|
1330
1357
|
|
|
1331
|
-
|
|
1332
1358
|
function interpret(machine, options) {
|
|
1333
1359
|
var interpreter = new Interpreter(machine, options);
|
|
1334
1360
|
return interpreter;
|
|
1335
1361
|
}
|
|
1336
1362
|
|
|
1337
|
-
export { Interpreter, InterpreterStatus, interpret, spawn };
|
|
1363
|
+
export { Interpreter, InterpreterStatus, interpret, spawn };
|
package/es/invokeUtils.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { __assign, __rest } from './_virtual/_tslib.js';
|
|
2
|
+
import './environment.js';
|
|
3
|
+
import './utils.js';
|
|
4
|
+
import './types.js';
|
|
2
5
|
import { invoke } from './actionTypes.js';
|
|
3
|
-
import './actions.js';
|
|
4
6
|
|
|
5
7
|
function toInvokeSource(src) {
|
|
6
8
|
if (typeof src === 'string') {
|
|
@@ -18,7 +20,6 @@ function toInvokeSource(src) {
|
|
|
18
20
|
|
|
19
21
|
return src;
|
|
20
22
|
}
|
|
21
|
-
|
|
22
23
|
function toInvokeDefinition(invokeConfig) {
|
|
23
24
|
return __assign(__assign({
|
|
24
25
|
type: invoke
|
|
@@ -36,4 +37,4 @@ function toInvokeDefinition(invokeConfig) {
|
|
|
36
37
|
});
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
export { toInvokeDefinition, toInvokeSource };
|
|
40
|
+
export { toInvokeDefinition, toInvokeSource };
|
package/es/mapState.js
CHANGED
package/es/match.js
CHANGED
package/es/model.d.ts
CHANGED
|
@@ -1,39 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
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];
|
|
1
|
+
import type { EventObject } from './types';
|
|
2
|
+
import { Cast, EventFromEventCreators, FinalModelCreators, Model, ModelCreators, Prop } from './model.types';
|
|
36
3
|
export declare function createModel<TContext, TEvent extends EventObject>(initialContext: TContext): Model<TContext, TEvent, void>;
|
|
37
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>;
|
|
38
|
-
export {};
|
|
39
5
|
//# sourceMappingURL=model.d.ts.map
|
|
@@ -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, any>;
|
|
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
|
package/es/registry.js
CHANGED
package/es/scheduler.js
CHANGED
package/es/schema.js
CHANGED
package/es/serviceScope.js
CHANGED
|
@@ -3,16 +3,14 @@
|
|
|
3
3
|
* This is used to provide the correct service to spawn().
|
|
4
4
|
*/
|
|
5
5
|
var serviceStack = [];
|
|
6
|
-
|
|
7
6
|
var provide = function (service, fn) {
|
|
8
7
|
serviceStack.push(service);
|
|
9
8
|
var result = fn(service);
|
|
10
9
|
serviceStack.pop();
|
|
11
10
|
return result;
|
|
12
11
|
};
|
|
13
|
-
|
|
14
12
|
var consume = function (fn) {
|
|
15
13
|
return fn(serviceStack[serviceStack.length - 1]);
|
|
16
14
|
};
|
|
17
15
|
|
|
18
|
-
export { consume, provide };
|
|
16
|
+
export { consume, provide };
|
package/es/stateUtils.d.ts
CHANGED
|
@@ -10,5 +10,6 @@ export declare function getValue<TC, TE extends EventObject>(rootNode: StateNode
|
|
|
10
10
|
export declare function has<T>(iterable: Iterable<T>, item: T): boolean;
|
|
11
11
|
export declare function nextEvents<TC, TE extends EventObject>(configuration: Array<StateNode<TC, any, TE>>): Array<TE['type']>;
|
|
12
12
|
export declare function isInFinalState<TC, TE extends EventObject>(configuration: Array<StateNode<TC, any, TE, any>>, stateNode: StateNode<TC, any, TE, any>): boolean;
|
|
13
|
+
export declare function getMeta(configuration?: StateNode[]): Record<string, any>;
|
|
13
14
|
export {};
|
|
14
15
|
//# sourceMappingURL=stateUtils.d.ts.map
|
package/es/stateUtils.js
CHANGED
|
@@ -4,13 +4,11 @@ import { keys, flatten } from './utils.js';
|
|
|
4
4
|
var isLeafNode = function (stateNode) {
|
|
5
5
|
return stateNode.type === 'atomic' || stateNode.type === 'final';
|
|
6
6
|
};
|
|
7
|
-
|
|
8
7
|
function getChildren(stateNode) {
|
|
9
8
|
return keys(stateNode.states).map(function (key) {
|
|
10
9
|
return stateNode.states[key];
|
|
11
10
|
});
|
|
12
11
|
}
|
|
13
|
-
|
|
14
12
|
function getAllStateNodes(stateNode) {
|
|
15
13
|
var stateNodes = [stateNode];
|
|
16
14
|
|
|
@@ -20,7 +18,6 @@ function getAllStateNodes(stateNode) {
|
|
|
20
18
|
|
|
21
19
|
return stateNodes.concat(flatten(getChildren(stateNode).map(getAllStateNodes)));
|
|
22
20
|
}
|
|
23
|
-
|
|
24
21
|
function getConfiguration(prevStateNodes, stateNodes) {
|
|
25
22
|
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
|
|
26
23
|
|
|
@@ -205,12 +202,10 @@ function getAdjList(configuration) {
|
|
|
205
202
|
|
|
206
203
|
return adjList;
|
|
207
204
|
}
|
|
208
|
-
|
|
209
205
|
function getValue(rootNode, configuration) {
|
|
210
206
|
var config = getConfiguration([rootNode], configuration);
|
|
211
207
|
return getValueFromAdj(rootNode, getAdjList(config));
|
|
212
208
|
}
|
|
213
|
-
|
|
214
209
|
function has(iterable, item) {
|
|
215
210
|
if (Array.isArray(iterable)) {
|
|
216
211
|
return iterable.some(function (member) {
|
|
@@ -224,13 +219,11 @@ function has(iterable, item) {
|
|
|
224
219
|
|
|
225
220
|
return false; // TODO: fix
|
|
226
221
|
}
|
|
227
|
-
|
|
228
222
|
function nextEvents(configuration) {
|
|
229
223
|
return __spreadArray([], __read(new Set(flatten(__spreadArray([], __read(configuration.map(function (sn) {
|
|
230
224
|
return sn.ownEvents;
|
|
231
225
|
})))))));
|
|
232
226
|
}
|
|
233
|
-
|
|
234
227
|
function isInFinalState(configuration, stateNode) {
|
|
235
228
|
if (stateNode.type === 'compound') {
|
|
236
229
|
return getChildren(stateNode).some(function (s) {
|
|
@@ -246,5 +239,18 @@ function isInFinalState(configuration, stateNode) {
|
|
|
246
239
|
|
|
247
240
|
return false;
|
|
248
241
|
}
|
|
242
|
+
function getMeta(configuration) {
|
|
243
|
+
if (configuration === void 0) {
|
|
244
|
+
configuration = [];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return configuration.reduce(function (acc, stateNode) {
|
|
248
|
+
if (stateNode.meta !== undefined) {
|
|
249
|
+
acc[stateNode.id] = stateNode.meta;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return acc;
|
|
253
|
+
}, {});
|
|
254
|
+
}
|
|
249
255
|
|
|
250
|
-
export { getAdjList, getAllStateNodes, getChildren, getConfiguration, getValue, has, isInFinalState, isLeafNode, nextEvents };
|
|
256
|
+
export { getAdjList, getAllStateNodes, getChildren, getConfiguration, getMeta, getValue, has, isInFinalState, isLeafNode, nextEvents };
|
package/es/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { StateNode } from './StateNode';
|
|
2
2
|
import { State } from './State';
|
|
3
3
|
import { Interpreter, Clock } from './interpreter';
|
|
4
|
+
import { Model } from './model.types';
|
|
4
5
|
export declare type EventType = string;
|
|
5
6
|
export declare type ActionType = string;
|
|
6
7
|
export declare type MetaObject = Record<string, any>;
|
|
@@ -134,7 +135,7 @@ export interface PayloadSender<TEvent extends EventObject> {
|
|
|
134
135
|
<K extends TEvent['type']>(eventType: K, payload: NeverIfEmpty<ExtractExtraParameters<TEvent, K>>): void;
|
|
135
136
|
}
|
|
136
137
|
export declare type Receiver<TEvent extends EventObject> = (listener: (event: TEvent) => void) => void;
|
|
137
|
-
export declare type InvokeCallback<TEvent extends EventObject = AnyEventObject> = (callback: Sender<
|
|
138
|
+
export declare type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject> = (callback: Sender<TSentEvent>, onReceive: Receiver<TEvent>) => (() => void) | Promise<any> | void;
|
|
138
139
|
export interface InvokeMeta {
|
|
139
140
|
data: any;
|
|
140
141
|
src: InvokeSourceDefinition;
|
|
@@ -152,7 +153,7 @@ export interface InvokeMeta {
|
|
|
152
153
|
* @param context The current machine `context`
|
|
153
154
|
* @param event The event that invoked the service
|
|
154
155
|
*/
|
|
155
|
-
export declare type InvokeCreator<TContext, TEvent extends EventObject
|
|
156
|
+
export declare type InvokeCreator<TContext, TEvent extends EventObject, TFinalContext = any> = (context: TContext, event: TEvent, meta: InvokeMeta) => PromiseLike<TFinalContext> | StateMachine<TFinalContext, any, any> | Subscribable<EventObject> | InvokeCallback<any, TEvent> | Behavior<any>;
|
|
156
157
|
export interface InvokeDefinition<TContext, TEvent extends EventObject> extends ActivityDefinition<TContext, TEvent> {
|
|
157
158
|
/**
|
|
158
159
|
* The source of the machine to be invoked, or the machine itself.
|
|
@@ -384,6 +385,13 @@ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEv
|
|
|
384
385
|
* The tags for this state node, which are accumulated into the `state.tags` property.
|
|
385
386
|
*/
|
|
386
387
|
tags?: SingleOrArray<string>;
|
|
388
|
+
/**
|
|
389
|
+
* Whether actions should be called in order.
|
|
390
|
+
* When `false` (default), `assign(...)` actions are prioritized before other actions.
|
|
391
|
+
*
|
|
392
|
+
* @default false
|
|
393
|
+
*/
|
|
394
|
+
preserveActionOrder?: boolean;
|
|
387
395
|
}
|
|
388
396
|
export interface StateNodeDefinition<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> {
|
|
389
397
|
id: string;
|
|
@@ -875,29 +883,47 @@ export interface Subscription {
|
|
|
875
883
|
unsubscribe(): void;
|
|
876
884
|
}
|
|
877
885
|
export interface Subscribable<T> {
|
|
878
|
-
subscribe(observer: Observer<T>): Subscription;
|
|
879
886
|
subscribe(next: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;
|
|
887
|
+
subscribe(observer: Observer<T>): Subscription;
|
|
880
888
|
}
|
|
881
|
-
export declare type Spawnable = StateMachine<any, any, any> |
|
|
889
|
+
export declare type Spawnable = StateMachine<any, any, any> | PromiseLike<any> | InvokeCallback | Subscribable<any> | Behavior<any>;
|
|
882
890
|
export declare type ExtractEvent<TEvent extends EventObject, TEventType extends TEvent['type']> = TEvent extends {
|
|
883
891
|
type: TEventType;
|
|
884
892
|
} ? TEvent : never;
|
|
893
|
+
export interface BaseActorRef<TEvent extends EventObject> {
|
|
894
|
+
send: (event: TEvent) => void;
|
|
895
|
+
}
|
|
885
896
|
export interface ActorRef<TEvent extends EventObject, TEmitted = any> extends Subscribable<TEmitted> {
|
|
886
897
|
send: Sender<TEvent>;
|
|
887
|
-
}
|
|
888
|
-
export interface SpawnedActorRef<TEvent extends EventObject, TEmitted = any> extends ActorRef<TEvent, TEmitted> {
|
|
889
898
|
id: string;
|
|
890
899
|
getSnapshot: () => TEmitted | undefined;
|
|
891
900
|
stop?: () => void;
|
|
892
901
|
toJSON?: () => any;
|
|
893
902
|
}
|
|
894
|
-
|
|
903
|
+
/**
|
|
904
|
+
* @deprecated Use `ActorRef` instead.
|
|
905
|
+
*/
|
|
906
|
+
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>> & {
|
|
895
908
|
/**
|
|
896
|
-
* @deprecated
|
|
909
|
+
* @deprecated Use `.getSnapshot()` instead.
|
|
897
910
|
*/
|
|
898
911
|
state: State<TContext, TEvent, any, TTypestate>;
|
|
899
|
-
} : T extends Promise<infer U> ?
|
|
912
|
+
} : T extends Promise<infer U> ? ActorRef<never, U> : T extends Behavior<infer TEvent1, infer TEmitted> ? ActorRef<TEvent1, TEmitted> : never;
|
|
900
913
|
export declare type AnyInterpreter = Interpreter<any, any, any, any>;
|
|
901
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;
|
|
915
|
+
export interface ActorContext<TEvent extends EventObject, TEmitted> {
|
|
916
|
+
parent?: ActorRef<any, any>;
|
|
917
|
+
self: ActorRef<TEvent, TEmitted>;
|
|
918
|
+
id: string;
|
|
919
|
+
observers: Set<Observer<TEmitted>>;
|
|
920
|
+
}
|
|
921
|
+
export interface Behavior<TEvent extends EventObject, TEmitted = any> {
|
|
922
|
+
transition: (state: TEmitted, event: TEvent, actorCtx: ActorContext<TEvent, TEmitted>) => TEmitted;
|
|
923
|
+
initialState: TEmitted;
|
|
924
|
+
start?: (actorCtx: ActorContext<TEvent, TEmitted>) => TEmitted;
|
|
925
|
+
}
|
|
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;
|
|
902
928
|
export {};
|
|
903
929
|
//# sourceMappingURL=types.d.ts.map
|
package/es/types.js
CHANGED
package/es/utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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';
|
|
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, Observer, Behavior } from './types';
|
|
2
2
|
import { StateNode } from './StateNode';
|
|
3
|
-
import {
|
|
3
|
+
import { State } from './State';
|
|
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;
|
|
@@ -40,6 +40,7 @@ export declare function toArray<T>(value: T[] | T | undefined): T[];
|
|
|
40
40
|
export declare function mapContext<TContext, TEvent extends EventObject>(mapper: Mapper<TContext, TEvent, any> | PropertyMapper<TContext, TEvent, any>, context: TContext, _event: SCXML.Event<TEvent>): any;
|
|
41
41
|
export declare function isBuiltInEvent(eventType: EventType): boolean;
|
|
42
42
|
export declare function isPromiseLike(value: any): value is PromiseLike<any>;
|
|
43
|
+
export declare function isBehavior(value: any): value is Behavior<any, any>;
|
|
43
44
|
export declare function partition<T, A extends T, B extends T>(items: T[], predicate: (item: T) => item is A): [A[], B[]];
|
|
44
45
|
export declare function updateHistoryStates(hist: HistoryValue, stateValue: StateValue): Record<string, HistoryValue | undefined>;
|
|
45
46
|
export declare function updateHistoryValue(hist: HistoryValue, stateValue: StateValue): HistoryValue;
|