xstate 5.0.0-beta.20 → 5.0.0-beta.22
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/actions/dist/xstate-actions.cjs.js +1 -1
- package/actions/dist/xstate-actions.development.cjs.js +1 -1
- package/actions/dist/xstate-actions.development.esm.js +1 -1
- package/actions/dist/xstate-actions.esm.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.esm.js +1 -1
- package/actors/dist/xstate-actors.esm.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dev/dist/xstate-dev.umd.min.js.map +1 -1
- package/dist/{actions-d1c41ed3.development.cjs.js → actions-4b70fc8d.development.cjs.js} +174 -82
- package/dist/{actions-069d9805.cjs.js → actions-8f2e997e.cjs.js} +172 -83
- package/dist/{actions-b299d008.development.esm.js → actions-d4305983.development.esm.js} +171 -82
- package/dist/{actions-a8a9433c.esm.js → actions-fb7384f8.esm.js} +169 -83
- package/dist/declarations/src/Machine.d.ts +2 -2
- package/dist/declarations/src/State.d.ts +4 -7
- package/dist/declarations/src/StateMachine.d.ts +7 -6
- package/dist/declarations/src/StateNode.d.ts +3 -3
- package/dist/declarations/src/actions/send.d.ts +1 -1
- package/dist/declarations/src/actions/stop.d.ts +1 -1
- package/dist/declarations/src/actions.d.ts +2 -2
- package/dist/declarations/src/actors/callback.d.ts +4 -4
- package/dist/declarations/src/actors/observable.d.ts +7 -4
- package/dist/declarations/src/actors/promise.d.ts +4 -4
- package/dist/declarations/src/dev/index.d.ts +6 -6
- package/dist/declarations/src/index.d.ts +3 -2
- package/dist/declarations/src/interpreter.d.ts +30 -16
- package/dist/declarations/src/types.d.ts +52 -33
- package/dist/declarations/src/utils.d.ts +2 -2
- package/dist/xstate.cjs.js +17 -14
- package/dist/xstate.cjs.mjs +3 -1
- package/dist/xstate.development.cjs.js +17 -14
- package/dist/xstate.development.cjs.mjs +3 -1
- package/dist/xstate.development.esm.js +14 -13
- package/dist/xstate.esm.js +14 -13
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { devToolsAdapter } from '../dev/dist/xstate-dev.esm.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* `T | unknown` reduces to `unknown` and that can be problematic when it comes to contextual typing.
|
|
5
|
+
* It especially is a problem when the union has a function member, like here:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* declare function test(cbOrVal: ((arg: number) => unknown) | unknown): void;
|
|
9
|
+
* test((arg) => {}) // oops, implicit any
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* This type can be used to avoid this problem. This union represents the same value space as `unknown`.
|
|
13
|
+
*/
|
|
14
|
+
|
|
3
15
|
// https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
|
|
4
16
|
|
|
5
17
|
/**
|
|
@@ -404,27 +416,15 @@ function toArray(value) {
|
|
|
404
416
|
}
|
|
405
417
|
return toArrayStrict(value);
|
|
406
418
|
}
|
|
407
|
-
function mapContext(mapper, context, event) {
|
|
419
|
+
function mapContext(mapper, context, event, self) {
|
|
408
420
|
if (typeof mapper === 'function') {
|
|
409
421
|
return mapper({
|
|
410
422
|
context,
|
|
411
|
-
event
|
|
423
|
+
event,
|
|
424
|
+
self
|
|
412
425
|
});
|
|
413
426
|
}
|
|
414
|
-
|
|
415
|
-
const args = {
|
|
416
|
-
context,
|
|
417
|
-
event
|
|
418
|
-
};
|
|
419
|
-
for (const key of Object.keys(mapper)) {
|
|
420
|
-
const subMapper = mapper[key];
|
|
421
|
-
if (typeof subMapper === 'function') {
|
|
422
|
-
result[key] = subMapper(args);
|
|
423
|
-
} else {
|
|
424
|
-
result[key] = subMapper;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
return result;
|
|
427
|
+
return mapper;
|
|
428
428
|
}
|
|
429
429
|
function isPromiseLike(value) {
|
|
430
430
|
if (value instanceof Promise) {
|
|
@@ -476,13 +476,12 @@ function toInvokeConfig(invocable, id) {
|
|
|
476
476
|
};
|
|
477
477
|
}
|
|
478
478
|
function toObserver(nextHandler, errorHandler, completionHandler) {
|
|
479
|
-
const noop = () => {};
|
|
480
479
|
const isObserver = typeof nextHandler === 'object';
|
|
481
|
-
const self = isObserver ? nextHandler :
|
|
480
|
+
const self = isObserver ? nextHandler : undefined;
|
|
482
481
|
return {
|
|
483
|
-
next: (
|
|
484
|
-
error: (
|
|
485
|
-
complete: (
|
|
482
|
+
next: (isObserver ? nextHandler.next : nextHandler)?.bind(self),
|
|
483
|
+
error: (isObserver ? nextHandler.error : errorHandler)?.bind(self),
|
|
484
|
+
complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self)
|
|
486
485
|
};
|
|
487
486
|
}
|
|
488
487
|
function createInvokeId(stateNodeId, index) {
|
|
@@ -496,7 +495,7 @@ function resolveReferencedActor(referenced) {
|
|
|
496
495
|
}
|
|
497
496
|
|
|
498
497
|
function fromCallback(invokeCallback) {
|
|
499
|
-
|
|
498
|
+
return {
|
|
500
499
|
config: invokeCallback,
|
|
501
500
|
start: (_state, {
|
|
502
501
|
self
|
|
@@ -562,19 +561,20 @@ function fromCallback(invokeCallback) {
|
|
|
562
561
|
},
|
|
563
562
|
getSnapshot: () => undefined,
|
|
564
563
|
getPersistedState: ({
|
|
565
|
-
input
|
|
566
|
-
|
|
564
|
+
input,
|
|
565
|
+
canceled
|
|
566
|
+
}) => ({
|
|
567
|
+
input,
|
|
568
|
+
canceled
|
|
569
|
+
})
|
|
567
570
|
};
|
|
568
|
-
return logic;
|
|
569
571
|
}
|
|
570
572
|
|
|
571
573
|
function fromObservable(observableCreator) {
|
|
572
574
|
const nextEventType = '$$xstate.next';
|
|
573
575
|
const errorEventType = '$$xstate.error';
|
|
574
576
|
const completeEventType = '$$xstate.complete';
|
|
575
|
-
|
|
576
|
-
// TODO: add event types
|
|
577
|
-
const logic = {
|
|
577
|
+
return {
|
|
578
578
|
config: observableCreator,
|
|
579
579
|
transition: (state, event, {
|
|
580
580
|
self,
|
|
@@ -604,6 +604,7 @@ function fromObservable(observableCreator) {
|
|
|
604
604
|
status: 'error',
|
|
605
605
|
input: undefined,
|
|
606
606
|
data: event.data,
|
|
607
|
+
// TODO: if we keep this as `data` we should reflect this in the type
|
|
607
608
|
subscription: undefined
|
|
608
609
|
};
|
|
609
610
|
case completeEventType:
|
|
@@ -681,7 +682,6 @@ function fromObservable(observableCreator) {
|
|
|
681
682
|
subscription: undefined
|
|
682
683
|
})
|
|
683
684
|
};
|
|
684
|
-
return logic;
|
|
685
685
|
}
|
|
686
686
|
|
|
687
687
|
/**
|
|
@@ -698,7 +698,7 @@ function fromEventObservable(lazyObservable) {
|
|
|
698
698
|
const completeEventType = '$$xstate.complete';
|
|
699
699
|
|
|
700
700
|
// TODO: event types
|
|
701
|
-
|
|
701
|
+
return {
|
|
702
702
|
config: lazyObservable,
|
|
703
703
|
transition: (state, event) => {
|
|
704
704
|
if (state.status !== 'active') {
|
|
@@ -711,6 +711,7 @@ function fromEventObservable(lazyObservable) {
|
|
|
711
711
|
status: 'error',
|
|
712
712
|
input: undefined,
|
|
713
713
|
data: event.data,
|
|
714
|
+
// TODO: if we keep this as `data` we should reflect this in the type
|
|
714
715
|
subscription: undefined
|
|
715
716
|
};
|
|
716
717
|
case completeEventType:
|
|
@@ -785,7 +786,6 @@ function fromEventObservable(lazyObservable) {
|
|
|
785
786
|
subscription: undefined
|
|
786
787
|
})
|
|
787
788
|
};
|
|
788
|
-
return logic;
|
|
789
789
|
}
|
|
790
790
|
|
|
791
791
|
const resolveEventType = '$$xstate.resolve';
|
|
@@ -813,6 +813,7 @@ promiseCreator) {
|
|
|
813
813
|
...state,
|
|
814
814
|
status: 'error',
|
|
815
815
|
data: event.data,
|
|
816
|
+
// TODO: if we keep this as `data` we should reflect this in the type
|
|
816
817
|
input: undefined
|
|
817
818
|
};
|
|
818
819
|
case stopSignalType:
|
|
@@ -919,7 +920,20 @@ function toActorRef(actorRefLike) {
|
|
|
919
920
|
}
|
|
920
921
|
const emptyLogic = fromTransition(_ => undefined, undefined);
|
|
921
922
|
function createEmptyActor() {
|
|
922
|
-
return
|
|
923
|
+
return createActor(emptyLogic);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* This function makes sure that unhandled errors are thrown in a separate macrotask.
|
|
928
|
+
* It allows those errors to be detected by global error handlers and reported to bug tracking services
|
|
929
|
+
* without interrupting our own stack of execution.
|
|
930
|
+
*
|
|
931
|
+
* @param err error to be thrown
|
|
932
|
+
*/
|
|
933
|
+
function reportUnhandledError(err) {
|
|
934
|
+
setTimeout(() => {
|
|
935
|
+
throw err;
|
|
936
|
+
});
|
|
923
937
|
}
|
|
924
938
|
|
|
925
939
|
function createSystem() {
|
|
@@ -962,6 +976,11 @@ let ActorStatus = /*#__PURE__*/function (ActorStatus) {
|
|
|
962
976
|
ActorStatus[ActorStatus["Stopped"] = 2] = "Stopped";
|
|
963
977
|
return ActorStatus;
|
|
964
978
|
}({});
|
|
979
|
+
|
|
980
|
+
/**
|
|
981
|
+
* @deprecated Use `ActorStatus` instead.
|
|
982
|
+
*/
|
|
983
|
+
const InterpreterStatus = ActorStatus;
|
|
965
984
|
const defaultOptions = {
|
|
966
985
|
deferEvents: true,
|
|
967
986
|
clock: {
|
|
@@ -975,9 +994,9 @@ const defaultOptions = {
|
|
|
975
994
|
logger: console.log.bind(console),
|
|
976
995
|
devTools: false
|
|
977
996
|
};
|
|
978
|
-
class
|
|
997
|
+
class Actor {
|
|
979
998
|
/**
|
|
980
|
-
* The current state of the
|
|
999
|
+
* The current internal state of the actor.
|
|
981
1000
|
*/
|
|
982
1001
|
|
|
983
1002
|
/**
|
|
@@ -1001,10 +1020,10 @@ class Interpreter {
|
|
|
1001
1020
|
*/
|
|
1002
1021
|
|
|
1003
1022
|
/**
|
|
1004
|
-
* Creates a new
|
|
1023
|
+
* Creates a new actor instance for the given logic with the provided options, if any.
|
|
1005
1024
|
*
|
|
1006
|
-
* @param logic The logic to
|
|
1007
|
-
* @param options
|
|
1025
|
+
* @param logic The logic to create an actor from
|
|
1026
|
+
* @param options Actor options
|
|
1008
1027
|
*/
|
|
1009
1028
|
constructor(logic, options) {
|
|
1010
1029
|
this.logic = logic;
|
|
@@ -1068,7 +1087,7 @@ class Interpreter {
|
|
|
1068
1087
|
}
|
|
1069
1088
|
};
|
|
1070
1089
|
|
|
1071
|
-
// Ensure that the send method is bound to this
|
|
1090
|
+
// Ensure that the send method is bound to this Actor instance
|
|
1072
1091
|
// if destructured
|
|
1073
1092
|
this.send = this.send.bind(this);
|
|
1074
1093
|
this._initState();
|
|
@@ -1090,29 +1109,38 @@ class Interpreter {
|
|
|
1090
1109
|
deferredFn();
|
|
1091
1110
|
}
|
|
1092
1111
|
for (const observer of this.observers) {
|
|
1093
|
-
|
|
1112
|
+
// TODO: should observers be notified in case of the error?
|
|
1113
|
+
try {
|
|
1114
|
+
observer.next?.(snapshot);
|
|
1115
|
+
} catch (err) {
|
|
1116
|
+
reportUnhandledError(err);
|
|
1117
|
+
}
|
|
1094
1118
|
}
|
|
1095
1119
|
const status = this.logic.getStatus?.(state);
|
|
1096
1120
|
switch (status?.status) {
|
|
1097
1121
|
case 'done':
|
|
1098
1122
|
this._stopProcedure();
|
|
1123
|
+
this._complete();
|
|
1099
1124
|
this._doneEvent = doneInvoke(this.id, status.data);
|
|
1100
1125
|
this._parent?.send(this._doneEvent);
|
|
1101
|
-
this._complete();
|
|
1102
1126
|
break;
|
|
1103
1127
|
case 'error':
|
|
1104
1128
|
this._stopProcedure();
|
|
1105
|
-
this._parent?.send(error(this.id, status.data));
|
|
1106
1129
|
this._error(status.data);
|
|
1130
|
+
this._parent?.send(error(this.id, status.data));
|
|
1107
1131
|
break;
|
|
1108
1132
|
}
|
|
1109
1133
|
}
|
|
1110
1134
|
subscribe(nextListenerOrObserver, errorListener, completeListener) {
|
|
1111
1135
|
const observer = toObserver(nextListenerOrObserver, errorListener, completeListener);
|
|
1112
|
-
this.
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1136
|
+
if (this.status !== ActorStatus.Stopped) {
|
|
1137
|
+
this.observers.add(observer);
|
|
1138
|
+
} else {
|
|
1139
|
+
try {
|
|
1140
|
+
observer.complete?.();
|
|
1141
|
+
} catch (err) {
|
|
1142
|
+
reportUnhandledError(err);
|
|
1143
|
+
}
|
|
1116
1144
|
}
|
|
1117
1145
|
return {
|
|
1118
1146
|
unsubscribe: () => {
|
|
@@ -1122,7 +1150,7 @@ class Interpreter {
|
|
|
1122
1150
|
}
|
|
1123
1151
|
|
|
1124
1152
|
/**
|
|
1125
|
-
* Starts the
|
|
1153
|
+
* Starts the Actor from the initial state
|
|
1126
1154
|
*/
|
|
1127
1155
|
start() {
|
|
1128
1156
|
if (this.status === ActorStatus.Running) {
|
|
@@ -1134,8 +1162,26 @@ class Interpreter {
|
|
|
1134
1162
|
this.system._set(this._systemId, this);
|
|
1135
1163
|
}
|
|
1136
1164
|
this.status = ActorStatus.Running;
|
|
1165
|
+
const status = this.logic.getStatus?.(this._state);
|
|
1166
|
+
switch (status?.status) {
|
|
1167
|
+
case 'done':
|
|
1168
|
+
// a state machine can be "done" upon intialization (it could reach a final state using initial microsteps)
|
|
1169
|
+
// we still need to complete observers, flush deferreds etc
|
|
1170
|
+
this.update(this._state);
|
|
1171
|
+
// fallthrough
|
|
1172
|
+
case 'error':
|
|
1173
|
+
// TODO: rethink cleanup of observers, mailbox, etc
|
|
1174
|
+
return this;
|
|
1175
|
+
}
|
|
1137
1176
|
if (this.logic.start) {
|
|
1138
|
-
|
|
1177
|
+
try {
|
|
1178
|
+
this.logic.start(this._state, this._actorContext);
|
|
1179
|
+
} catch (err) {
|
|
1180
|
+
this._stopProcedure();
|
|
1181
|
+
this._error(err);
|
|
1182
|
+
this._parent?.send(error(this.id, err));
|
|
1183
|
+
return this;
|
|
1184
|
+
}
|
|
1139
1185
|
}
|
|
1140
1186
|
|
|
1141
1187
|
// TODO: this notifies all subscribers but usually this is redundant
|
|
@@ -1149,23 +1195,30 @@ class Interpreter {
|
|
|
1149
1195
|
return this;
|
|
1150
1196
|
}
|
|
1151
1197
|
_process(event) {
|
|
1198
|
+
// TODO: reexamine what happens when an action (or a guard or smth) throws
|
|
1199
|
+
let nextState;
|
|
1200
|
+
let caughtError;
|
|
1152
1201
|
try {
|
|
1153
|
-
|
|
1154
|
-
this.update(nextState);
|
|
1155
|
-
if (event.type === stopSignalType) {
|
|
1156
|
-
this._stopProcedure();
|
|
1157
|
-
this._complete();
|
|
1158
|
-
}
|
|
1202
|
+
nextState = this.logic.transition(this._state, event, this._actorContext);
|
|
1159
1203
|
} catch (err) {
|
|
1160
|
-
//
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
}
|
|
1204
|
+
// we wrap it in a box so we can rethrow it later even if falsy value gets caught here
|
|
1205
|
+
caughtError = {
|
|
1206
|
+
err
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
if (caughtError) {
|
|
1210
|
+
const {
|
|
1211
|
+
err
|
|
1212
|
+
} = caughtError;
|
|
1213
|
+
this._stopProcedure();
|
|
1214
|
+
this._error(err);
|
|
1215
|
+
this._parent?.send(error(this.id, err));
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
this.update(nextState);
|
|
1219
|
+
if (event.type === stopSignalType) {
|
|
1220
|
+
this._stopProcedure();
|
|
1221
|
+
this._complete();
|
|
1169
1222
|
}
|
|
1170
1223
|
}
|
|
1171
1224
|
_stop() {
|
|
@@ -1184,7 +1237,7 @@ class Interpreter {
|
|
|
1184
1237
|
}
|
|
1185
1238
|
|
|
1186
1239
|
/**
|
|
1187
|
-
* Stops the
|
|
1240
|
+
* Stops the Actor and unsubscribe all listeners.
|
|
1188
1241
|
*/
|
|
1189
1242
|
stop() {
|
|
1190
1243
|
if (this._parent) {
|
|
@@ -1194,19 +1247,39 @@ class Interpreter {
|
|
|
1194
1247
|
}
|
|
1195
1248
|
_complete() {
|
|
1196
1249
|
for (const observer of this.observers) {
|
|
1197
|
-
|
|
1250
|
+
try {
|
|
1251
|
+
observer.complete?.();
|
|
1252
|
+
} catch (err) {
|
|
1253
|
+
reportUnhandledError(err);
|
|
1254
|
+
}
|
|
1198
1255
|
}
|
|
1199
1256
|
this.observers.clear();
|
|
1200
1257
|
}
|
|
1201
|
-
_error(
|
|
1258
|
+
_error(err) {
|
|
1259
|
+
if (!this.observers.size) {
|
|
1260
|
+
if (!this._parent) {
|
|
1261
|
+
reportUnhandledError(err);
|
|
1262
|
+
}
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
let reportError = false;
|
|
1202
1266
|
for (const observer of this.observers) {
|
|
1203
|
-
observer.error
|
|
1267
|
+
const errorListener = observer.error;
|
|
1268
|
+
reportError ||= !errorListener;
|
|
1269
|
+
try {
|
|
1270
|
+
errorListener?.(err);
|
|
1271
|
+
} catch (err2) {
|
|
1272
|
+
reportUnhandledError(err2);
|
|
1273
|
+
}
|
|
1204
1274
|
}
|
|
1205
1275
|
this.observers.clear();
|
|
1276
|
+
if (reportError) {
|
|
1277
|
+
reportUnhandledError(err);
|
|
1278
|
+
}
|
|
1206
1279
|
}
|
|
1207
1280
|
_stopProcedure() {
|
|
1208
1281
|
if (this.status !== ActorStatus.Running) {
|
|
1209
|
-
//
|
|
1282
|
+
// Actor already stopped; do nothing
|
|
1210
1283
|
return this;
|
|
1211
1284
|
}
|
|
1212
1285
|
|
|
@@ -1228,7 +1301,7 @@ class Interpreter {
|
|
|
1228
1301
|
}
|
|
1229
1302
|
|
|
1230
1303
|
/**
|
|
1231
|
-
* Sends an event to the running
|
|
1304
|
+
* Sends an event to the running Actor to trigger a transition.
|
|
1232
1305
|
*
|
|
1233
1306
|
* @param event The event to send
|
|
1234
1307
|
*/
|
|
@@ -1299,17 +1372,28 @@ class Interpreter {
|
|
|
1299
1372
|
}
|
|
1300
1373
|
|
|
1301
1374
|
/**
|
|
1302
|
-
* Creates a new
|
|
1375
|
+
* Creates a new `ActorRef` instance for the given machine with the provided options, if any.
|
|
1303
1376
|
*
|
|
1304
|
-
* @param machine The machine to
|
|
1305
|
-
* @param options
|
|
1377
|
+
* @param machine The machine to create an actor from
|
|
1378
|
+
* @param options `ActorRef` options
|
|
1306
1379
|
*/
|
|
1307
1380
|
|
|
1308
|
-
function
|
|
1309
|
-
const interpreter = new
|
|
1381
|
+
function createActor(logic, options) {
|
|
1382
|
+
const interpreter = new Actor(logic, options);
|
|
1310
1383
|
return interpreter;
|
|
1311
1384
|
}
|
|
1312
1385
|
|
|
1386
|
+
/**
|
|
1387
|
+
* Creates a new Interpreter instance for the given machine with the provided options, if any.
|
|
1388
|
+
*
|
|
1389
|
+
* @deprecated Use `createActor` instead
|
|
1390
|
+
*/
|
|
1391
|
+
const interpret = createActor;
|
|
1392
|
+
|
|
1393
|
+
/**
|
|
1394
|
+
* @deprecated Use `Actor` instead.
|
|
1395
|
+
*/
|
|
1396
|
+
|
|
1313
1397
|
function resolve$6(actorContext, state, actionArgs, {
|
|
1314
1398
|
id,
|
|
1315
1399
|
systemId,
|
|
@@ -1321,7 +1405,7 @@ function resolve$6(actorContext, state, actionArgs, {
|
|
|
1321
1405
|
if (referenced) {
|
|
1322
1406
|
// TODO: inline `input: undefined` should win over the referenced one
|
|
1323
1407
|
const configuredInput = input || referenced.input;
|
|
1324
|
-
actorRef =
|
|
1408
|
+
actorRef = createActor(referenced.src, {
|
|
1325
1409
|
id,
|
|
1326
1410
|
src,
|
|
1327
1411
|
parent: actorContext?.self,
|
|
@@ -1524,10 +1608,10 @@ function toGuardDefinition(guardConfig, getPredicate) {
|
|
|
1524
1608
|
}
|
|
1525
1609
|
}
|
|
1526
1610
|
|
|
1527
|
-
function getOutput(configuration, context, event) {
|
|
1611
|
+
function getOutput(configuration, context, event, self) {
|
|
1528
1612
|
const machine = configuration[0].machine;
|
|
1529
1613
|
const finalChildStateNode = configuration.find(stateNode => stateNode.type === 'final' && stateNode.parent === machine.root);
|
|
1530
|
-
return finalChildStateNode && finalChildStateNode.output ? mapContext(finalChildStateNode.output, context, event) : undefined;
|
|
1614
|
+
return finalChildStateNode && finalChildStateNode.output ? mapContext(finalChildStateNode.output, context, event, self) : undefined;
|
|
1531
1615
|
}
|
|
1532
1616
|
const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
|
|
1533
1617
|
function getChildren(stateNode) {
|
|
@@ -2143,7 +2227,7 @@ function microstepProcedure(transitions, currentState, mutConfiguration, event,
|
|
|
2143
2227
|
actions.push(...filteredTransitions.flatMap(t => t.actions));
|
|
2144
2228
|
|
|
2145
2229
|
// Enter states
|
|
2146
|
-
enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial);
|
|
2230
|
+
enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial, actorCtx);
|
|
2147
2231
|
const nextConfiguration = [...mutConfiguration];
|
|
2148
2232
|
const done = isInFinalState(nextConfiguration);
|
|
2149
2233
|
if (done) {
|
|
@@ -2152,7 +2236,7 @@ function microstepProcedure(transitions, currentState, mutConfiguration, event,
|
|
|
2152
2236
|
}
|
|
2153
2237
|
try {
|
|
2154
2238
|
const nextState = resolveActionsAndContext(actions, event, currentState, actorCtx);
|
|
2155
|
-
const output = done ? getOutput(nextConfiguration, nextState.context, event) : undefined;
|
|
2239
|
+
const output = done ? getOutput(nextConfiguration, nextState.context, event, actorCtx.self) : undefined;
|
|
2156
2240
|
internalQueue.push(...nextState._internalQueue);
|
|
2157
2241
|
return cloneState(currentState, {
|
|
2158
2242
|
configuration: nextConfiguration,
|
|
@@ -2169,7 +2253,7 @@ function microstepProcedure(transitions, currentState, mutConfiguration, event,
|
|
|
2169
2253
|
throw e;
|
|
2170
2254
|
}
|
|
2171
2255
|
}
|
|
2172
|
-
function enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial) {
|
|
2256
|
+
function enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial, actorContext) {
|
|
2173
2257
|
const statesToEnter = new Set();
|
|
2174
2258
|
const statesForDefaultEntry = new Set();
|
|
2175
2259
|
computeEntrySet(filteredTransitions, historyValue, statesForDefaultEntry, statesToEnter);
|
|
@@ -2197,7 +2281,7 @@ function enterStates(event, filteredTransitions, mutConfiguration, actions, inte
|
|
|
2197
2281
|
if (!parent.parent) {
|
|
2198
2282
|
continue;
|
|
2199
2283
|
}
|
|
2200
|
-
internalQueue.push(done(parent.id, stateNodeToEnter.output ? mapContext(stateNodeToEnter.output, currentState.context, event) : undefined));
|
|
2284
|
+
internalQueue.push(done(parent.id, stateNodeToEnter.output ? mapContext(stateNodeToEnter.output, currentState.context, event, actorContext.self) : undefined));
|
|
2201
2285
|
if (parent.parent) {
|
|
2202
2286
|
const grandparent = parent.parent;
|
|
2203
2287
|
if (grandparent.type === 'parallel') {
|
|
@@ -2517,6 +2601,7 @@ class State {
|
|
|
2517
2601
|
this.value = void 0;
|
|
2518
2602
|
this.done = void 0;
|
|
2519
2603
|
this.output = void 0;
|
|
2604
|
+
this.error = void 0;
|
|
2520
2605
|
this.context = void 0;
|
|
2521
2606
|
this.historyValue = {};
|
|
2522
2607
|
this._internalQueue = void 0;
|
|
@@ -2533,6 +2618,7 @@ class State {
|
|
|
2533
2618
|
this.tags = new Set(flatten(this.configuration.map(sn => sn.tags)));
|
|
2534
2619
|
this.done = config.done ?? false;
|
|
2535
2620
|
this.output = config.output;
|
|
2621
|
+
this.error = config.error;
|
|
2536
2622
|
}
|
|
2537
2623
|
|
|
2538
2624
|
/**
|
|
@@ -2745,7 +2831,7 @@ function createSpawner(actorContext, {
|
|
|
2745
2831
|
const input = 'input' in options ? options.input : referenced.input;
|
|
2746
2832
|
|
|
2747
2833
|
// TODO: this should also receive `src`
|
|
2748
|
-
const actor =
|
|
2834
|
+
const actor = createActor(referenced.src, {
|
|
2749
2835
|
id: options.id,
|
|
2750
2836
|
parent: actorContext.self,
|
|
2751
2837
|
input: typeof input === 'function' ? input({
|
|
@@ -2759,7 +2845,7 @@ function createSpawner(actorContext, {
|
|
|
2759
2845
|
return actor;
|
|
2760
2846
|
} else {
|
|
2761
2847
|
// TODO: this should also receive `src`
|
|
2762
|
-
return
|
|
2848
|
+
return createActor(src, {
|
|
2763
2849
|
id: options.id,
|
|
2764
2850
|
parent: actorContext.self,
|
|
2765
2851
|
input: options.input,
|
|
@@ -2982,4 +3068,4 @@ function createInitEvent(input) {
|
|
|
2982
3068
|
};
|
|
2983
3069
|
}
|
|
2984
3070
|
|
|
2985
|
-
export {
|
|
3071
|
+
export { fromObservable as $, microstep as A, isAtomicStateNode as B, isStateId as C, getStateNodeByPath as D, getPersistedState as E, resolveReferencedActor as F, createActor as G, matchesState as H, sendTo as I, sendParent as J, forwardTo as K, interpret as L, Actor as M, NULL_EVENT as N, ActorStatus as O, InterpreterStatus as P, doneInvoke as Q, cancel as R, STATE_DELIMITER as S, choose as T, log as U, pure as V, raise as W, stop as X, pathToStateValue as Y, toObserver as Z, fromPromise as _, toTransitionConfigArray as a, fromCallback as a0, fromEventObservable as a1, fromTransition as a2, stateIn as a3, not as a4, and as a5, or as a6, ConstantPrefix as a7, SpecialTargets as a8, startSignalType as a9, stopSignalType as aa, startSignal as ab, stopSignal as ac, isSignal as ad, isActorRef as ae, toActorRef as af, createEmptyActor as ag, toGuardDefinition as ah, constantPrefixes as ai, after as aj, done as ak, error as al, escalate as am, formatTransition as b, memo as c, flatten as d, evaluateGuard as e, formatTransitions as f, createInvokeId as g, getDelayedTransitions as h, formatInitialTransition as i, getCandidates as j, toInvokeConfig as k, getConfiguration as l, mapValues as m, getStateNodes as n, isInFinalState as o, State as p, isErrorEvent as q, resolveStateValue as r, cloneState as s, toArray as t, macrostep as u, transitionNode as v, getInitialConfiguration as w, resolveActionsAndContext as x, assign as y, createInitEvent as z };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MachineConfig, EventObject,
|
|
1
|
+
import { MachineConfig, EventObject, MachineContext, InternalMachineImplementations, ParameterizedObject, ProvidedActor, AnyEventObject } from "./types.js";
|
|
2
2
|
import { TypegenConstraint, TypegenDisabled, ResolveTypegenMeta } from "./typegenTypes.js";
|
|
3
3
|
import { StateMachine } from "./StateMachine.js";
|
|
4
|
-
export declare function createMachine<TContext extends MachineContext, TEvent extends EventObject = AnyEventObject, TActor extends ProvidedActor = ProvidedActor, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: MachineConfig<TContext, TEvent, ParameterizedObject, TActor, TTypesMeta>, implementations?: InternalMachineImplementations<TContext, TEvent, ParameterizedObject, TActor, ResolveTypegenMeta<TTypesMeta, TEvent, ParameterizedObject, TActor>>): StateMachine<TContext, TEvent, ParameterizedObject, TActor, ResolveTypegenMeta<TTypesMeta, TEvent, ParameterizedObject, TActor>>;
|
|
4
|
+
export declare function createMachine<TContext extends MachineContext, TEvent extends EventObject = AnyEventObject, TActor extends ProvidedActor = ProvidedActor, TInput = any, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: MachineConfig<TContext, TEvent, ParameterizedObject, TActor, TInput, TTypesMeta>, implementations?: InternalMachineImplementations<TContext, TEvent, ParameterizedObject, TActor, ResolveTypegenMeta<TTypesMeta, TEvent, ParameterizedObject, TActor>>): StateMachine<TContext, TEvent, ParameterizedObject, TActor, TInput, ResolveTypegenMeta<TTypesMeta, TEvent, ParameterizedObject, TActor>>;
|
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import type { StateNode } from "./StateNode.js";
|
|
2
2
|
import { TypegenDisabled, TypegenEnabled } from "./typegenTypes.js";
|
|
3
3
|
import type { ProvidedActor, ActorRefFrom, AnyState, AnyStateMachine, EventObject, HistoryValue, MachineContext, PersistedMachineState, Prop, StateConfig, StateValue, TODO, AnyActorRef, Compute } from "./types.js";
|
|
4
|
-
type ComputeConcreteChildren<TActor extends
|
|
5
|
-
[
|
|
6
|
-
id: K;
|
|
7
|
-
})['logic']>;
|
|
4
|
+
type ComputeConcreteChildren<TActor extends ProvidedActor> = {
|
|
5
|
+
[A in TActor as 'id' extends keyof A ? A['id'] & string : never]?: ActorRefFrom<A['logic']>;
|
|
8
6
|
};
|
|
9
|
-
type ComputeChildren<TActor extends ProvidedActor> = string extends TActor['src'] ? Record<string, AnyActorRef> : Compute<ComputeConcreteChildren<
|
|
10
|
-
id: string;
|
|
11
|
-
}>> & (undefined extends TActor['id'] ? {
|
|
7
|
+
type ComputeChildren<TActor extends ProvidedActor> = string extends TActor['src'] ? Record<string, AnyActorRef> : Compute<ComputeConcreteChildren<TActor> & (undefined extends TActor['id'] ? {
|
|
12
8
|
[id: string]: TActor extends any ? ActorRefFrom<TActor['logic']> | undefined : never;
|
|
13
9
|
} : {})>;
|
|
14
10
|
export declare function isStateConfig<TContext extends MachineContext, TEvent extends EventObject>(state: any): state is StateConfig<TContext, TEvent>;
|
|
@@ -28,6 +24,7 @@ export declare class State<TContext extends MachineContext, TEvent extends Event
|
|
|
28
24
|
* The done data of the top-level finite state.
|
|
29
25
|
*/
|
|
30
26
|
output: any;
|
|
27
|
+
error: unknown;
|
|
31
28
|
context: TContext;
|
|
32
29
|
historyValue: Readonly<HistoryValue<TContext, TEvent>>;
|
|
33
30
|
_internalQueue: Array<TEvent>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { State } from "./State.js";
|
|
2
2
|
import { StateNode } from "./StateNode.js";
|
|
3
3
|
import type { AreAllImplementationsAssumedToBeProvided, MarkAllImplementationsAsProvided, ResolveTypegenMeta, TypegenDisabled } from "./typegenTypes.js";
|
|
4
|
-
import type { ActorContext, ActorLogic, EventObject, InternalMachineImplementations, MachineConfig, MachineContext, MachineImplementationsSimplified, MachineTypes, NoInfer, StateConfig, StateMachineDefinition, StateValue, TransitionDefinition, PersistedMachineState, ParameterizedObject, AnyActorContext, ProvidedActor, Equals } from "./types.js";
|
|
4
|
+
import type { ActorContext, ActorLogic, EventObject, InternalMachineImplementations, MachineConfig, MachineContext, MachineImplementationsSimplified, MachineTypes, NoInfer, StateConfig, StateMachineDefinition, StateValue, TransitionDefinition, PersistedMachineState, ParameterizedObject, AnyActorContext, ProvidedActor, Equals, TODO } from "./types.js";
|
|
5
5
|
export declare const STATE_IDENTIFIER = "#";
|
|
6
6
|
export declare const WILDCARD = "*";
|
|
7
|
-
export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject
|
|
7
|
+
export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject, TAction extends ParameterizedObject, TActor extends ProvidedActor, TInput, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, TAction, TActor>> implements ActorLogic<TEvent, State<TContext, TEvent, TActor, TResolvedTypesMeta>, State<TContext, TEvent, TActor, TResolvedTypesMeta>, PersistedMachineState<State<TContext, TEvent, TActor, TResolvedTypesMeta>>, TODO, TInput, TODO> {
|
|
8
8
|
/**
|
|
9
9
|
* The raw config used to create the machine.
|
|
10
10
|
*/
|
|
@@ -14,7 +14,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
14
14
|
*/
|
|
15
15
|
version?: string;
|
|
16
16
|
implementations: MachineImplementationsSimplified<TContext, TEvent>;
|
|
17
|
-
types: MachineTypes<TContext, TEvent, TActor>;
|
|
17
|
+
types: MachineTypes<TContext, TEvent, TActor, TInput>;
|
|
18
18
|
__xstatenode: true;
|
|
19
19
|
idMap: Map<string, StateNode<TContext, TEvent>>;
|
|
20
20
|
root: StateNode<TContext, TEvent>;
|
|
@@ -35,7 +35,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
35
35
|
*
|
|
36
36
|
* @returns A new `StateMachine` instance with the provided implementations.
|
|
37
37
|
*/
|
|
38
|
-
provide(implementations: InternalMachineImplementations<TContext, TEvent, TAction, TActor, TResolvedTypesMeta, true>): StateMachine<TContext, TEvent, TAction, TActor, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta>;
|
|
38
|
+
provide(implementations: InternalMachineImplementations<TContext, TEvent, TAction, TActor, TResolvedTypesMeta, true>): StateMachine<TContext, TEvent, TAction, TActor, TInput, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta>;
|
|
39
39
|
/**
|
|
40
40
|
* Resolves the given `state` to a new `State` instance relative to this machine.
|
|
41
41
|
*
|
|
@@ -70,8 +70,8 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
70
70
|
/**
|
|
71
71
|
* Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
|
|
72
72
|
*/
|
|
73
|
-
getInitialState(actorCtx: ActorContext<TEvent, State<TContext, TEvent, TActor, TResolvedTypesMeta>>, input?:
|
|
74
|
-
start(state: State<TContext, TEvent, TActor, TResolvedTypesMeta
|
|
73
|
+
getInitialState(actorCtx: ActorContext<TEvent, State<TContext, TEvent, TActor, TResolvedTypesMeta>>, input?: TInput): State<TContext, TEvent, TActor, TResolvedTypesMeta>;
|
|
74
|
+
start(state: State<TContext, TEvent, TActor, TResolvedTypesMeta>): void;
|
|
75
75
|
getStateNodeById(stateId: string): StateNode<TContext, TEvent>;
|
|
76
76
|
get definition(): StateMachineDefinition<TContext, TEvent>;
|
|
77
77
|
toJSON(): StateMachineDefinition<TContext, TEvent>;
|
|
@@ -95,4 +95,5 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
|
|
|
95
95
|
__TActor: TActor;
|
|
96
96
|
/** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
97
97
|
__TResolvedTypesMeta: TResolvedTypesMeta;
|
|
98
|
+
__TInput: TInput;
|
|
98
99
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { State } from "./State.js";
|
|
2
2
|
import type { StateMachine } from "./StateMachine.js";
|
|
3
|
-
import type { Action, DelayedTransitionDefinition, EventObject, InitialTransitionDefinition, InvokeDefinition, MachineContext, Mapper,
|
|
3
|
+
import type { Action, DelayedTransitionDefinition, EventObject, InitialTransitionDefinition, InvokeDefinition, MachineContext, Mapper, StateNodeConfig, StateNodeDefinition, StateNodesConfig, TransitionDefinition, TransitionDefinitionMap, TODO } from "./types.js";
|
|
4
4
|
interface StateNodeOptions<TContext extends MachineContext, TEvent extends EventObject> {
|
|
5
5
|
_key: string;
|
|
6
6
|
_parent?: StateNode<TContext, TEvent>;
|
|
@@ -59,7 +59,7 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
|
|
|
59
59
|
/**
|
|
60
60
|
* The root machine node.
|
|
61
61
|
*/
|
|
62
|
-
machine: StateMachine<TContext, TEvent, any, any>;
|
|
62
|
+
machine: StateMachine<TContext, TEvent, any, any, TODO, TODO>;
|
|
63
63
|
/**
|
|
64
64
|
* The meta data associated with this state node, which will be returned in State instances.
|
|
65
65
|
*/
|
|
@@ -67,7 +67,7 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
|
|
|
67
67
|
/**
|
|
68
68
|
* The output data sent with the "done.state._id_" event if this is a final state node.
|
|
69
69
|
*/
|
|
70
|
-
output?: Mapper<TContext, TEvent, any
|
|
70
|
+
output?: Mapper<TContext, TEvent, any>;
|
|
71
71
|
/**
|
|
72
72
|
* The order this state node appears. Corresponds to the implicit document order.
|
|
73
73
|
*/
|
|
@@ -29,7 +29,7 @@ export declare function sendTo<TContext extends MachineContext, TExpressionEvent
|
|
|
29
29
|
(_: ActionArgs<TContext, TExpressionEvent>): void;
|
|
30
30
|
type: string;
|
|
31
31
|
to: string | TActor | ((args: UnifiedArg<TContext, TExpressionEvent>) => TActor | string);
|
|
32
|
-
event: ((TActor extends import("../types.js").AnyFunction ? ReturnType<TActor> : TActor) extends infer R ? R extends import("../StateMachine.js").StateMachine<infer _ extends MachineContext, infer TEvent_1 extends EventObject, infer __ extends import("../types.js").ParameterizedObject, infer ___ extends import("../types.js").ProvidedActor, infer ____> ? TEvent_1 : R extends import("../State.js").State<infer _TContext extends MachineContext, infer TEvent_2 extends EventObject, infer _TAction extends import("../types.js").ProvidedActor, infer _TActor> ? TEvent_2 : R extends ActorRef<infer TEvent_3 extends EventObject, infer __1> ? TEvent_3 : never : never) | SendExpr<TContext, TExpressionEvent, InferEvent<Cast<(TActor extends import("../types.js").AnyFunction ? ReturnType<TActor> : TActor) extends infer R ? R extends import("../StateMachine.js").StateMachine<infer _ extends MachineContext, infer TEvent_1 extends EventObject, infer __ extends import("../types.js").ParameterizedObject, infer ___ extends import("../types.js").ProvidedActor, infer ____> ? TEvent_1 : R extends import("../State.js").State<infer _TContext extends MachineContext, infer TEvent_2 extends EventObject, infer _TAction extends import("../types.js").ProvidedActor, infer _TActor> ? TEvent_2 : R extends ActorRef<infer TEvent_3 extends EventObject, infer __1> ? TEvent_3 : never : never, EventObject>>>;
|
|
32
|
+
event: ((TActor extends import("../types.js").AnyFunction ? ReturnType<TActor> : TActor) extends infer R ? R extends import("../StateMachine.js").StateMachine<infer _ extends MachineContext, infer TEvent_1 extends EventObject, infer __ extends import("../types.js").ParameterizedObject, infer ___ extends import("../types.js").ProvidedActor, infer ____, infer _____> ? TEvent_1 : R extends import("../State.js").State<infer _TContext extends MachineContext, infer TEvent_2 extends EventObject, infer _TAction extends import("../types.js").ProvidedActor, infer _TActor> ? TEvent_2 : R extends ActorRef<infer TEvent_3 extends EventObject, infer __1> ? TEvent_3 : never : never) | SendExpr<TContext, TExpressionEvent, InferEvent<Cast<(TActor extends import("../types.js").AnyFunction ? ReturnType<TActor> : TActor) extends infer R ? R extends import("../StateMachine.js").StateMachine<infer _ extends MachineContext, infer TEvent_1 extends EventObject, infer __ extends import("../types.js").ParameterizedObject, infer ___ extends import("../types.js").ProvidedActor, infer ____, infer _____> ? TEvent_1 : R extends import("../State.js").State<infer _TContext extends MachineContext, infer TEvent_2 extends EventObject, infer _TAction extends import("../types.js").ProvidedActor, infer _TActor> ? TEvent_2 : R extends ActorRef<infer TEvent_3 extends EventObject, infer __1> ? TEvent_3 : never : never, EventObject>>>;
|
|
33
33
|
id: string | undefined;
|
|
34
34
|
delay: string | number | DelayExpr<TContext, TExpressionEvent> | undefined;
|
|
35
35
|
resolve: typeof resolve;
|