ihsm 0.0.26 → 0.1.21
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/README.md +105 -113
- package/lib/cjs/index.d.ts +8 -1394
- package/lib/cjs/index.js +65 -764
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/internal/console-instrumentation.d.ts +34 -0
- package/lib/cjs/internal/console-instrumentation.js +71 -0
- package/lib/cjs/internal/console-instrumentation.js.map +1 -0
- package/lib/cjs/internal/identity.d.ts +16 -0
- package/lib/cjs/internal/identity.js +170 -0
- package/lib/cjs/internal/identity.js.map +1 -0
- package/lib/cjs/internal/instrumentation.d.ts +47 -0
- package/lib/cjs/internal/instrumentation.js +201 -0
- package/lib/cjs/internal/instrumentation.js.map +1 -0
- package/lib/cjs/internal/runtime.d.ts +376 -0
- package/lib/cjs/internal/runtime.js +2530 -0
- package/lib/cjs/internal/runtime.js.map +1 -0
- package/lib/cjs/internal/types.d.ts +515 -0
- package/lib/cjs/internal/types.js +9 -0
- package/lib/cjs/internal/types.js.map +1 -0
- package/lib/cjs/test-only.d.ts +5 -0
- package/lib/cjs/test-only.js +21 -0
- package/lib/cjs/test-only.js.map +1 -0
- package/lib/cjs/testing.d.ts +120 -88
- package/lib/cjs/testing.js +126 -38
- package/lib/cjs/testing.js.map +1 -1
- package/lib/cjs/transition-routines.d.ts +3 -0
- package/lib/cjs/transition-routines.js +11 -0
- package/lib/cjs/transition-routines.js.map +1 -0
- package/lib/cjs/types.d.ts +5 -0
- package/lib/cjs/{internal/defs.private.js → types.js} +1 -1
- package/lib/cjs/types.js.map +1 -0
- package/lib/esm/index.d.ts +8 -1394
- package/lib/esm/index.js +5 -742
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/internal/console-instrumentation.d.ts +34 -0
- package/lib/esm/internal/console-instrumentation.js +68 -0
- package/lib/esm/internal/console-instrumentation.js.map +1 -0
- package/lib/esm/internal/identity.d.ts +16 -0
- package/lib/esm/internal/identity.js +159 -0
- package/lib/esm/internal/identity.js.map +1 -0
- package/lib/esm/internal/instrumentation.d.ts +47 -0
- package/lib/esm/internal/instrumentation.js +178 -0
- package/lib/esm/internal/instrumentation.js.map +1 -0
- package/lib/esm/internal/runtime.d.ts +376 -0
- package/lib/esm/internal/runtime.js +2462 -0
- package/lib/esm/internal/runtime.js.map +1 -0
- package/lib/esm/internal/types.d.ts +515 -0
- package/lib/esm/internal/types.js +6 -0
- package/lib/esm/internal/types.js.map +1 -0
- package/lib/esm/test-only.d.ts +5 -0
- package/lib/esm/test-only.js +15 -0
- package/lib/esm/test-only.js.map +1 -0
- package/lib/esm/testing.d.ts +120 -88
- package/lib/esm/testing.js +125 -38
- package/lib/esm/testing.js.map +1 -1
- package/lib/esm/transition-routines.d.ts +3 -0
- package/lib/esm/transition-routines.js +3 -0
- package/lib/esm/transition-routines.js.map +1 -0
- package/lib/esm/types.d.ts +5 -0
- package/lib/esm/types.js +2 -0
- package/lib/esm/types.js.map +1 -0
- package/package.json +22 -4
- package/lib/cjs/internal/defs.private.d.ts +0 -41
- package/lib/cjs/internal/defs.private.js.map +0 -1
- package/lib/cjs/internal/dispatch.debug.d.ts +0 -4
- package/lib/cjs/internal/dispatch.debug.js +0 -332
- package/lib/cjs/internal/dispatch.debug.js.map +0 -1
- package/lib/cjs/internal/dispatch.production.d.ts +0 -6
- package/lib/cjs/internal/dispatch.production.js +0 -241
- package/lib/cjs/internal/dispatch.production.js.map +0 -1
- package/lib/cjs/internal/dispatch.trace.d.ts +0 -4
- package/lib/cjs/internal/dispatch.trace.js +0 -418
- package/lib/cjs/internal/dispatch.trace.js.map +0 -1
- package/lib/cjs/internal/hsm.d.ts +0 -60
- package/lib/cjs/internal/hsm.js +0 -215
- package/lib/cjs/internal/hsm.js.map +0 -1
- package/lib/cjs/internal/lookup.d.ts +0 -15
- package/lib/cjs/internal/lookup.js +0 -32
- package/lib/cjs/internal/lookup.js.map +0 -1
- package/lib/cjs/internal/utils.d.ts +0 -26
- package/lib/cjs/internal/utils.js +0 -63
- package/lib/cjs/internal/utils.js.map +0 -1
- package/lib/esm/internal/defs.private.d.ts +0 -41
- package/lib/esm/internal/defs.private.js +0 -2
- package/lib/esm/internal/defs.private.js.map +0 -1
- package/lib/esm/internal/dispatch.debug.d.ts +0 -4
- package/lib/esm/internal/dispatch.debug.js +0 -328
- package/lib/esm/internal/dispatch.debug.js.map +0 -1
- package/lib/esm/internal/dispatch.production.d.ts +0 -6
- package/lib/esm/internal/dispatch.production.js +0 -237
- package/lib/esm/internal/dispatch.production.js.map +0 -1
- package/lib/esm/internal/dispatch.trace.d.ts +0 -4
- package/lib/esm/internal/dispatch.trace.js +0 -414
- package/lib/esm/internal/dispatch.trace.js.map +0 -1
- package/lib/esm/internal/hsm.d.ts +0 -60
- package/lib/esm/internal/hsm.js +0 -211
- package/lib/esm/internal/hsm.js.map +0 -1
- package/lib/esm/internal/lookup.d.ts +0 -15
- package/lib/esm/internal/lookup.js +0 -29
- package/lib/esm/internal/lookup.js.map +0 -1
- package/lib/esm/internal/utils.d.ts +0 -26
- package/lib/esm/internal/utils.js +0 -52
- package/lib/esm/internal/utils.js.map +0 -1
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createInitTask = createInitTask;
|
|
4
|
-
exports.createEventDispatchTask = createEventDispatchTask;
|
|
5
|
-
const __1 = require("../");
|
|
6
|
-
const lookup_1 = require("./lookup");
|
|
7
|
-
const utils_1 = require("./utils");
|
|
8
|
-
class ProductionTransition {
|
|
9
|
-
exitList;
|
|
10
|
-
entryList;
|
|
11
|
-
finalState;
|
|
12
|
-
constructor(exitList, entryList, finalState) {
|
|
13
|
-
this.exitList = exitList;
|
|
14
|
-
this.entryList = entryList;
|
|
15
|
-
this.finalState = finalState;
|
|
16
|
-
}
|
|
17
|
-
async execute(hsm, srcState, dstState) {
|
|
18
|
-
// Execute exit
|
|
19
|
-
for (const state of this.exitList) {
|
|
20
|
-
try {
|
|
21
|
-
const res = state.prototype.onExit.call(hsm._instance);
|
|
22
|
-
if (res) {
|
|
23
|
-
await res;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
catch (cause) {
|
|
27
|
-
throw new __1.TransitionError(hsm, (0, utils_1.asError)(cause), (0, utils_1.getStateName)(state), 'onExit', (0, utils_1.getStateName)(srcState), (0, utils_1.getStateName)(dstState));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
// Execute entry
|
|
31
|
-
for (const state of this.entryList) {
|
|
32
|
-
try {
|
|
33
|
-
(0, utils_1.adoptStateBeforeOnEntry)(hsm, state);
|
|
34
|
-
const res = state.prototype.onEntry.call(hsm._instance);
|
|
35
|
-
if (res) {
|
|
36
|
-
await res;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
catch (cause) {
|
|
40
|
-
throw new __1.TransitionError(hsm, (0, utils_1.asError)(cause), (0, utils_1.getStateName)(state), 'onEntry', (0, utils_1.getStateName)(srcState), (0, utils_1.getStateName)(dstState));
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (this.finalState) {
|
|
44
|
-
hsm.currentState = this.finalState;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function createTransition(srcState, destState) {
|
|
49
|
-
const src = srcState;
|
|
50
|
-
let dst = destState;
|
|
51
|
-
let srcPath = [];
|
|
52
|
-
const end = __1.TopState;
|
|
53
|
-
const srcIndex = new Map();
|
|
54
|
-
let dstPath = [];
|
|
55
|
-
let cur = src;
|
|
56
|
-
let i = 0;
|
|
57
|
-
while (cur !== end) {
|
|
58
|
-
srcPath.push(cur);
|
|
59
|
-
srcIndex.set(cur, i);
|
|
60
|
-
cur = Object.getPrototypeOf(cur);
|
|
61
|
-
++i;
|
|
62
|
-
}
|
|
63
|
-
cur = dst;
|
|
64
|
-
while (cur !== end) {
|
|
65
|
-
const i = srcIndex.get(cur);
|
|
66
|
-
if (i !== undefined) {
|
|
67
|
-
srcPath = srcPath.slice(0, i);
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
dstPath.unshift(cur);
|
|
71
|
-
cur = Object.getPrototypeOf(cur);
|
|
72
|
-
}
|
|
73
|
-
while ((0, utils_1.hasInitialState)(dst)) {
|
|
74
|
-
dst = (0, utils_1.getInitialState)(dst);
|
|
75
|
-
dstPath.push(dst);
|
|
76
|
-
}
|
|
77
|
-
let finalState;
|
|
78
|
-
if (dstPath.length !== 0) {
|
|
79
|
-
finalState = dstPath[dstPath.length - 1];
|
|
80
|
-
}
|
|
81
|
-
else if (srcPath.length !== 0) {
|
|
82
|
-
finalState = Object.getPrototypeOf(srcPath[srcPath.length - 1]);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
finalState = undefined;
|
|
86
|
-
}
|
|
87
|
-
srcPath = srcPath.filter(value => !value.hasOwnProperty('onExit'));
|
|
88
|
-
dstPath = dstPath.filter(value => !value.hasOwnProperty('onEntry'));
|
|
89
|
-
return new ProductionTransition(srcPath, dstPath, finalState);
|
|
90
|
-
}
|
|
91
|
-
async function doTransition(hsm) {
|
|
92
|
-
if (hsm._transitionState) {
|
|
93
|
-
try {
|
|
94
|
-
const srcState = hsm.currentState;
|
|
95
|
-
const destState = hsm._transitionState;
|
|
96
|
-
const transitionKey = (0, utils_1.getTransitionKey)(srcState, destState);
|
|
97
|
-
let tr = hsm._transitionCache.get(transitionKey);
|
|
98
|
-
if (!tr) {
|
|
99
|
-
tr = createTransition(srcState, destState);
|
|
100
|
-
hsm._transitionCache.set(transitionKey, tr);
|
|
101
|
-
}
|
|
102
|
-
try {
|
|
103
|
-
await tr.execute(hsm, srcState, destState);
|
|
104
|
-
}
|
|
105
|
-
catch (transitionError) {
|
|
106
|
-
hsm.currentState = __1.FatalErrorState;
|
|
107
|
-
throw transitionError;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
finally {
|
|
111
|
-
hsm._transitionState = undefined;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
async function completePendingTransitions(hsm, onComplete) {
|
|
116
|
-
await doTransition(hsm);
|
|
117
|
-
onComplete();
|
|
118
|
-
}
|
|
119
|
-
async function doError(hsm, err, onComplete) {
|
|
120
|
-
hsm._transitionState = undefined; // clear next state
|
|
121
|
-
const messageHandler = hsm.currentState.prototype.onError;
|
|
122
|
-
try {
|
|
123
|
-
const result = messageHandler.call(hsm._instance, new __1.EventHandlerError(hsm, err));
|
|
124
|
-
if (result) {
|
|
125
|
-
await result;
|
|
126
|
-
}
|
|
127
|
-
await completePendingTransitions(hsm, onComplete);
|
|
128
|
-
}
|
|
129
|
-
catch (recoveryErr) {
|
|
130
|
-
if (recoveryErr instanceof __1.TransitionError) {
|
|
131
|
-
throw new __1.FatalError(hsm, recoveryErr);
|
|
132
|
-
}
|
|
133
|
-
const err = (0, utils_1.asError)(recoveryErr);
|
|
134
|
-
hsm.transition(__1.FatalErrorState);
|
|
135
|
-
await completePendingTransitions(hsm, onComplete);
|
|
136
|
-
throw new __1.FatalError(hsm, err);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
async function doUnhandledEvent(hsm, error, onComplete) {
|
|
140
|
-
try {
|
|
141
|
-
const result = hsm.currentState.prototype.onUnhandled.call(hsm._instance, error);
|
|
142
|
-
if (result) {
|
|
143
|
-
await result;
|
|
144
|
-
}
|
|
145
|
-
await completePendingTransitions(hsm, onComplete);
|
|
146
|
-
}
|
|
147
|
-
catch (recoveryErr) {
|
|
148
|
-
if (recoveryErr instanceof __1.TransitionError) {
|
|
149
|
-
hsm.currentState = __1.FatalErrorState;
|
|
150
|
-
throw recoveryErr;
|
|
151
|
-
}
|
|
152
|
-
await doError(hsm, (0, utils_1.asError)(recoveryErr), onComplete);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
async function executeInit(hsm) {
|
|
156
|
-
let currState = hsm.topState;
|
|
157
|
-
try {
|
|
158
|
-
while (true) {
|
|
159
|
-
(0, utils_1.adoptStateBeforeOnEntry)(hsm, currState);
|
|
160
|
-
const proto = currState.prototype;
|
|
161
|
-
if (proto.hasOwnProperty('onEntry')) {
|
|
162
|
-
proto.onEntry.call(hsm._instance);
|
|
163
|
-
}
|
|
164
|
-
if ((0, utils_1.hasInitialState)(currState)) {
|
|
165
|
-
currState = (0, utils_1.getInitialState)(currState);
|
|
166
|
-
}
|
|
167
|
-
else
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
catch (cause) {
|
|
172
|
-
if (cause instanceof __1.TransitionError) {
|
|
173
|
-
throw cause;
|
|
174
|
-
}
|
|
175
|
-
hsm.currentState = __1.FatalErrorState;
|
|
176
|
-
throw new __1.InitializationError(hsm, currState, (0, utils_1.asError)(cause));
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
function finishEventDispatch(hsm) {
|
|
180
|
-
hsm._currentEventName = undefined;
|
|
181
|
-
hsm._currentEventPayload = undefined;
|
|
182
|
-
}
|
|
183
|
-
async function dispatchEvent(hsm, eventName, ...eventPayload) {
|
|
184
|
-
hsm._currentEventName = String(eventName);
|
|
185
|
-
hsm._currentEventPayload = eventPayload;
|
|
186
|
-
try {
|
|
187
|
-
const eventHandler = (0, lookup_1.lookupEventHandler)(hsm, eventName);
|
|
188
|
-
if (!eventHandler) {
|
|
189
|
-
await doUnhandledEvent(hsm, new __1.UnhandledEventError(hsm), () => finishEventDispatch(hsm));
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
try {
|
|
193
|
-
const result = eventHandler.call(hsm._instance, ...eventPayload);
|
|
194
|
-
if (result)
|
|
195
|
-
await result;
|
|
196
|
-
await completePendingTransitions(hsm, () => finishEventDispatch(hsm));
|
|
197
|
-
}
|
|
198
|
-
catch (recoveryErr) {
|
|
199
|
-
if (recoveryErr instanceof __1.UnhandledEventError) {
|
|
200
|
-
await doUnhandledEvent(hsm, recoveryErr, () => finishEventDispatch(hsm));
|
|
201
|
-
}
|
|
202
|
-
else if (recoveryErr instanceof __1.TransitionError) {
|
|
203
|
-
finishEventDispatch(hsm);
|
|
204
|
-
throw recoveryErr;
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
await doError(hsm, (0, utils_1.asError)(recoveryErr), () => finishEventDispatch(hsm));
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
catch (err) {
|
|
212
|
-
finishEventDispatch(hsm);
|
|
213
|
-
throw err;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
// ---------------------------------------------------------------------------------------------------------------------
|
|
217
|
-
// Export: _createInitTask, _createEventDispatchTask
|
|
218
|
-
// ---------------------------------------------------------------------------------------------------------------------
|
|
219
|
-
/** @internal */
|
|
220
|
-
function createInitTask(hsm) {
|
|
221
|
-
return (done) => {
|
|
222
|
-
executeInit(hsm)
|
|
223
|
-
.then(async () => {
|
|
224
|
-
await doTransition(hsm);
|
|
225
|
-
done();
|
|
226
|
-
})
|
|
227
|
-
.catch((err) => {
|
|
228
|
-
hsm.dispatchErrorCallback(hsm, (0, utils_1.asError)(err));
|
|
229
|
-
done();
|
|
230
|
-
});
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
/** @internal */
|
|
234
|
-
function createEventDispatchTask(hsm, eventName, ...eventPayload) {
|
|
235
|
-
return (done) => {
|
|
236
|
-
dispatchEvent(hsm, eventName, ...eventPayload)
|
|
237
|
-
.catch((err) => hsm.dispatchErrorCallback(hsm, (0, utils_1.asError)(err)))
|
|
238
|
-
.finally(() => done());
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
//# sourceMappingURL=dispatch.production.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dispatch.production.js","sourceRoot":"","sources":["../../../src/internal/dispatch.production.ts"],"names":[],"mappings":";;AA0NA,wCAYC;AAGD,0DAMC;AA/OD,2BAAiL;AAGjL,qCAA8C;AAC9C,mCAA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CAAC;QACR,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACH,CAAC;AAED,gBAAgB;AAChB,SAAgB,uBAAuB,CAAqG,GAAsD,EAAE,SAAmD,EAAE,GAAG,YAAuD;IAClT,OAAO,CAAC,IAAkB,EAAQ,EAAE;QACnC,aAAa,CAAC,GAAG,EAAE,SAAS,EAAE,GAAG,YAAY,CAAC;aAC5C,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE,CAAC,GAAG,CAAC,qBAAqB,CAAC,GAAG,EAAE,IAAA,eAAO,EAAC,GAAG,CAAC,CAAC,CAAC;aACrE,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IACzB,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { PostedEvent, EventPayload } from '../';
|
|
2
|
-
import { HsmWithTracing, Task } from './defs.private';
|
|
3
|
-
export declare function createInitTask<DispatchContext, DispatchProtocol extends {} | undefined>(hsm: HsmWithTracing<DispatchContext, DispatchProtocol>): Task;
|
|
4
|
-
export declare function createEventDispatchTask<DispatchContext, DispatchProtocol extends {} | undefined, EventName extends keyof DispatchProtocol>(hsm: HsmWithTracing<DispatchContext, DispatchProtocol>, eventName: PostedEvent<DispatchProtocol, EventName>, ...eventPayload: EventPayload<DispatchProtocol, EventName>): Task;
|
|
@@ -1,418 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createInitTask = createInitTask;
|
|
4
|
-
exports.createEventDispatchTask = createEventDispatchTask;
|
|
5
|
-
const __1 = require("../");
|
|
6
|
-
const utils_1 = require("./utils");
|
|
7
|
-
function finishEventDispatch(hsm) {
|
|
8
|
-
hsm._traceWrite(`end event dispatch`);
|
|
9
|
-
hsm._currentEventName = undefined;
|
|
10
|
-
hsm._currentEventPayload = undefined;
|
|
11
|
-
}
|
|
12
|
-
async function completePendingTransitions(hsm, onComplete) {
|
|
13
|
-
await doTransition(hsm);
|
|
14
|
-
onComplete();
|
|
15
|
-
}
|
|
16
|
-
/** @internal */
|
|
17
|
-
class TraceTransition {
|
|
18
|
-
exitList;
|
|
19
|
-
entryList;
|
|
20
|
-
constructor(exitList, entryList) {
|
|
21
|
-
this.exitList = exitList;
|
|
22
|
-
this.entryList = entryList;
|
|
23
|
-
}
|
|
24
|
-
async execute(hsm, srcState, dstState) {
|
|
25
|
-
hsm._tracePush(`transition from ${(0, utils_1.getStateName)(srcState)} to ${(0, utils_1.getStateName)(dstState)}`, `started transition from ${(0, utils_1.getStateName)(srcState)} to ${(0, utils_1.getStateName)(dstState)} `);
|
|
26
|
-
for (const state of this.exitList) {
|
|
27
|
-
const statePrototype = state.prototype;
|
|
28
|
-
const stateName = (0, utils_1.getStateName)(state);
|
|
29
|
-
if (Object.prototype.hasOwnProperty.call(statePrototype, 'onExit')) {
|
|
30
|
-
try {
|
|
31
|
-
const res = statePrototype.onExit.call(hsm._instance);
|
|
32
|
-
if (res) {
|
|
33
|
-
await res;
|
|
34
|
-
}
|
|
35
|
-
hsm._traceWrite(`${stateName}.onExit() done`);
|
|
36
|
-
}
|
|
37
|
-
catch (cause) {
|
|
38
|
-
hsm._tracePopError(`${stateName}.onExit() has thrown ${(0, utils_1.quoteUnknown)(cause)}`);
|
|
39
|
-
throw new __1.TransitionError(hsm, (0, utils_1.asError)(cause), stateName, 'onExit', (0, utils_1.getStateName)(srcState), (0, utils_1.getStateName)(dstState));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
hsm._traceWrite(`${stateName}.onExit() skipped: default empty implementation`);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
for (const state of this.entryList) {
|
|
47
|
-
const statePrototype = state.prototype;
|
|
48
|
-
const stateName = (0, utils_1.getStateName)(state);
|
|
49
|
-
(0, utils_1.adoptStateBeforeOnEntry)(hsm, state);
|
|
50
|
-
if (Object.prototype.hasOwnProperty.call(statePrototype, 'onEntry')) {
|
|
51
|
-
try {
|
|
52
|
-
const res = statePrototype.onEntry.call(hsm._instance);
|
|
53
|
-
if (res) {
|
|
54
|
-
await res;
|
|
55
|
-
}
|
|
56
|
-
hsm._traceWrite(`${stateName}.onEntry() done`);
|
|
57
|
-
}
|
|
58
|
-
catch (cause) {
|
|
59
|
-
hsm._tracePopError(`${stateName}.onEntry() has thrown ${(0, utils_1.quoteUnknown)(cause)}`);
|
|
60
|
-
throw new __1.TransitionError(hsm, (0, utils_1.asError)(cause), (0, utils_1.getStateName)(state), 'onEntry', (0, utils_1.getStateName)(srcState), (0, utils_1.getStateName)(dstState));
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
hsm._traceWrite(`${stateName}.onEntry() skipped: default empty implementation`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
let newState;
|
|
68
|
-
if (this.entryList.length !== 0) {
|
|
69
|
-
newState = this.entryList[this.entryList.length - 1];
|
|
70
|
-
}
|
|
71
|
-
else if (this.exitList.length !== 0) {
|
|
72
|
-
newState = Object.getPrototypeOf(this.exitList[this.exitList.length - 1]);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
newState = hsm.currentState;
|
|
76
|
-
}
|
|
77
|
-
hsm._tracePopDone(`final state is ${(0, utils_1.getStateName)(newState)}`);
|
|
78
|
-
hsm.currentState = newState;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/** @internal */
|
|
82
|
-
function createTransition(srcState, destState) {
|
|
83
|
-
const src = srcState;
|
|
84
|
-
let dst = destState;
|
|
85
|
-
let srcPath = [];
|
|
86
|
-
const end = __1.TopState;
|
|
87
|
-
const srcIndex = new Map();
|
|
88
|
-
const dstPath = [];
|
|
89
|
-
let cur = src;
|
|
90
|
-
let i = 0;
|
|
91
|
-
while (cur !== end) {
|
|
92
|
-
srcPath.push(cur);
|
|
93
|
-
srcIndex.set(cur, i);
|
|
94
|
-
cur = Object.getPrototypeOf(cur);
|
|
95
|
-
++i;
|
|
96
|
-
}
|
|
97
|
-
cur = dst;
|
|
98
|
-
while (cur !== end) {
|
|
99
|
-
const i = srcIndex.get(cur);
|
|
100
|
-
if (i !== undefined) {
|
|
101
|
-
srcPath = srcPath.slice(0, i);
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
dstPath.unshift(cur);
|
|
105
|
-
cur = Object.getPrototypeOf(cur);
|
|
106
|
-
}
|
|
107
|
-
while ((0, utils_1.hasInitialState)(dst)) {
|
|
108
|
-
dst = (0, utils_1.getInitialState)(dst);
|
|
109
|
-
dstPath.push(dst);
|
|
110
|
-
}
|
|
111
|
-
return new TraceTransition(srcPath, dstPath);
|
|
112
|
-
}
|
|
113
|
-
/** @internal */
|
|
114
|
-
async function doTransition(hsm) {
|
|
115
|
-
if (hsm._transitionState) {
|
|
116
|
-
try {
|
|
117
|
-
const srcState = hsm.currentState;
|
|
118
|
-
const destState = hsm._transitionState;
|
|
119
|
-
hsm._traceWrite(`requested transition from ${(0, utils_1.getStateName)(srcState)} to ${(0, utils_1.getStateName)(destState)} `);
|
|
120
|
-
const transitionKey = (0, utils_1.getTransitionKey)(srcState, destState);
|
|
121
|
-
let tr = hsm._transitionCache.get(transitionKey);
|
|
122
|
-
if (tr) {
|
|
123
|
-
hsm._traceWrite(`transition cache hit for ${(0, utils_1.getStateName)(srcState)} to ${(0, utils_1.getStateName)(destState)} `);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
hsm._traceWrite(`transition cache miss for ${(0, utils_1.getStateName)(srcState)} to ${(0, utils_1.getStateName)(destState)} `);
|
|
127
|
-
tr = createTransition(srcState, destState);
|
|
128
|
-
hsm._transitionCache.set(transitionKey, tr);
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
await tr.execute(hsm, srcState, destState);
|
|
132
|
-
}
|
|
133
|
-
catch (transitionError) {
|
|
134
|
-
hsm.currentState = __1.FatalErrorState;
|
|
135
|
-
throw transitionError;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
finally {
|
|
139
|
-
hsm._transitionState = undefined;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
hsm._traceWrite('no transition requested');
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
/** @internal */
|
|
147
|
-
function lookupErrorHandler(hsm) {
|
|
148
|
-
hsm._tracePush(`lookup`, `started lookup of #onError event handler`);
|
|
149
|
-
let state = hsm.currentState;
|
|
150
|
-
while (state != __1.TopState) {
|
|
151
|
-
const prototype = state.prototype;
|
|
152
|
-
if (Object.prototype.hasOwnProperty.call(prototype, 'onError')) {
|
|
153
|
-
hsm._tracePopDone(`found in state ${(0, utils_1.getStateName)(state)}`);
|
|
154
|
-
return prototype['onError'];
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
hsm._traceWrite(`not found in state ${(0, utils_1.getStateName)(state)}`);
|
|
158
|
-
state = Object.getPrototypeOf(state);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
hsm._tracePopDone(`found in state ${(0, utils_1.getStateName)(__1.TopState)}`);
|
|
162
|
-
return __1.TopState.prototype.onError;
|
|
163
|
-
}
|
|
164
|
-
/** @internal */
|
|
165
|
-
async function doError(hsm, err, onComplete) {
|
|
166
|
-
hsm._transitionState = undefined;
|
|
167
|
-
hsm._tracePush(`error recovery`, `started error recovery`);
|
|
168
|
-
const messageHandler = lookupErrorHandler(hsm);
|
|
169
|
-
try {
|
|
170
|
-
hsm._tracePush('execute', 'started #onError handler execution');
|
|
171
|
-
const result = messageHandler.call(hsm._instance, new __1.EventHandlerError(hsm, err));
|
|
172
|
-
if (result) {
|
|
173
|
-
await result;
|
|
174
|
-
}
|
|
175
|
-
hsm._tracePopDone('error handler execution successful');
|
|
176
|
-
await completePendingTransitions(hsm, () => {
|
|
177
|
-
hsm._tracePopDone('error recovery successful');
|
|
178
|
-
onComplete();
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
catch (recoveryErr) {
|
|
182
|
-
hsm._tracePopError(`error handler execution failure: ${(0, utils_1.quoteUnknown)(recoveryErr)}`);
|
|
183
|
-
if (recoveryErr instanceof __1.TransitionError) {
|
|
184
|
-
hsm._tracePopError(`error recovery failure: ${(0, utils_1.quoteUnknown)(recoveryErr)}`);
|
|
185
|
-
throw recoveryErr;
|
|
186
|
-
}
|
|
187
|
-
const err = (0, utils_1.asError)(recoveryErr);
|
|
188
|
-
hsm.transition(__1.FatalErrorState);
|
|
189
|
-
await completePendingTransitions(hsm, () => {
|
|
190
|
-
hsm._tracePopError(`error recovery failure: ${(0, utils_1.quoteUnknown)(err)}`);
|
|
191
|
-
onComplete();
|
|
192
|
-
});
|
|
193
|
-
throw new __1.FatalError(hsm, err);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/** @internal */
|
|
197
|
-
function lookupUnhandled(hsm) {
|
|
198
|
-
let state = hsm.currentState;
|
|
199
|
-
hsm._tracePush(`lookup`, `started lookup of #onUnhandled event handler`);
|
|
200
|
-
while (true) {
|
|
201
|
-
const prototype = state.prototype;
|
|
202
|
-
if (Object.prototype.hasOwnProperty.call(prototype, 'onUnhandled')) {
|
|
203
|
-
hsm._tracePopDone(`found in state ${(0, utils_1.getStateName)(state)}`);
|
|
204
|
-
return prototype.onUnhandled;
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
hsm._traceWrite(`not found in state ${(0, utils_1.getStateName)(state)}`);
|
|
208
|
-
state = Object.getPrototypeOf(state);
|
|
209
|
-
if (state == __1.TopState) {
|
|
210
|
-
hsm._tracePopDone(`found in state ${(0, utils_1.getStateName)(state)}`);
|
|
211
|
-
return prototype.onUnhandled;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
/** @internal */
|
|
217
|
-
async function doUnhandledEvent(hsm, error, onComplete) {
|
|
218
|
-
hsm._tracePush('unhandled recovery', `started unhandled event recovery`);
|
|
219
|
-
const messageHandler = lookupUnhandled(hsm);
|
|
220
|
-
try {
|
|
221
|
-
hsm._tracePush('execute', 'started #onUnhandled handler execution');
|
|
222
|
-
const result = messageHandler.call(hsm._instance, error);
|
|
223
|
-
if (result) {
|
|
224
|
-
await result;
|
|
225
|
-
}
|
|
226
|
-
hsm._tracePopDone('unhandled handler execution successful');
|
|
227
|
-
await completePendingTransitions(hsm, () => {
|
|
228
|
-
hsm._tracePopDone('unhandled event recovery successful');
|
|
229
|
-
onComplete();
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
catch (recoveryErr) {
|
|
233
|
-
hsm._tracePopError(`unhandled event recovery failure: ${(0, utils_1.quoteUnknown)(recoveryErr)}`);
|
|
234
|
-
if (recoveryErr instanceof __1.TransitionError) {
|
|
235
|
-
hsm.currentState = __1.FatalErrorState;
|
|
236
|
-
hsm._tracePopError(`unhandled event recovery failure: ${(0, utils_1.quoteUnknown)(recoveryErr)}`);
|
|
237
|
-
throw recoveryErr;
|
|
238
|
-
}
|
|
239
|
-
try {
|
|
240
|
-
await doError(hsm, (0, utils_1.asError)(recoveryErr), () => {
|
|
241
|
-
hsm._tracePopDone('unhandled event recovery successful');
|
|
242
|
-
onComplete();
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
catch (nestedErr) {
|
|
246
|
-
hsm._tracePopError(`unhandled event recovery failure: ${(0, utils_1.quoteUnknown)(nestedErr)}`);
|
|
247
|
-
throw nestedErr;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* @internal
|
|
253
|
-
*
|
|
254
|
-
* Verbose-mode event-handler lookup. This narrates, step by step, the **same** canonical algorithm
|
|
255
|
-
* implemented (without tracing) by `lookupEventHandler` in `./lookup` and shared by the production /
|
|
256
|
-
* debug dispatchers (proposal T6): walk the constructor chain from `currentState` up to and
|
|
257
|
-
* including {@link TopState}, returning the first state that owns `eventName`. Keep the two in sync.
|
|
258
|
-
*/
|
|
259
|
-
function lookupEventHandler(hsm, eventName) {
|
|
260
|
-
const eventLabel = String(eventName);
|
|
261
|
-
let state = hsm.currentState;
|
|
262
|
-
hsm._tracePush(`lookup`, `started lookup of #${eventLabel} event handler`);
|
|
263
|
-
while (true) {
|
|
264
|
-
const prototype = state.prototype;
|
|
265
|
-
if (Object.prototype.hasOwnProperty.call(prototype, eventName)) {
|
|
266
|
-
hsm._tracePopDone(`#${eventLabel} found in state ${(0, utils_1.getStateName)(state)}`);
|
|
267
|
-
return prototype[eventName];
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
hsm._traceWrite(`not found in state ${(0, utils_1.getStateName)(state)}`);
|
|
271
|
-
if (state == __1.TopState)
|
|
272
|
-
break;
|
|
273
|
-
state = Object.getPrototypeOf(state);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
hsm._tracePopError(`not found in state ${hsm.currentStateName}`);
|
|
277
|
-
return undefined;
|
|
278
|
-
}
|
|
279
|
-
/** @internal */
|
|
280
|
-
async function executeInit(hsm) {
|
|
281
|
-
hsm._traceWrite('begin initialization');
|
|
282
|
-
try {
|
|
283
|
-
let currState = hsm.topState;
|
|
284
|
-
hsm._tracePush(`initialize`, `started initialization from ${(0, utils_1.getStateName)(hsm.topState)}`);
|
|
285
|
-
try {
|
|
286
|
-
while (true) {
|
|
287
|
-
(0, utils_1.adoptStateBeforeOnEntry)(hsm, currState);
|
|
288
|
-
if (Object.prototype.hasOwnProperty.call(currState.prototype, 'onEntry')) {
|
|
289
|
-
currState.prototype['onEntry'].call(hsm._instance);
|
|
290
|
-
hsm._traceWrite(`${(0, utils_1.getStateName)(currState)}.onEntry() done`);
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
hsm._traceWrite(`skip ${(0, utils_1.getStateName)(currState)}.onEntry(): default empty implementation`);
|
|
294
|
-
}
|
|
295
|
-
if ((0, utils_1.hasInitialState)(currState)) {
|
|
296
|
-
const newInitialState = (0, utils_1.getInitialState)(currState);
|
|
297
|
-
hsm._traceWrite(`${(0, utils_1.getStateName)(currState)} initial state is ${(0, utils_1.getStateName)(newInitialState)}`);
|
|
298
|
-
currState = newInitialState;
|
|
299
|
-
}
|
|
300
|
-
else {
|
|
301
|
-
hsm._traceWrite(`${(0, utils_1.getStateName)(currState)} has no initial state; final state is ${(0, utils_1.getStateName)(currState)}`);
|
|
302
|
-
break;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
hsm._tracePopDone(`final state is ${(0, utils_1.getStateName)(currState)}`);
|
|
306
|
-
}
|
|
307
|
-
catch (cause) {
|
|
308
|
-
if (cause instanceof __1.TransitionError) {
|
|
309
|
-
throw cause;
|
|
310
|
-
}
|
|
311
|
-
hsm._tracePopError(`initialization failed from top state '${(0, utils_1.getStateName)(hsm.topState)}' as ${(0, utils_1.getStateName)(currState)}.onEntry() handler has raised ${(0, utils_1.quoteUnknown)(cause)}; final state is ${(0, utils_1.getStateName)(__1.FatalErrorState)}`);
|
|
312
|
-
hsm.currentState = __1.FatalErrorState;
|
|
313
|
-
throw new __1.InitializationError(hsm, currState, (0, utils_1.asError)(cause));
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
finally {
|
|
317
|
-
hsm._traceWrite('end initialization');
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
/** @internal */
|
|
321
|
-
async function dispatchEvent(hsm, eventName, ...eventPayload) {
|
|
322
|
-
const eventLabel = String(eventName);
|
|
323
|
-
hsm._traceWrite(`begin event dispatch of #${eventLabel}`);
|
|
324
|
-
hsm._tracePush(`#${eventLabel}`, `started event dispatch`);
|
|
325
|
-
hsm._currentEventName = eventLabel;
|
|
326
|
-
hsm._currentEventPayload = eventPayload;
|
|
327
|
-
try {
|
|
328
|
-
const eventHandler = lookupEventHandler(hsm, eventName);
|
|
329
|
-
if (!eventHandler) {
|
|
330
|
-
hsm._traceWrite(`event #${eventLabel} is unhandled in state ${hsm.currentStateName}`);
|
|
331
|
-
try {
|
|
332
|
-
await doUnhandledEvent(hsm, new __1.UnhandledEventError(hsm), () => {
|
|
333
|
-
hsm._tracePopDone('event dispatch successful');
|
|
334
|
-
finishEventDispatch(hsm);
|
|
335
|
-
});
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
catch (recoveryErr) {
|
|
339
|
-
hsm._tracePopError(`event dispatch failed: ${(0, utils_1.quoteUnknown)(recoveryErr)}`);
|
|
340
|
-
finishEventDispatch(hsm);
|
|
341
|
-
throw recoveryErr;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
try {
|
|
345
|
-
hsm._tracePush('execute', 'started event handler execution');
|
|
346
|
-
const result = eventHandler.call(hsm._instance, ...eventPayload);
|
|
347
|
-
if (result) {
|
|
348
|
-
await result;
|
|
349
|
-
}
|
|
350
|
-
hsm._tracePopDone('event handler execution successful');
|
|
351
|
-
await completePendingTransitions(hsm, () => {
|
|
352
|
-
hsm._tracePopDone(`event dispatch successful`);
|
|
353
|
-
finishEventDispatch(hsm);
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
catch (recoveryErr) {
|
|
357
|
-
hsm._tracePopError((0, utils_1.quoteUnknown)(recoveryErr));
|
|
358
|
-
if (recoveryErr instanceof __1.UnhandledEventError) {
|
|
359
|
-
hsm._traceWrite(`event #${eventLabel} is unhandled in state ${hsm.currentStateName}`);
|
|
360
|
-
try {
|
|
361
|
-
await doUnhandledEvent(hsm, recoveryErr, () => {
|
|
362
|
-
hsm._tracePopDone('event dispatch successful');
|
|
363
|
-
finishEventDispatch(hsm);
|
|
364
|
-
});
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
catch (nestedErr) {
|
|
368
|
-
hsm._tracePopError(`event dispatch failed: ${(0, utils_1.quoteUnknown)(nestedErr)}`);
|
|
369
|
-
finishEventDispatch(hsm);
|
|
370
|
-
throw nestedErr;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
else if (recoveryErr instanceof __1.TransitionError) {
|
|
374
|
-
hsm._tracePopError(`event dispatch failed: ${(0, utils_1.quoteUnknown)(recoveryErr)}`);
|
|
375
|
-
finishEventDispatch(hsm);
|
|
376
|
-
throw recoveryErr;
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
try {
|
|
380
|
-
await doError(hsm, (0, utils_1.asError)(recoveryErr), () => {
|
|
381
|
-
hsm._tracePopDone('event dispatch successful');
|
|
382
|
-
finishEventDispatch(hsm);
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
catch (nestedErr) {
|
|
386
|
-
hsm._tracePopError(`event dispatch failed: ${(0, utils_1.quoteUnknown)(nestedErr)}`);
|
|
387
|
-
finishEventDispatch(hsm);
|
|
388
|
-
throw nestedErr;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
catch (err) {
|
|
394
|
-
finishEventDispatch(hsm);
|
|
395
|
-
throw err;
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
function createInitTask(hsm) {
|
|
399
|
-
return (done) => {
|
|
400
|
-
executeInit(hsm)
|
|
401
|
-
.then(async () => {
|
|
402
|
-
await doTransition(hsm);
|
|
403
|
-
done();
|
|
404
|
-
})
|
|
405
|
-
.catch((err) => {
|
|
406
|
-
hsm.dispatchErrorCallback(hsm, (0, utils_1.asError)(err));
|
|
407
|
-
done();
|
|
408
|
-
});
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
function createEventDispatchTask(hsm, eventName, ...eventPayload) {
|
|
412
|
-
return (done) => {
|
|
413
|
-
dispatchEvent(hsm, eventName, ...eventPayload)
|
|
414
|
-
.catch((err) => hsm.dispatchErrorCallback(hsm, (0, utils_1.asError)(err)))
|
|
415
|
-
.finally(() => done());
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
//# sourceMappingURL=dispatch.trace.js.map
|