ihsm 0.0.13 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +190 -6
  2. package/lib/cjs/index.d.ts +282 -0
  3. package/lib/cjs/index.js +331 -0
  4. package/lib/cjs/index.js.map +1 -0
  5. package/lib/cjs/internal/defs.private.d.ts +31 -0
  6. package/lib/cjs/internal/defs.private.js +3 -0
  7. package/lib/cjs/internal/defs.private.js.map +1 -0
  8. package/lib/cjs/internal/dispatch.debug.d.ts +4 -0
  9. package/lib/cjs/internal/dispatch.debug.js +330 -0
  10. package/lib/cjs/internal/dispatch.debug.js.map +1 -0
  11. package/lib/cjs/internal/dispatch.production.d.ts +6 -0
  12. package/lib/cjs/internal/dispatch.production.js +239 -0
  13. package/lib/cjs/internal/dispatch.production.js.map +1 -0
  14. package/lib/cjs/internal/dispatch.trace.d.ts +4 -0
  15. package/lib/cjs/internal/dispatch.trace.js +410 -0
  16. package/lib/cjs/internal/dispatch.trace.js.map +1 -0
  17. package/lib/cjs/internal/hsm.d.ts +54 -0
  18. package/lib/cjs/internal/hsm.js +182 -0
  19. package/lib/cjs/internal/hsm.js.map +1 -0
  20. package/lib/cjs/internal/utils.d.ts +18 -0
  21. package/lib/cjs/internal/utils.js +51 -0
  22. package/lib/cjs/internal/utils.js.map +1 -0
  23. package/lib/cjs/package.json +3 -0
  24. package/lib/esm/index.d.ts +282 -0
  25. package/lib/esm/index.js +314 -0
  26. package/lib/esm/index.js.map +1 -0
  27. package/lib/esm/internal/defs.private.d.ts +31 -0
  28. package/lib/esm/internal/defs.private.js +2 -0
  29. package/lib/esm/internal/defs.private.js.map +1 -0
  30. package/lib/esm/internal/dispatch.debug.d.ts +4 -0
  31. package/lib/esm/internal/dispatch.debug.js +326 -0
  32. package/lib/esm/internal/dispatch.debug.js.map +1 -0
  33. package/lib/esm/internal/dispatch.production.d.ts +6 -0
  34. package/lib/esm/internal/dispatch.production.js +235 -0
  35. package/lib/esm/internal/dispatch.production.js.map +1 -0
  36. package/lib/esm/internal/dispatch.trace.d.ts +4 -0
  37. package/lib/esm/internal/dispatch.trace.js +406 -0
  38. package/lib/esm/internal/dispatch.trace.js.map +1 -0
  39. package/lib/esm/internal/hsm.d.ts +54 -0
  40. package/lib/esm/internal/hsm.js +178 -0
  41. package/lib/esm/internal/hsm.js.map +1 -0
  42. package/lib/esm/internal/utils.d.ts +18 -0
  43. package/lib/esm/internal/utils.js +41 -0
  44. package/lib/esm/internal/utils.js.map +1 -0
  45. package/lib/esm/package.json +3 -0
  46. package/package.json +114 -66
  47. package/lib/index.browser.js +0 -523
  48. package/lib/index.d.ts +0 -105
  49. package/lib/index.js +0 -374
  50. package/tsconfig.json +0 -72
@@ -1,523 +0,0 @@
1
- var __extends = (this && this.__extends) || (function () {
2
- var extendStatics = function (d, b) {
3
- extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6
- return extendStatics(d, b);
7
- };
8
- return function (d, b) {
9
- extendStatics(d, b);
10
- function __() { this.constructor = d; }
11
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
12
- };
13
- })();
14
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
- return new (P || (P = Promise))(function (resolve, reject) {
17
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
- step((generator = generator.apply(thisArg, _arguments || [])).next());
21
- });
22
- };
23
- var __generator = (this && this.__generator) || function (thisArg, body) {
24
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
25
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
26
- function verb(n) { return function (v) { return step([n, v]); }; }
27
- function step(op) {
28
- if (f) throw new TypeError("Generator is already executing.");
29
- while (_) try {
30
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
31
- if (y = 0, t) op = [op[0] & 2, t.value];
32
- switch (op[0]) {
33
- case 0: case 1: t = op; break;
34
- case 4: _.label++; return { value: op[1], done: false };
35
- case 5: _.label++; y = op[1]; op = [0]; continue;
36
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
37
- default:
38
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
39
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
40
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
41
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
42
- if (t[2]) _.ops.pop();
43
- _.trys.pop(); continue;
44
- }
45
- op = body.call(thisArg, _);
46
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
47
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
48
- }
49
- };
50
- var __spreadArrays = (this && this.__spreadArrays) || function () {
51
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
52
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
53
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
54
- r[k] = a[j];
55
- return r;
56
- };
57
- export var LogLevel;
58
- (function (LogLevel) {
59
- LogLevel[LogLevel["ALL"] = 0] = "ALL";
60
- LogLevel[LogLevel["TRACE"] = 20] = "TRACE";
61
- LogLevel[LogLevel["DEBUG"] = 30] = "DEBUG";
62
- LogLevel[LogLevel["INFO"] = 30] = "INFO";
63
- LogLevel[LogLevel["WARN"] = 40] = "WARN";
64
- LogLevel[LogLevel["ERROR"] = 50] = "ERROR";
65
- LogLevel[LogLevel["FATAL"] = 60] = "FATAL";
66
- LogLevel[LogLevel["OFF"] = 70] = "OFF";
67
- })(LogLevel || (LogLevel = {}));
68
- var State = (function () {
69
- function State() {
70
- }
71
- State.prototype._init = function () {
72
- var _ = [];
73
- for (var _i = 0; _i < arguments.length; _i++) {
74
- _[_i] = arguments[_i];
75
- }
76
- };
77
- State.prototype._exit = function () { };
78
- State.prototype._entry = function () { };
79
- return State;
80
- }());
81
- export { State };
82
- export function createObject(topState, logLevel) {
83
- if (logLevel === void 0) { logLevel = LogLevel.INFO; }
84
- return create(topState, {}, logLevel);
85
- }
86
- export function create(topState, userData, logLevel) {
87
- if (logLevel === void 0) { logLevel = LogLevel.INFO; }
88
- var hsm = new Hsm(topState, userData, logLevel);
89
- var currState = topState;
90
- hsm.logTrace("[" + currState.name + "] #_init");
91
- while (true) {
92
- if (Object.prototype.hasOwnProperty.call(currState.prototype, '_init')) {
93
- currState.prototype['_init'].call(hsm, userData);
94
- hsm.logTrace(" [" + currState.name + "] init");
95
- }
96
- else {
97
- hsm.logTrace(" [" + currState.name + "] init?");
98
- }
99
- if (Object.prototype.hasOwnProperty.call(currState, 'initialState') && currState.initialState) {
100
- hsm.logTrace(" [" + currState.name + "] initialState found in '" + currState.name + "'");
101
- currState = currState.initialState;
102
- hsm.currentState = currState;
103
- }
104
- else {
105
- break;
106
- }
107
- }
108
- return hsm;
109
- }
110
- export function init(topState, logLevel, fieldName) {
111
- if (logLevel === void 0) { logLevel = LogLevel.INFO; }
112
- if (fieldName === void 0) { fieldName = 'hsm'; }
113
- return function (constructor) {
114
- return (function (_super) {
115
- __extends(class_1, _super);
116
- function class_1() {
117
- var _a;
118
- var args = [];
119
- for (var _i = 0; _i < arguments.length; _i++) {
120
- args[_i] = arguments[_i];
121
- }
122
- var _this = _super.apply(this, args) || this;
123
- var self = _this;
124
- var hsm = new Hsm(topState, _this, logLevel);
125
- Object.defineProperty(self, fieldName, {
126
- get: function () { return hsm; }, set: function (_) { throw new Error("Field " + fieldName + " is constant"); }
127
- });
128
- var currState = topState;
129
- hsm.logTrace("[" + currState.name + "] #_init");
130
- ++hsm.indent;
131
- while (true) {
132
- if (Object.prototype.hasOwnProperty.call(currState.prototype, '_init')) {
133
- (_a = currState.prototype['_init']).call.apply(_a, __spreadArrays([hsm], args));
134
- hsm.logTrace("[" + currState.name + "] init");
135
- }
136
- else {
137
- hsm.logTrace("[" + currState.name + "] init?");
138
- }
139
- if (Object.prototype.hasOwnProperty.call(currState, 'initialState') && currState.initialState) {
140
- hsm.logTrace("[" + currState.name + "] initial state is [" + currState.initialState.name + "]");
141
- currState = currState.initialState;
142
- hsm.currentState = currState;
143
- }
144
- else {
145
- break;
146
- }
147
- }
148
- hsm.logTrace("current state: [" + currState.name + "]");
149
- --hsm.indent;
150
- return _this;
151
- }
152
- return class_1;
153
- }(constructor));
154
- };
155
- }
156
- export function initialState() {
157
- return function (TargetState) {
158
- var ParentOfTargetState = Object.getPrototypeOf(TargetState.prototype).constructor;
159
- if (ParentOfTargetState.initialState) {
160
- throw new Error("@ihsm.initialState has been set twice for parent class \"" + ParentOfTargetState.name + "\"; check all classes that extend \"" + ParentOfTargetState.name + "\"");
161
- }
162
- TargetState.isInitialState = true;
163
- ParentOfTargetState.initialState = TargetState;
164
- };
165
- }
166
- var DebugTransition = (function () {
167
- function DebugTransition(exitList, entryList) {
168
- this.exitList = exitList;
169
- this.entryList = entryList;
170
- }
171
- DebugTransition.prototype.execute = function (hsm) {
172
- return __awaiter(this, void 0, void 0, function () {
173
- var exitLen, i, lastState_1, res, entryLen, lastState, i, stateProto, res;
174
- return __generator(this, function (_a) {
175
- switch (_a.label) {
176
- case 0:
177
- ++hsm.indent;
178
- exitLen = this.exitList.length;
179
- i = 0;
180
- _a.label = 1;
181
- case 1:
182
- if (!(i < exitLen)) return [3, 6];
183
- lastState_1 = this.exitList[i].prototype;
184
- if (!Object.prototype.hasOwnProperty.call(lastState_1, '_exit')) return [3, 4];
185
- res = lastState_1._exit.call(hsm);
186
- if (!Object.isPrototypeOf.call(hsm, Promise)) return [3, 3];
187
- return [4, res];
188
- case 2:
189
- res = _a.sent();
190
- _a.label = 3;
191
- case 3:
192
- hsm.logTrace("[" + this.exitList[i].name + "] exit");
193
- return [3, 5];
194
- case 4:
195
- hsm.logTrace("[" + this.exitList[i].name + "] exit?");
196
- _a.label = 5;
197
- case 5:
198
- ++i;
199
- return [3, 1];
200
- case 6:
201
- entryLen = this.entryList.length;
202
- lastState = hsm.currentState;
203
- i = 0;
204
- _a.label = 7;
205
- case 7:
206
- if (!(i < entryLen)) return [3, 12];
207
- lastState = this.entryList[i];
208
- stateProto = lastState.prototype;
209
- if (!Object.prototype.hasOwnProperty.call(stateProto, '_entry')) return [3, 10];
210
- res = stateProto._entry.call(hsm);
211
- if (!Object.isPrototypeOf.call(hsm, Promise)) return [3, 9];
212
- return [4, res];
213
- case 8:
214
- res = _a.sent();
215
- _a.label = 9;
216
- case 9:
217
- hsm.logTrace("[" + this.entryList[i].name + "] enter");
218
- return [3, 11];
219
- case 10:
220
- hsm.logTrace("[" + this.entryList[i].name + "] enter?");
221
- _a.label = 11;
222
- case 11:
223
- ++i;
224
- return [3, 7];
225
- case 12:
226
- hsm.currentState = lastState;
227
- --hsm.indent;
228
- return [2];
229
- }
230
- });
231
- });
232
- };
233
- return DebugTransition;
234
- }());
235
- var Hsm = (function () {
236
- function Hsm(UserTopState, userData, logLevel) {
237
- this.jobs = [];
238
- this.hsm = this;
239
- this.ctx = userData;
240
- this.topState = UserTopState;
241
- this.currentState = UserTopState;
242
- this.typeName = Object.getPrototypeOf(userData).constructor.name;
243
- this.name = this.typeName;
244
- this.logLevel = logLevel;
245
- this.log = console.log;
246
- this.transitionCache = new Map();
247
- this.jobs = [];
248
- this.isRunning = false;
249
- this.indent = 0;
250
- }
251
- Hsm.prototype.post = function (signal) {
252
- var payload = [];
253
- for (var _i = 1; _i < arguments.length; _i++) {
254
- payload[_i - 1] = arguments[_i];
255
- }
256
- this.pushTask(createSyncTask.apply(void 0, __spreadArrays([this, signal], payload)));
257
- };
258
- Hsm.prototype.send = function (signal) {
259
- var payload = [];
260
- for (var _i = 1; _i < arguments.length; _i++) {
261
- payload[_i - 1] = arguments[_i];
262
- }
263
- var self = this;
264
- return new Promise(function (resolve, reject) {
265
- self.pushTask(createAsyncTask.apply(void 0, __spreadArrays([self, resolve, reject, signal], payload)));
266
- });
267
- };
268
- Hsm.prototype.transition = function (nextState) {
269
- if (nextState === undefined) {
270
- throw new Error("Cannot transition to undefined");
271
- }
272
- this.nextState = nextState;
273
- };
274
- Hsm.prototype.unhandled = function () {
275
- throw new Error("Unhandled method");
276
- };
277
- Hsm.prototype.prepLine = function (msg) {
278
- var optionalParameters = [];
279
- for (var _i = 1; _i < arguments.length; _i++) {
280
- optionalParameters[_i - 1] = arguments[_i];
281
- }
282
- return this.typeName + " | " + ' '.repeat(this.indent * 4) + msg;
283
- };
284
- Hsm.prototype.logTrace = function (msg) {
285
- var optionalParameters = [];
286
- for (var _i = 1; _i < arguments.length; _i++) {
287
- optionalParameters[_i - 1] = arguments[_i];
288
- }
289
- if (this.logLevel <= LogLevel.TRACE) {
290
- if (typeof msg === "string") {
291
- this.log(this.prepLine(msg, optionalParameters));
292
- }
293
- else {
294
- this.log(msg);
295
- }
296
- }
297
- };
298
- Hsm.prototype.logDebug = function (msg) {
299
- var optionalParameters = [];
300
- for (var _i = 1; _i < arguments.length; _i++) {
301
- optionalParameters[_i - 1] = arguments[_i];
302
- }
303
- if (this.logLevel <= LogLevel.DEBUG) {
304
- if (typeof msg === "string") {
305
- this.log(this.prepLine(msg, optionalParameters));
306
- }
307
- else {
308
- this.log(msg);
309
- }
310
- }
311
- };
312
- Hsm.prototype.logWarn = function (msg) {
313
- var optionalParameters = [];
314
- for (var _i = 1; _i < arguments.length; _i++) {
315
- optionalParameters[_i - 1] = arguments[_i];
316
- }
317
- if (this.logLevel <= LogLevel.WARN) {
318
- if (typeof msg === "string") {
319
- this.log(this.prepLine(msg, optionalParameters));
320
- }
321
- else {
322
- this.log(msg);
323
- }
324
- }
325
- };
326
- Hsm.prototype.logInfo = function (msg) {
327
- var optionalParameters = [];
328
- for (var _i = 1; _i < arguments.length; _i++) {
329
- optionalParameters[_i - 1] = arguments[_i];
330
- }
331
- if (this.logLevel <= LogLevel.INFO) {
332
- if (typeof msg === "string") {
333
- this.log(this.prepLine(msg, optionalParameters));
334
- }
335
- else {
336
- this.log(msg);
337
- }
338
- }
339
- };
340
- Hsm.prototype.logError = function (msg) {
341
- var optionalParameters = [];
342
- for (var _i = 1; _i < arguments.length; _i++) {
343
- optionalParameters[_i - 1] = arguments[_i];
344
- }
345
- if (this.logLevel <= LogLevel.ERROR) {
346
- if (typeof msg === "string") {
347
- this.log(this.prepLine(msg, optionalParameters));
348
- }
349
- else {
350
- this.log(msg);
351
- }
352
- }
353
- };
354
- Hsm.prototype.logFatal = function (msg) {
355
- var optionalParameters = [];
356
- for (var _i = 1; _i < arguments.length; _i++) {
357
- optionalParameters[_i - 1] = arguments[_i];
358
- }
359
- if (typeof msg === "string") {
360
- this.log(this.prepLine(msg, optionalParameters));
361
- }
362
- else {
363
- this.log(msg);
364
- }
365
- };
366
- Hsm.prototype.wait = function (millis) {
367
- return new Promise(function (resolve) {
368
- setTimeout(function () { resolve(); }, millis);
369
- });
370
- };
371
- Hsm.prototype.pushTask = function (t) {
372
- this.jobs.push(t);
373
- if (this.isRunning) {
374
- return;
375
- }
376
- this.isRunning = true;
377
- this.dequeue();
378
- };
379
- Hsm.prototype.dequeue = function () {
380
- if (this.jobs.length == 0) {
381
- this.isRunning = false;
382
- return;
383
- }
384
- var task = this.jobs.shift();
385
- this.exec(task);
386
- };
387
- Hsm.prototype.exec = function (task) {
388
- var self = this;
389
- setTimeout(function () {
390
- Promise
391
- .resolve()
392
- .then(function () {
393
- return new Promise(function (resolve) { task(resolve); });
394
- })
395
- .then(function () {
396
- self.dequeue();
397
- });
398
- }, 0);
399
- };
400
- return Hsm;
401
- }());
402
- function getDebugTransition(srcState, destState, topState) {
403
- var src = srcState;
404
- var dst = destState;
405
- var srcPath = [];
406
- var end = topState;
407
- var srcIndex = new Map();
408
- var dstPath = [];
409
- var cur = src;
410
- var i = 0;
411
- while (cur !== end) {
412
- srcPath.push(cur);
413
- srcIndex.set(cur, i);
414
- cur = Object.getPrototypeOf(cur);
415
- ++i;
416
- }
417
- cur = dst;
418
- while (cur !== end) {
419
- var i_1 = srcIndex.get(cur);
420
- if (i_1 !== undefined) {
421
- srcPath = srcPath.slice(0, i_1);
422
- break;
423
- }
424
- dstPath.unshift(cur);
425
- cur = Object.getPrototypeOf(cur);
426
- }
427
- while (dst.hasOwnProperty('initialState')) {
428
- dst = dst.initialState;
429
- dstPath.push(dst);
430
- }
431
- return new DebugTransition(srcPath, dstPath);
432
- }
433
- function debugDispatch(hsm, signal) {
434
- var payload = [];
435
- for (var _i = 2; _i < arguments.length; _i++) {
436
- payload[_i - 2] = arguments[_i];
437
- }
438
- return __awaiter(this, void 0, void 0, function () {
439
- var messageHandler, result, output, destState, tr;
440
- return __generator(this, function (_a) {
441
- switch (_a.label) {
442
- case 0:
443
- hsm.logTrace("[" + hsm.currentState.name + "] #" + signal);
444
- ++hsm.indent;
445
- _a.label = 1;
446
- case 1:
447
- _a.trys.push([1, , 6, 7]);
448
- messageHandler = hsm.currentState.prototype[signal];
449
- if (!messageHandler) {
450
- throw {
451
- errorType: 'UnknownMessage',
452
- errorLevel: 'Application Error',
453
- errorMessage: "Cannot handle the #" + signal + " message",
454
- actorType: "" + hsm.name,
455
- currentState: "" + hsm.currentState.name
456
- };
457
- }
458
- result = messageHandler.call.apply(messageHandler, __spreadArrays([hsm], payload));
459
- output = undefined;
460
- if (!(result instanceof Promise)) return [3, 3];
461
- return [4, result];
462
- case 2:
463
- output = _a.sent();
464
- _a.label = 3;
465
- case 3:
466
- if (!(hsm.nextState != null)) return [3, 5];
467
- destState = hsm.nextState;
468
- hsm.logTrace("transition [" + hsm.currentState.name + "] => [" + destState.name + "]");
469
- tr = hsm.transitionCache.get([hsm.currentState, destState]);
470
- if (!tr) {
471
- tr = getDebugTransition(hsm.currentState, destState, hsm.topState);
472
- hsm.transitionCache.set([hsm.currentState, destState], tr);
473
- }
474
- return [4, tr.execute(hsm)];
475
- case 4:
476
- _a.sent();
477
- hsm.nextState = undefined;
478
- hsm.logTrace("current state: [" + hsm.currentState.name + "]");
479
- _a.label = 5;
480
- case 5:
481
- if (output !== undefined) {
482
- return [2, output];
483
- }
484
- else {
485
- return [2, undefined];
486
- }
487
- return [3, 7];
488
- case 6:
489
- --hsm.indent;
490
- return [7];
491
- case 7: return [2];
492
- }
493
- });
494
- });
495
- }
496
- function createAsyncTask(hsm, resolve, reject, signal) {
497
- var payload = [];
498
- for (var _i = 4; _i < arguments.length; _i++) {
499
- payload[_i - 4] = arguments[_i];
500
- }
501
- return function (doneCallback) {
502
- debugDispatch.apply(void 0, __spreadArrays([hsm, signal], payload)).then(function (result) { resolve(result); })
503
- .catch(function (err) { reject(err); })
504
- .finally(function () {
505
- doneCallback();
506
- });
507
- };
508
- }
509
- function createSyncTask(hsm, signal) {
510
- var payload = [];
511
- for (var _i = 2; _i < arguments.length; _i++) {
512
- payload[_i - 2] = arguments[_i];
513
- }
514
- return function (doneCallback) {
515
- debugDispatch.apply(void 0, __spreadArrays([hsm, signal], payload)).catch(function (err) {
516
- hsm.logError(err);
517
- })
518
- .finally(function () {
519
- doneCallback();
520
- });
521
- };
522
- }
523
- //# sourceMappingURL=index.js.map
package/lib/index.d.ts DELETED
@@ -1,105 +0,0 @@
1
- declare type SignalOf<Protocol extends {
2
- [key: string]: any;
3
- } | undefined, Signal extends keyof Protocol> = Protocol extends undefined ? string : Signal;
4
- declare type PayloadOf<Protocol extends {
5
- [key: string]: any;
6
- } | undefined, Signal extends keyof Protocol> = Protocol extends undefined ? any[] : Protocol[Signal] extends (...payload: infer Payload) => any ? Payload : never;
7
- declare type ReturnValueOf<Protocol extends {
8
- [key: string]: any;
9
- } | undefined, Signal extends keyof Protocol> = Protocol extends undefined ? any : Protocol[Signal] extends (...payload: any[]) => infer ReturnType ? ReturnType extends Promise<infer Value> ? Value : ReturnType : never;
10
- export declare type StateConstructor<UserData, Protocol extends {
11
- [key: string]: any;
12
- } | undefined> = Function & {
13
- initialState?: StateConstructor<UserData, Protocol>;
14
- exceptionState?: StateConstructor<UserData, Protocol>;
15
- isInitialState?: boolean;
16
- prototype: IState<UserData, Protocol>;
17
- };
18
- export interface IBaseHsm<UserData, Protocol extends {
19
- [key: string]: any;
20
- } | undefined> {
21
- logLevel: LogLevel;
22
- readonly name: string;
23
- readonly typeName: string;
24
- readonly currentState: StateConstructor<UserData, Protocol>;
25
- readonly topState: StateConstructor<UserData, Protocol>;
26
- post<Signal extends keyof Protocol>(signal: SignalOf<Protocol, Signal>, ...payload: PayloadOf<Protocol, Signal>): void;
27
- }
28
- export interface IHsm<UserData = {
29
- [key: string]: any;
30
- }, Protocol extends {
31
- [key: string]: any;
32
- } | undefined = undefined> extends IBaseHsm<UserData, Protocol> {
33
- readonly ctx: UserData;
34
- send<Signal extends keyof Protocol>(signal: SignalOf<Protocol, Signal>, ...payload: PayloadOf<Protocol, Signal>): Promise<ReturnValueOf<Protocol, Signal>>;
35
- }
36
- export interface IHsmDebug {
37
- logTrace(msg?: any, ...optionalParameters: any[]): void;
38
- logDebug(msg?: any, ...optionalParameters: any[]): void;
39
- logWarn(msg?: any, ...optionalParameters: any[]): void;
40
- logInfo(msg?: any, ...optionalParameters: any[]): void;
41
- logError(msg?: any, ...optionalParameters: any[]): void;
42
- logFatal(msg?: any, ...optionalParameters: any[]): void;
43
- }
44
- export interface IHsmHooks<UserData, Protocol extends {
45
- [key: string]: any;
46
- } | undefined> {
47
- preTransition(ctx: UserData, from: StateConstructor<UserData, Protocol>, to: StateConstructor<UserData, Protocol>): void;
48
- postTransition(ctx: UserData, from: StateConstructor<UserData, Protocol>, to: StateConstructor<UserData, Protocol>): void;
49
- preInit(ctx: UserData, args: any[], state: StateConstructor<UserData, Protocol>): void;
50
- postInit(ctx: UserData, args: any[], state: StateConstructor<UserData, Protocol>): void;
51
- preStateEntry(ctx: UserData, state: StateConstructor<UserData, Protocol>): void;
52
- postStateEntry(ctx: UserData, state: StateConstructor<UserData, Protocol>): void;
53
- preStateExit(ctx: UserData, state: StateConstructor<UserData, Protocol>): void;
54
- postStateExit(ctx: UserData, state: StateConstructor<UserData, Protocol>): void;
55
- preDispatch(ctx: UserData, state: StateConstructor<UserData, Protocol>): void;
56
- postDispatch(ctx: UserData, state: StateConstructor<UserData, Protocol>, result?: any, error?: Error): void;
57
- }
58
- export interface IBoundHsm<UserData, Protocol extends {
59
- [key: string]: any;
60
- } | undefined> extends IBaseHsm<UserData, Protocol>, IHsmDebug {
61
- transition(nextState: StateConstructor<UserData, Protocol>): void;
62
- unhandled(): never;
63
- wait(millis: number): Promise<void>;
64
- }
65
- export interface IState<UserData = {
66
- [key: string]: any;
67
- }, Protocol extends {
68
- [key: string]: any;
69
- } | undefined = undefined> {
70
- readonly ctx: UserData;
71
- readonly hsm: IBoundHsm<UserData, Protocol>;
72
- _init(...args: any[]): Promise<void> | void;
73
- _exit(): Promise<void> | void;
74
- _entry(): Promise<void> | void;
75
- }
76
- export declare enum LogLevel {
77
- ALL = 0,
78
- TRACE = 20,
79
- DEBUG = 30,
80
- INFO = 30,
81
- WARN = 40,
82
- ERROR = 50,
83
- FATAL = 60,
84
- OFF = 70
85
- }
86
- export declare class State<UserData = {
87
- [key: string]: any;
88
- }, Protocol extends {
89
- [key: string]: any;
90
- } | undefined = undefined> implements IState<UserData, Protocol> {
91
- readonly ctx: UserData;
92
- readonly hsm: IBoundHsm<UserData, Protocol>;
93
- _init(..._: any[]): Promise<void> | void;
94
- _exit(): Promise<void> | void;
95
- _entry(): Promise<void> | void;
96
- }
97
- export declare function createObject(topState: StateConstructor<{
98
- [key: string]: any;
99
- }, undefined>, logLevel?: LogLevel): IHsm<{
100
- [key: string]: any;
101
- }, undefined>;
102
- export declare function create<UserData, Protocol>(topState: StateConstructor<UserData, Protocol>, userData: UserData, logLevel?: LogLevel): IHsm<UserData, Protocol>;
103
- export declare function init<UserData, Protocol>(topState: StateConstructor<UserData, Protocol>, logLevel?: LogLevel, fieldName?: string): (constructor: new (...args: any[]) => any) => (new (...args: any[]) => any);
104
- export declare function initialState<UserData, Protocol>(): (state: StateConstructor<UserData, Protocol>) => void;
105
- export {};