@stateforward/hsm.ts 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -0
- package/dist/chunk-R6UCUJT5.js +307 -0
- package/dist/chunk-R6UCUJT5.js.map +1 -0
- package/dist/chunk-UO7RF4FF.js +132 -0
- package/dist/chunk-UO7RF4FF.js.map +1 -0
- package/dist/index.cjs +3015 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1013 -0
- package/dist/index.d.ts +1013 -0
- package/dist/index.js +2462 -0
- package/dist/index.js.map +1 -0
- package/dist/kind.cjs +162 -0
- package/dist/kind.cjs.map +1 -0
- package/dist/kind.d.cts +75 -0
- package/dist/kind.d.ts +75 -0
- package/dist/kind.js +21 -0
- package/dist/kind.js.map +1 -0
- package/dist/muid.cjs +345 -0
- package/dist/muid.cjs.map +1 -0
- package/dist/muid.d.cts +56 -0
- package/dist/muid.d.ts +56 -0
- package/dist/muid.js +37 -0
- package/dist/muid.js.map +1 -0
- package/package.json +68 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3015 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target2, all) => {
|
|
6
|
+
for (var name2 in all)
|
|
7
|
+
__defProp(target2, name2, { get: all[name2], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
Activity: () => Activity,
|
|
23
|
+
After: () => After,
|
|
24
|
+
At: () => At,
|
|
25
|
+
Attribute: () => Attribute,
|
|
26
|
+
AttributeKind: () => AttributeKind,
|
|
27
|
+
BehaviorKind: () => BehaviorKind,
|
|
28
|
+
Call: () => Call,
|
|
29
|
+
CallEventKind: () => CallEventKind,
|
|
30
|
+
ChangeEventKind: () => ChangeEventKind,
|
|
31
|
+
Choice: () => Choice,
|
|
32
|
+
ChoiceKind: () => ChoiceKind,
|
|
33
|
+
Clock: () => Clock,
|
|
34
|
+
CompletionEventKind: () => CompletionEventKind,
|
|
35
|
+
ConcurrentKind: () => ConcurrentKind,
|
|
36
|
+
ConstraintKind: () => ConstraintKind,
|
|
37
|
+
Context: () => Context,
|
|
38
|
+
DeepHistory: () => DeepHistory,
|
|
39
|
+
DeepHistoryKind: () => DeepHistoryKind,
|
|
40
|
+
DefaultClock: () => DefaultClock,
|
|
41
|
+
Defer: () => Defer,
|
|
42
|
+
Define: () => Define,
|
|
43
|
+
DispatchAll: () => DispatchAll,
|
|
44
|
+
DispatchTo: () => DispatchTo,
|
|
45
|
+
Effect: () => Effect,
|
|
46
|
+
ElementKind: () => ElementKind,
|
|
47
|
+
Entry: () => Entry,
|
|
48
|
+
ErrorEvent: () => ErrorEvent,
|
|
49
|
+
ErrorEventKind: () => ErrorEventKind,
|
|
50
|
+
Event: () => Event,
|
|
51
|
+
EventKind: () => EventKind,
|
|
52
|
+
Every: () => Every,
|
|
53
|
+
Exit: () => Exit,
|
|
54
|
+
ExternalKind: () => ExternalKind,
|
|
55
|
+
Final: () => Final,
|
|
56
|
+
FinalEvent: () => FinalEvent,
|
|
57
|
+
FinalStateKind: () => FinalStateKind,
|
|
58
|
+
Generator: () => Generator,
|
|
59
|
+
Get: () => Get,
|
|
60
|
+
Group: () => Group,
|
|
61
|
+
Guard: () => Guard,
|
|
62
|
+
ID: () => ID,
|
|
63
|
+
Initial: () => Initial,
|
|
64
|
+
InitialEvent: () => InitialEvent,
|
|
65
|
+
InitialKind: () => InitialKind,
|
|
66
|
+
Instance: () => Instance,
|
|
67
|
+
InternalKind: () => InternalKind,
|
|
68
|
+
IsAncestor: () => IsAncestor,
|
|
69
|
+
IsKind: () => isKind,
|
|
70
|
+
JunctionKind: () => JunctionKind,
|
|
71
|
+
Kinds: () => kinds,
|
|
72
|
+
LCA: () => LCA,
|
|
73
|
+
LocalKind: () => LocalKind,
|
|
74
|
+
MUID: () => MUID,
|
|
75
|
+
MakeGroup: () => MakeGroup,
|
|
76
|
+
MakeKind: () => makeKind,
|
|
77
|
+
ModelKind: () => ModelKind,
|
|
78
|
+
Name: () => Name,
|
|
79
|
+
NamespaceKind: () => NamespaceKind,
|
|
80
|
+
NullKind: () => NullKind,
|
|
81
|
+
On: () => On,
|
|
82
|
+
OnCall: () => OnCall,
|
|
83
|
+
OnSet: () => OnSet,
|
|
84
|
+
Operation: () => Operation,
|
|
85
|
+
PartialKind: () => PartialKind,
|
|
86
|
+
PseudostateKind: () => PseudostateKind,
|
|
87
|
+
QualifiedName: () => QualifiedName,
|
|
88
|
+
Queue: () => Queue,
|
|
89
|
+
Restart: () => Restart,
|
|
90
|
+
SelfKind: () => SelfKind,
|
|
91
|
+
SequentialKind: () => SequentialKind,
|
|
92
|
+
Set: () => Set,
|
|
93
|
+
ShallowHistory: () => ShallowHistory,
|
|
94
|
+
ShallowHistoryKind: () => ShallowHistoryKind,
|
|
95
|
+
ShardedGenerators: () => ShardedGenerators,
|
|
96
|
+
Source: () => Source,
|
|
97
|
+
State: () => State,
|
|
98
|
+
StateKind: () => StateKind,
|
|
99
|
+
StateMachineKind: () => StateMachineKind,
|
|
100
|
+
TakeSnapshot: () => TakeSnapshot,
|
|
101
|
+
Target: () => Target,
|
|
102
|
+
TimeEventKind: () => TimeEventKind,
|
|
103
|
+
Transition: () => Transition,
|
|
104
|
+
TransitionKind: () => TransitionKind,
|
|
105
|
+
VertexKind: () => VertexKind,
|
|
106
|
+
When: () => When,
|
|
107
|
+
activity: () => activity,
|
|
108
|
+
add64: () => add64,
|
|
109
|
+
after: () => after,
|
|
110
|
+
afterDispatch: () => afterDispatch,
|
|
111
|
+
afterEntry: () => afterEntry,
|
|
112
|
+
afterExecuted: () => afterExecuted,
|
|
113
|
+
afterExit: () => afterExit,
|
|
114
|
+
afterProcess: () => afterProcess,
|
|
115
|
+
and64: () => and64,
|
|
116
|
+
apply: () => apply,
|
|
117
|
+
at: () => at,
|
|
118
|
+
attribute: () => attribute,
|
|
119
|
+
call: () => call,
|
|
120
|
+
choice: () => choice,
|
|
121
|
+
clock: () => clock,
|
|
122
|
+
cmp64: () => cmp64,
|
|
123
|
+
deepHistory: () => deepHistory,
|
|
124
|
+
defer: () => defer,
|
|
125
|
+
define: () => define,
|
|
126
|
+
dirname: () => dirname,
|
|
127
|
+
dispatchAll: () => dispatchAll,
|
|
128
|
+
dispatchTo: () => dispatchTo,
|
|
129
|
+
effect: () => effect,
|
|
130
|
+
entry: () => entry,
|
|
131
|
+
event: () => event,
|
|
132
|
+
every: () => every,
|
|
133
|
+
exit: () => exit,
|
|
134
|
+
final: () => final,
|
|
135
|
+
find: () => find,
|
|
136
|
+
from32: () => from32,
|
|
137
|
+
get: () => get,
|
|
138
|
+
getDefaultConfig: () => getDefaultConfig,
|
|
139
|
+
guard: () => guard,
|
|
140
|
+
id: () => id,
|
|
141
|
+
initial: () => initial,
|
|
142
|
+
isAbsolute: () => isAbsolute,
|
|
143
|
+
isAncestor: () => isAncestor,
|
|
144
|
+
isKind: () => isKind,
|
|
145
|
+
join: () => join,
|
|
146
|
+
kinds: () => kinds,
|
|
147
|
+
lca: () => lca,
|
|
148
|
+
make: () => make,
|
|
149
|
+
make64: () => make64,
|
|
150
|
+
makeGroup: () => makeGroup,
|
|
151
|
+
makeKind: () => makeKind,
|
|
152
|
+
makeMuid: () => makeMuid,
|
|
153
|
+
name: () => name,
|
|
154
|
+
newGenerator: () => newGenerator,
|
|
155
|
+
on: () => on,
|
|
156
|
+
onCall: () => onCall,
|
|
157
|
+
onSet: () => onSet,
|
|
158
|
+
operation: () => operation,
|
|
159
|
+
or64: () => or64,
|
|
160
|
+
qualifiedName: () => qualifiedName,
|
|
161
|
+
restart: () => restart,
|
|
162
|
+
set: () => set,
|
|
163
|
+
shallowHistory: () => shallowHistory,
|
|
164
|
+
shl64: () => shl64,
|
|
165
|
+
shr64: () => shr64,
|
|
166
|
+
source: () => source,
|
|
167
|
+
start: () => start,
|
|
168
|
+
state: () => state,
|
|
169
|
+
stop: () => stop,
|
|
170
|
+
sub64: () => sub64,
|
|
171
|
+
takeSnapshot: () => takeSnapshot,
|
|
172
|
+
target: () => target,
|
|
173
|
+
transition: () => transition,
|
|
174
|
+
when: () => when
|
|
175
|
+
});
|
|
176
|
+
module.exports = __toCommonJS(index_exports);
|
|
177
|
+
|
|
178
|
+
// src/kind.ts
|
|
179
|
+
var length = 48;
|
|
180
|
+
var idLength = 8;
|
|
181
|
+
var depthMax = length / idLength;
|
|
182
|
+
var idMask = (1 << idLength) - 1;
|
|
183
|
+
var counter = 0;
|
|
184
|
+
var nextId = () => {
|
|
185
|
+
const id2 = counter & idMask;
|
|
186
|
+
counter += 1;
|
|
187
|
+
return id2;
|
|
188
|
+
};
|
|
189
|
+
var extractId = (kindValue, depth) => Math.floor(kindValue / Math.pow(2, idLength * depth)) & idMask;
|
|
190
|
+
function kind(...baseKinds) {
|
|
191
|
+
let result = nextId();
|
|
192
|
+
const used = {};
|
|
193
|
+
let usedCount = 0;
|
|
194
|
+
for (const base of baseKinds) {
|
|
195
|
+
for (let depth = 0; depth < depthMax; depth += 1) {
|
|
196
|
+
const baseId = extractId(base, depth);
|
|
197
|
+
if (baseId === 0) {
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
if (used[baseId]) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
used[baseId] = true;
|
|
204
|
+
usedCount += 1;
|
|
205
|
+
result += baseId * Math.pow(2, usedCount * idLength);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
function isKind(kindValue, ...baseKinds) {
|
|
211
|
+
for (const base of baseKinds) {
|
|
212
|
+
const baseId = base & idMask;
|
|
213
|
+
if (kindValue === baseId) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
for (let depth = 0; depth < depthMax; depth += 1) {
|
|
217
|
+
const currentId = extractId(kindValue, depth);
|
|
218
|
+
if (currentId === baseId) {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
var makeKind = (...baseKinds) => kind(...baseKinds);
|
|
226
|
+
var kinds = {
|
|
227
|
+
Null: makeKind(),
|
|
228
|
+
Element: makeKind(),
|
|
229
|
+
Partial: 0,
|
|
230
|
+
Vertex: 0,
|
|
231
|
+
Constraint: 0,
|
|
232
|
+
Behavior: 0,
|
|
233
|
+
Namespace: 0,
|
|
234
|
+
Concurrent: 0,
|
|
235
|
+
Sequential: 0,
|
|
236
|
+
StateMachine: 0,
|
|
237
|
+
Attribute: 0,
|
|
238
|
+
State: 0,
|
|
239
|
+
Model: 0,
|
|
240
|
+
Transition: 0,
|
|
241
|
+
Internal: 0,
|
|
242
|
+
External: 0,
|
|
243
|
+
Local: 0,
|
|
244
|
+
Self: 0,
|
|
245
|
+
Event: 0,
|
|
246
|
+
CompletionEvent: 0,
|
|
247
|
+
ChangeEvent: 0,
|
|
248
|
+
ErrorEvent: 0,
|
|
249
|
+
TimeEvent: 0,
|
|
250
|
+
CallEvent: 0,
|
|
251
|
+
Pseudostate: 0,
|
|
252
|
+
Initial: 0,
|
|
253
|
+
FinalState: 0,
|
|
254
|
+
Choice: 0,
|
|
255
|
+
Junction: 0,
|
|
256
|
+
DeepHistory: 0,
|
|
257
|
+
ShallowHistory: 0
|
|
258
|
+
};
|
|
259
|
+
kinds.Partial = makeKind(kinds.Element);
|
|
260
|
+
kinds.Vertex = makeKind(kinds.Element);
|
|
261
|
+
kinds.Constraint = makeKind(kinds.Element);
|
|
262
|
+
kinds.Behavior = makeKind(kinds.Element);
|
|
263
|
+
kinds.Namespace = makeKind(kinds.Element);
|
|
264
|
+
kinds.Concurrent = makeKind(kinds.Behavior);
|
|
265
|
+
kinds.Sequential = makeKind(kinds.Behavior);
|
|
266
|
+
kinds.StateMachine = makeKind(kinds.Concurrent, kinds.Namespace);
|
|
267
|
+
kinds.Attribute = makeKind(kinds.Element);
|
|
268
|
+
kinds.State = makeKind(kinds.Vertex, kinds.Namespace);
|
|
269
|
+
kinds.Model = makeKind(kinds.State);
|
|
270
|
+
kinds.Transition = makeKind(kinds.Element);
|
|
271
|
+
kinds.Internal = makeKind(kinds.Transition);
|
|
272
|
+
kinds.External = makeKind(kinds.Transition);
|
|
273
|
+
kinds.Local = makeKind(kinds.Transition);
|
|
274
|
+
kinds.Self = makeKind(kinds.Transition);
|
|
275
|
+
kinds.Event = makeKind(kinds.Element);
|
|
276
|
+
kinds.CompletionEvent = makeKind(kinds.Event);
|
|
277
|
+
kinds.ChangeEvent = makeKind(kinds.Event);
|
|
278
|
+
kinds.ErrorEvent = makeKind(kinds.CompletionEvent);
|
|
279
|
+
kinds.TimeEvent = makeKind(kinds.Event);
|
|
280
|
+
kinds.CallEvent = makeKind(kinds.Event);
|
|
281
|
+
kinds.Pseudostate = makeKind(kinds.Vertex);
|
|
282
|
+
kinds.Initial = makeKind(kinds.Pseudostate);
|
|
283
|
+
kinds.FinalState = makeKind(kinds.State);
|
|
284
|
+
kinds.Choice = makeKind(kinds.Pseudostate);
|
|
285
|
+
kinds.Junction = makeKind(kinds.Pseudostate);
|
|
286
|
+
kinds.DeepHistory = makeKind(kinds.Pseudostate);
|
|
287
|
+
kinds.ShallowHistory = makeKind(kinds.Pseudostate);
|
|
288
|
+
|
|
289
|
+
// src/hsm.ts
|
|
290
|
+
function attachSpec(builder, spec) {
|
|
291
|
+
return Object.assign(builder, {
|
|
292
|
+
__hsmSpec: spec
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
var runtimeByInstance = /* @__PURE__ */ new WeakMap();
|
|
296
|
+
function bindRuntime(instance, runtime) {
|
|
297
|
+
runtimeByInstance.set(instance, runtime);
|
|
298
|
+
}
|
|
299
|
+
function runtimeFor(instance) {
|
|
300
|
+
if (!instance || typeof instance !== "object") {
|
|
301
|
+
return void 0;
|
|
302
|
+
}
|
|
303
|
+
return runtimeByInstance.get(instance);
|
|
304
|
+
}
|
|
305
|
+
function join(...segments) {
|
|
306
|
+
var parts = [];
|
|
307
|
+
var currentIsAbsolute = false;
|
|
308
|
+
for (var i = 0; i < segments.length; i++) {
|
|
309
|
+
var segment = segments[i];
|
|
310
|
+
if (segment === null || segment === void 0 || segment.length === 0) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (segment[0] === "/") {
|
|
314
|
+
parts = [];
|
|
315
|
+
currentIsAbsolute = true;
|
|
316
|
+
}
|
|
317
|
+
var startIndex = 0;
|
|
318
|
+
var currentPartEnd = 0;
|
|
319
|
+
var part = "";
|
|
320
|
+
while (currentPartEnd < segment.length) {
|
|
321
|
+
if (segment[currentPartEnd] === "/") {
|
|
322
|
+
if (currentPartEnd > startIndex) {
|
|
323
|
+
part = segment.substring(startIndex, currentPartEnd);
|
|
324
|
+
if (part === "..") {
|
|
325
|
+
if (currentIsAbsolute && parts.length === 0) {
|
|
326
|
+
} else if (parts.length > 0 && parts[parts.length - 1] !== "..") {
|
|
327
|
+
parts.pop();
|
|
328
|
+
} else {
|
|
329
|
+
parts.push(part);
|
|
330
|
+
}
|
|
331
|
+
} else if (part !== ".") {
|
|
332
|
+
parts.push(part);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
startIndex = currentPartEnd + 1;
|
|
336
|
+
}
|
|
337
|
+
currentPartEnd++;
|
|
338
|
+
}
|
|
339
|
+
if (currentPartEnd > startIndex) {
|
|
340
|
+
part = segment.substring(startIndex, currentPartEnd);
|
|
341
|
+
if (part === "..") {
|
|
342
|
+
if (currentIsAbsolute && parts.length === 0) {
|
|
343
|
+
} else if (parts.length > 0 && parts[parts.length - 1] !== "..") {
|
|
344
|
+
parts.pop();
|
|
345
|
+
} else {
|
|
346
|
+
parts.push(part);
|
|
347
|
+
}
|
|
348
|
+
} else if (part !== ".") {
|
|
349
|
+
parts.push(part);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
var joinedPath = parts.join("/");
|
|
354
|
+
if (currentIsAbsolute) {
|
|
355
|
+
joinedPath = "/" + joinedPath;
|
|
356
|
+
}
|
|
357
|
+
var hasTrailingSlash = false;
|
|
358
|
+
if (segments.length > 0) {
|
|
359
|
+
var lastInputSegment = segments[segments.length - 1];
|
|
360
|
+
if (lastInputSegment && lastInputSegment.length > 0 && lastInputSegment[lastInputSegment.length - 1] === "/") {
|
|
361
|
+
hasTrailingSlash = true;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (joinedPath.length === 0) {
|
|
365
|
+
return currentIsAbsolute ? "/" : ".";
|
|
366
|
+
} else if (hasTrailingSlash && joinedPath !== "/") {
|
|
367
|
+
return joinedPath + "/";
|
|
368
|
+
}
|
|
369
|
+
return joinedPath;
|
|
370
|
+
}
|
|
371
|
+
function slice(args, start2 = 0) {
|
|
372
|
+
var result = [];
|
|
373
|
+
for (var i = start2; i < args.length; i++) {
|
|
374
|
+
result.push(args[i]);
|
|
375
|
+
}
|
|
376
|
+
return result;
|
|
377
|
+
}
|
|
378
|
+
function dirname(path) {
|
|
379
|
+
if (path === void 0 || path === null || path.length === 0) {
|
|
380
|
+
return ".";
|
|
381
|
+
}
|
|
382
|
+
var originalPathWasAbsolute = path[0] === "/";
|
|
383
|
+
var i = path.length - 1;
|
|
384
|
+
while (i >= 0 && path[i] === "/") {
|
|
385
|
+
i--;
|
|
386
|
+
}
|
|
387
|
+
var p = path.substring(0, i + 1);
|
|
388
|
+
if (p.length === 0) {
|
|
389
|
+
return originalPathWasAbsolute ? "/" : ".";
|
|
390
|
+
}
|
|
391
|
+
var lastSeparatorIndex = p.lastIndexOf("/");
|
|
392
|
+
if (lastSeparatorIndex < 0) {
|
|
393
|
+
return originalPathWasAbsolute ? "/" : ".";
|
|
394
|
+
}
|
|
395
|
+
var result = p.substring(0, lastSeparatorIndex);
|
|
396
|
+
if (result.length === 0) {
|
|
397
|
+
return originalPathWasAbsolute ? "/" : ".";
|
|
398
|
+
}
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
function isAbsolute(path) {
|
|
402
|
+
var c = path.charAt(0);
|
|
403
|
+
if (c === "/") return true;
|
|
404
|
+
return path.length > 2 && path.charAt(1) === ":" && path.charAt(2) === "/";
|
|
405
|
+
}
|
|
406
|
+
var Context = class {
|
|
407
|
+
listeners;
|
|
408
|
+
instances;
|
|
409
|
+
done;
|
|
410
|
+
constructor() {
|
|
411
|
+
this.listeners = [];
|
|
412
|
+
this.instances = {};
|
|
413
|
+
this.done = false;
|
|
414
|
+
}
|
|
415
|
+
addEventListener(_type, listener) {
|
|
416
|
+
this.listeners.push(listener);
|
|
417
|
+
}
|
|
418
|
+
removeEventListener(_type, listener) {
|
|
419
|
+
var index = this.listeners.indexOf(listener);
|
|
420
|
+
if (index !== -1) {
|
|
421
|
+
this.listeners.splice(index, 1);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
var InitialEvent = {
|
|
426
|
+
kind: kinds.CompletionEvent,
|
|
427
|
+
qualifiedName: "hsm_initial",
|
|
428
|
+
name: "hsm_initial"
|
|
429
|
+
};
|
|
430
|
+
var FinalEvent = {
|
|
431
|
+
name: "hsm_final",
|
|
432
|
+
kind: kinds.CompletionEvent
|
|
433
|
+
};
|
|
434
|
+
var ErrorEvent = {
|
|
435
|
+
name: "hsm_error",
|
|
436
|
+
kind: kinds.ErrorEvent
|
|
437
|
+
};
|
|
438
|
+
var DefaultClock = {
|
|
439
|
+
setTimeout: typeof setTimeout === "function" ? ((callback, timeout, ...args) => setTimeout(callback, timeout || 0, ...args)) : function(_callback, _timeout, ..._args) {
|
|
440
|
+
return 0;
|
|
441
|
+
},
|
|
442
|
+
clearTimeout: typeof clearTimeout === "function" ? ((id2) => {
|
|
443
|
+
if (id2 !== void 0) {
|
|
444
|
+
clearTimeout(id2);
|
|
445
|
+
}
|
|
446
|
+
}) : function() {
|
|
447
|
+
},
|
|
448
|
+
now: typeof Date !== "undefined" && typeof Date.now === "function" ? Date.now : function() {
|
|
449
|
+
return 0;
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
function event(name2, schema) {
|
|
453
|
+
return {
|
|
454
|
+
kind: kinds.Event,
|
|
455
|
+
name: name2,
|
|
456
|
+
schema
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
function apply(model, stack, partials) {
|
|
460
|
+
for (var i = 0; i < partials.length; i++) {
|
|
461
|
+
var partial = partials[i];
|
|
462
|
+
if (typeof partial === "function") {
|
|
463
|
+
partial(model, stack);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function qualifyModelName(model, name2) {
|
|
468
|
+
if (isAbsolute(name2)) {
|
|
469
|
+
return isAncestor(model.qualifiedName, name2) || model.qualifiedName === name2 ? name2 : join(model.qualifiedName, name2.slice(1));
|
|
470
|
+
}
|
|
471
|
+
return join(model.qualifiedName, name2);
|
|
472
|
+
}
|
|
473
|
+
function registerEvent(model, event2) {
|
|
474
|
+
if (!model.events) {
|
|
475
|
+
model.events = {};
|
|
476
|
+
}
|
|
477
|
+
var existing = model.events[event2.name];
|
|
478
|
+
if (!existing) {
|
|
479
|
+
model.events[event2.name] = event2;
|
|
480
|
+
return event2;
|
|
481
|
+
}
|
|
482
|
+
return existing;
|
|
483
|
+
}
|
|
484
|
+
function resolveOperation(model, value) {
|
|
485
|
+
return function(ctx, instance, event2) {
|
|
486
|
+
var runtime = runtimeFor(instance);
|
|
487
|
+
if (!runtime) {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
var name2 = qualifyModelName(model, value);
|
|
491
|
+
return !!runtime.invoke(name2, ctx, [event2]);
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
function hasThen(value) {
|
|
495
|
+
return !!value && typeof value.then === "function";
|
|
496
|
+
}
|
|
497
|
+
function hasAddEventListener(value) {
|
|
498
|
+
return !!value && typeof value.addEventListener === "function";
|
|
499
|
+
}
|
|
500
|
+
function hasOn(value) {
|
|
501
|
+
return !!value && typeof value.on === "function";
|
|
502
|
+
}
|
|
503
|
+
function toSignalLike(value) {
|
|
504
|
+
if (hasThen(value)) {
|
|
505
|
+
return value;
|
|
506
|
+
}
|
|
507
|
+
if (hasAddEventListener(value) || hasOn(value)) {
|
|
508
|
+
return value;
|
|
509
|
+
}
|
|
510
|
+
return void 0;
|
|
511
|
+
}
|
|
512
|
+
function coerceDuration(value) {
|
|
513
|
+
var numericValue = Number(value);
|
|
514
|
+
return isNaN(numericValue) ? 0 : numericValue;
|
|
515
|
+
}
|
|
516
|
+
function coerceTimepoint(value) {
|
|
517
|
+
if (value instanceof Date) {
|
|
518
|
+
return value.getTime();
|
|
519
|
+
}
|
|
520
|
+
return coerceDuration(value);
|
|
521
|
+
}
|
|
522
|
+
function withThenable(value, callback) {
|
|
523
|
+
if (hasThen(value)) {
|
|
524
|
+
return Promise.resolve(value).then(callback);
|
|
525
|
+
}
|
|
526
|
+
return callback(value);
|
|
527
|
+
}
|
|
528
|
+
function getState(model, path) {
|
|
529
|
+
if (!path) {
|
|
530
|
+
return void 0;
|
|
531
|
+
}
|
|
532
|
+
var member = model.members[path];
|
|
533
|
+
return isStateElement(member) ? member : void 0;
|
|
534
|
+
}
|
|
535
|
+
function getVertex(model, path) {
|
|
536
|
+
if (!path) {
|
|
537
|
+
return void 0;
|
|
538
|
+
}
|
|
539
|
+
var member = model.members[path];
|
|
540
|
+
return isVertex(member) ? member : void 0;
|
|
541
|
+
}
|
|
542
|
+
function getTransition(model, path) {
|
|
543
|
+
if (!path) {
|
|
544
|
+
return void 0;
|
|
545
|
+
}
|
|
546
|
+
var member = model.members[path];
|
|
547
|
+
return isTransitionElement(member) ? member : void 0;
|
|
548
|
+
}
|
|
549
|
+
function getBehavior(model, path) {
|
|
550
|
+
if (!path) {
|
|
551
|
+
return void 0;
|
|
552
|
+
}
|
|
553
|
+
var member = model.members[path];
|
|
554
|
+
return isBehaviorElement(member) ? member : void 0;
|
|
555
|
+
}
|
|
556
|
+
function getConstraint(model, path) {
|
|
557
|
+
if (!path) {
|
|
558
|
+
return void 0;
|
|
559
|
+
}
|
|
560
|
+
var member = model.members[path];
|
|
561
|
+
return isConstraintElement(member) ? member : void 0;
|
|
562
|
+
}
|
|
563
|
+
function isAncestor(ancestor, descendant) {
|
|
564
|
+
if (ancestor === descendant) return false;
|
|
565
|
+
if (ancestor === "/") return isAbsolute(descendant);
|
|
566
|
+
return descendant.startsWith(ancestor + "/");
|
|
567
|
+
}
|
|
568
|
+
function lca(a, b) {
|
|
569
|
+
if (a === b) return dirname(a);
|
|
570
|
+
if (!a) return b;
|
|
571
|
+
if (!b) return a;
|
|
572
|
+
if (dirname(a) === dirname(b)) return dirname(a);
|
|
573
|
+
if (isAncestor(a, b)) return a;
|
|
574
|
+
if (isAncestor(b, a)) return b;
|
|
575
|
+
return lca(dirname(a), dirname(b));
|
|
576
|
+
}
|
|
577
|
+
function isStateElement(element) {
|
|
578
|
+
return !!element && isKind(element.kind, kinds.State);
|
|
579
|
+
}
|
|
580
|
+
function isModelElement(element) {
|
|
581
|
+
return !!element && isKind(element.kind, kinds.Model);
|
|
582
|
+
}
|
|
583
|
+
function isTransitionElement(element) {
|
|
584
|
+
return !!element && isKind(element.kind, kinds.Transition);
|
|
585
|
+
}
|
|
586
|
+
function isConstraintElement(element) {
|
|
587
|
+
return !!element && isKind(element.kind, kinds.Constraint);
|
|
588
|
+
}
|
|
589
|
+
function isBehaviorElement(element) {
|
|
590
|
+
return !!element && isKind(element.kind, kinds.Behavior);
|
|
591
|
+
}
|
|
592
|
+
function isVertex(element) {
|
|
593
|
+
return !!element && isKind(element.kind, kinds.Vertex);
|
|
594
|
+
}
|
|
595
|
+
function findState(stack) {
|
|
596
|
+
return find(stack, kinds.State);
|
|
597
|
+
}
|
|
598
|
+
function findTransition(stack) {
|
|
599
|
+
return find(stack, kinds.Transition);
|
|
600
|
+
}
|
|
601
|
+
function findModel(stack) {
|
|
602
|
+
return find(stack, kinds.Model);
|
|
603
|
+
}
|
|
604
|
+
function findVertex(stack) {
|
|
605
|
+
return find(stack, kinds.Vertex);
|
|
606
|
+
}
|
|
607
|
+
var Queue = class {
|
|
608
|
+
front = [];
|
|
609
|
+
back = [];
|
|
610
|
+
backHead = 0;
|
|
611
|
+
len() {
|
|
612
|
+
return this.front.length + (this.back.length - this.backHead);
|
|
613
|
+
}
|
|
614
|
+
pop() {
|
|
615
|
+
var event2;
|
|
616
|
+
if (this.front.length > 0) {
|
|
617
|
+
event2 = this.front.pop();
|
|
618
|
+
} else if (this.backHead < this.back.length) {
|
|
619
|
+
event2 = this.back[this.backHead];
|
|
620
|
+
this.back[this.backHead] = void 0;
|
|
621
|
+
this.backHead++;
|
|
622
|
+
if (this.backHead === this.back.length) {
|
|
623
|
+
this.back = [];
|
|
624
|
+
this.backHead = 0;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return event2;
|
|
628
|
+
}
|
|
629
|
+
push(...events) {
|
|
630
|
+
for (var i = 0; i < events.length; i++) {
|
|
631
|
+
var event2 = events[i];
|
|
632
|
+
if (isKind(event2.kind, kinds.CompletionEvent)) {
|
|
633
|
+
this.front.push(event2);
|
|
634
|
+
} else {
|
|
635
|
+
this.back.push(event2);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
function buildTransitionTable(model) {
|
|
641
|
+
for (var stateName in model.members) {
|
|
642
|
+
var state2 = model.members[stateName];
|
|
643
|
+
if (!isStateElement(state2) && !isModelElement(state2)) continue;
|
|
644
|
+
model.transitionMap[stateName] = {};
|
|
645
|
+
var transitionsByEvent = {};
|
|
646
|
+
var currentPath = stateName;
|
|
647
|
+
var depth = 0;
|
|
648
|
+
while (currentPath) {
|
|
649
|
+
var currentState = model.members[currentPath];
|
|
650
|
+
if (currentState && (isStateElement(currentState) || isModelElement(currentState))) {
|
|
651
|
+
var stateOrModel = currentState;
|
|
652
|
+
for (var i = 0; i < stateOrModel.transitions.length; i++) {
|
|
653
|
+
var transitionName = stateOrModel.transitions[i];
|
|
654
|
+
var transition2 = model.members[transitionName];
|
|
655
|
+
if (isTransitionElement(transition2) && transition2.events) {
|
|
656
|
+
for (var j = 0; j < transition2.events.length; j++) {
|
|
657
|
+
var transitionEventName = transition2.events[j];
|
|
658
|
+
if (transitionEventName.indexOf("*") !== -1) {
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
if (!transitionsByEvent[transitionEventName]) {
|
|
662
|
+
transitionsByEvent[transitionEventName] = [];
|
|
663
|
+
}
|
|
664
|
+
transitionsByEvent[transitionEventName].push({
|
|
665
|
+
transition: transition2,
|
|
666
|
+
priority: depth
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
if (currentPath === "/" || !currentPath) break;
|
|
673
|
+
var parentPath = dirname(currentPath);
|
|
674
|
+
if (parentPath === currentPath) break;
|
|
675
|
+
currentPath = parentPath;
|
|
676
|
+
depth++;
|
|
677
|
+
}
|
|
678
|
+
for (var eventName in transitionsByEvent) {
|
|
679
|
+
var transitions = transitionsByEvent[eventName];
|
|
680
|
+
transitions.sort(function(a, b) {
|
|
681
|
+
return a.priority - b.priority;
|
|
682
|
+
});
|
|
683
|
+
model.transitionMap[stateName][eventName] = transitions.map(function(t) {
|
|
684
|
+
return t.transition;
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
function buildDeferredTable(model) {
|
|
690
|
+
for (var stateName in model.members) {
|
|
691
|
+
var state2 = model.members[stateName];
|
|
692
|
+
if (!isStateElement(state2) && !isModelElement(state2)) continue;
|
|
693
|
+
model.deferredMap[stateName] = {};
|
|
694
|
+
var currentPath = stateName;
|
|
695
|
+
while (currentPath) {
|
|
696
|
+
var currentState = model.members[currentPath];
|
|
697
|
+
if (currentState && (isStateElement(currentState) || isModelElement(currentState))) {
|
|
698
|
+
var stateOrModel = currentState;
|
|
699
|
+
if (stateOrModel.deferred) {
|
|
700
|
+
for (var i = 0; i < stateOrModel.deferred.length; i++) {
|
|
701
|
+
var deferredEvent = stateOrModel.deferred[i];
|
|
702
|
+
var transitions = model.transitionMap[stateName][deferredEvent];
|
|
703
|
+
if (transitions && transitions.some((t) => t.source === stateName)) {
|
|
704
|
+
continue;
|
|
705
|
+
}
|
|
706
|
+
if (deferredEvent.indexOf("*") === -1) {
|
|
707
|
+
model.deferredMap[stateName][deferredEvent] = true;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
if (currentPath === "/" || !currentPath) break;
|
|
713
|
+
var parentPath = dirname(currentPath);
|
|
714
|
+
if (parentPath === currentPath) break;
|
|
715
|
+
currentPath = parentPath;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
var EMPTY_PATH = {
|
|
720
|
+
enter: [],
|
|
721
|
+
exit: []
|
|
722
|
+
};
|
|
723
|
+
var InstanceImpl = class {
|
|
724
|
+
dispatch(event2) {
|
|
725
|
+
var runtime = runtimeFor(this);
|
|
726
|
+
if (!runtime) {
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
runtime.dispatch(event2);
|
|
730
|
+
}
|
|
731
|
+
state() {
|
|
732
|
+
var runtime = runtimeFor(this);
|
|
733
|
+
return runtime ? runtime.state() : "";
|
|
734
|
+
}
|
|
735
|
+
context() {
|
|
736
|
+
var runtime = runtimeFor(this);
|
|
737
|
+
return runtime ? runtime.context() : new Context();
|
|
738
|
+
}
|
|
739
|
+
clock() {
|
|
740
|
+
var runtime = runtimeFor(this);
|
|
741
|
+
return runtime ? runtime.clock : DefaultClock;
|
|
742
|
+
}
|
|
743
|
+
get(name2) {
|
|
744
|
+
var runtime = runtimeFor(this);
|
|
745
|
+
return runtime ? runtime.get(name2) : void 0;
|
|
746
|
+
}
|
|
747
|
+
set(name2, value) {
|
|
748
|
+
var runtime = runtimeFor(this);
|
|
749
|
+
if (runtime) {
|
|
750
|
+
runtime.set(name2, value);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
call(name2, ...args) {
|
|
754
|
+
var runtime = runtimeFor(this);
|
|
755
|
+
if (!runtime) {
|
|
756
|
+
return void 0;
|
|
757
|
+
}
|
|
758
|
+
return runtime.call(name2, ...args);
|
|
759
|
+
}
|
|
760
|
+
restart(data) {
|
|
761
|
+
var runtime = runtimeFor(this);
|
|
762
|
+
if (runtime) {
|
|
763
|
+
runtime.restart(data);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
takeSnapshot() {
|
|
767
|
+
var runtime = runtimeFor(this);
|
|
768
|
+
return runtime ? runtime.takeSnapshot() : {
|
|
769
|
+
id: "",
|
|
770
|
+
qualifiedName: "",
|
|
771
|
+
state: "",
|
|
772
|
+
attributes: {},
|
|
773
|
+
queueLen: 0,
|
|
774
|
+
events: []
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
var Instance = (function() {
|
|
779
|
+
if (this && typeof this === "object") {
|
|
780
|
+
this._hsm = null;
|
|
781
|
+
}
|
|
782
|
+
});
|
|
783
|
+
Instance.prototype = InstanceImpl.prototype;
|
|
784
|
+
var HSM = class _HSM {
|
|
785
|
+
static id = 0;
|
|
786
|
+
runtime;
|
|
787
|
+
instance;
|
|
788
|
+
ctx;
|
|
789
|
+
model;
|
|
790
|
+
currentState;
|
|
791
|
+
queue;
|
|
792
|
+
active;
|
|
793
|
+
processing;
|
|
794
|
+
id;
|
|
795
|
+
name;
|
|
796
|
+
attributes;
|
|
797
|
+
historyShallow;
|
|
798
|
+
historyDeep;
|
|
799
|
+
after;
|
|
800
|
+
clock;
|
|
801
|
+
startData;
|
|
802
|
+
constructor(ctxOrInstance, instanceOrModel, maybeModelOrConfig, maybeConfig) {
|
|
803
|
+
if (!(ctxOrInstance instanceof Context)) {
|
|
804
|
+
maybeConfig = maybeModelOrConfig;
|
|
805
|
+
maybeModelOrConfig = instanceOrModel;
|
|
806
|
+
instanceOrModel = ctxOrInstance;
|
|
807
|
+
ctxOrInstance = new Context();
|
|
808
|
+
}
|
|
809
|
+
const id2 = ((maybeConfig ? maybeConfig.id : "") || _HSM.id++).toString();
|
|
810
|
+
const name2 = (maybeConfig ? maybeConfig.name : "") || maybeModelOrConfig.qualifiedName;
|
|
811
|
+
this.instance = instanceOrModel;
|
|
812
|
+
this.ctx = ctxOrInstance;
|
|
813
|
+
this.model = maybeModelOrConfig;
|
|
814
|
+
this.currentState = this.model;
|
|
815
|
+
this.queue = new Queue();
|
|
816
|
+
this.active = {};
|
|
817
|
+
this.processing = false;
|
|
818
|
+
this.id = id2;
|
|
819
|
+
this.name = name2;
|
|
820
|
+
this.attributes = {};
|
|
821
|
+
this.historyShallow = {};
|
|
822
|
+
this.historyDeep = {};
|
|
823
|
+
this.after = {
|
|
824
|
+
entered: {},
|
|
825
|
+
exited: {},
|
|
826
|
+
processed: {},
|
|
827
|
+
dispatched: {},
|
|
828
|
+
executed: {}
|
|
829
|
+
};
|
|
830
|
+
this.clock = {
|
|
831
|
+
setTimeout: maybeConfig?.clock?.setTimeout || DefaultClock.setTimeout,
|
|
832
|
+
clearTimeout: maybeConfig?.clock?.clearTimeout || DefaultClock.clearTimeout,
|
|
833
|
+
now: maybeConfig?.clock?.now || DefaultClock.now
|
|
834
|
+
};
|
|
835
|
+
this.startData = maybeConfig ? maybeConfig.data : void 0;
|
|
836
|
+
this.runtime = {
|
|
837
|
+
dispatch: (event2) => {
|
|
838
|
+
this.dispatch(event2);
|
|
839
|
+
},
|
|
840
|
+
state: () => {
|
|
841
|
+
return this.state();
|
|
842
|
+
},
|
|
843
|
+
context: () => {
|
|
844
|
+
return this.ctx;
|
|
845
|
+
},
|
|
846
|
+
clock: this.clock,
|
|
847
|
+
get: (name3) => {
|
|
848
|
+
return this.get(name3);
|
|
849
|
+
},
|
|
850
|
+
set: (name3, value) => {
|
|
851
|
+
this.set(name3, value);
|
|
852
|
+
},
|
|
853
|
+
invoke: (name3, ctx, args) => {
|
|
854
|
+
return this.invoke(name3, ctx, args);
|
|
855
|
+
},
|
|
856
|
+
call: (name3, ...args) => {
|
|
857
|
+
return this.call(name3, ...args);
|
|
858
|
+
},
|
|
859
|
+
restart: (data) => {
|
|
860
|
+
this.restart(data);
|
|
861
|
+
},
|
|
862
|
+
takeSnapshot: () => {
|
|
863
|
+
return this.takeSnapshot();
|
|
864
|
+
},
|
|
865
|
+
onAfter: (bucket, key, listener) => {
|
|
866
|
+
this.onAfter(bucket, key, listener);
|
|
867
|
+
},
|
|
868
|
+
stop: () => {
|
|
869
|
+
this.stop();
|
|
870
|
+
},
|
|
871
|
+
id: this.id,
|
|
872
|
+
model: this.model,
|
|
873
|
+
name: this.name,
|
|
874
|
+
queue: this.queue,
|
|
875
|
+
processing: this.processing
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
start() {
|
|
879
|
+
this.processing = true;
|
|
880
|
+
this.ctx.done = false;
|
|
881
|
+
bindRuntime(this.instance, this.runtime);
|
|
882
|
+
this.resetAttributes();
|
|
883
|
+
var initialEvent = Object.create(InitialEvent);
|
|
884
|
+
initialEvent.data = this.startData;
|
|
885
|
+
var newState = this.enter(this.model, initialEvent, true);
|
|
886
|
+
this.ctx.instances[this.id] = this.instance;
|
|
887
|
+
this.currentState = newState;
|
|
888
|
+
this.process();
|
|
889
|
+
return this;
|
|
890
|
+
}
|
|
891
|
+
state() {
|
|
892
|
+
return this.currentState ? this.currentState.qualifiedName : "";
|
|
893
|
+
}
|
|
894
|
+
resetAttributes() {
|
|
895
|
+
this.attributes = {};
|
|
896
|
+
if (!this.model.attributes) {
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
for (var qualifiedName2 in this.model.attributes) {
|
|
900
|
+
var attribute2 = this.model.attributes[qualifiedName2];
|
|
901
|
+
if (attribute2 && attribute2.hasDefault) {
|
|
902
|
+
this.attributes[qualifiedName2] = attribute2.defaultValue;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
notify(bucket, key) {
|
|
907
|
+
var listeners = this.after[bucket][key];
|
|
908
|
+
if (!listeners || listeners.length === 0) {
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
delete this.after[bucket][key];
|
|
912
|
+
for (var i = 0; i < listeners.length; i++) {
|
|
913
|
+
listeners[i]();
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
onAfter(bucket, key, listener) {
|
|
917
|
+
if (!this.after[bucket][key]) {
|
|
918
|
+
this.after[bucket][key] = [];
|
|
919
|
+
}
|
|
920
|
+
this.after[bucket][key].push(listener);
|
|
921
|
+
}
|
|
922
|
+
get(name2) {
|
|
923
|
+
return this.attributes[qualifyModelName(this.model, name2)];
|
|
924
|
+
}
|
|
925
|
+
set(name2, value) {
|
|
926
|
+
var qualifiedName2 = qualifyModelName(this.model, name2);
|
|
927
|
+
var hadValue = Object.prototype.hasOwnProperty.call(this.attributes, qualifiedName2);
|
|
928
|
+
var old = this.attributes[qualifiedName2];
|
|
929
|
+
this.attributes[qualifiedName2] = value;
|
|
930
|
+
if (hadValue && old === value) {
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
var event2 = {
|
|
934
|
+
kind: kinds.ChangeEvent,
|
|
935
|
+
name: qualifiedName2,
|
|
936
|
+
source: qualifiedName2,
|
|
937
|
+
data: {
|
|
938
|
+
name: qualifiedName2,
|
|
939
|
+
old,
|
|
940
|
+
new: value
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
this.dispatch(event2);
|
|
944
|
+
}
|
|
945
|
+
call(name2, ...args) {
|
|
946
|
+
var argsList = args;
|
|
947
|
+
var qualifiedName2 = qualifyModelName(this.model, name2);
|
|
948
|
+
var event2 = {
|
|
949
|
+
kind: kinds.CallEvent,
|
|
950
|
+
name: qualifiedName2,
|
|
951
|
+
source: qualifiedName2,
|
|
952
|
+
data: {
|
|
953
|
+
name: qualifiedName2,
|
|
954
|
+
args: argsList
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
this.dispatch(event2);
|
|
958
|
+
return this.invoke(name2, this.ctx, argsList);
|
|
959
|
+
}
|
|
960
|
+
invoke(name2, ctx, args) {
|
|
961
|
+
var qualifiedName2 = qualifyModelName(this.model, name2);
|
|
962
|
+
var operation2 = this.model.operations && this.model.operations[qualifiedName2];
|
|
963
|
+
if (!operation2) {
|
|
964
|
+
throw new Error('missing operation "' + qualifiedName2 + '"');
|
|
965
|
+
}
|
|
966
|
+
var fn = operation2.implementation;
|
|
967
|
+
if (typeof fn !== "function") {
|
|
968
|
+
throw new Error('invalid operation "' + qualifiedName2 + '"');
|
|
969
|
+
}
|
|
970
|
+
var candidates = [
|
|
971
|
+
[ctx, this.instance, ...args],
|
|
972
|
+
[ctx, ...args],
|
|
973
|
+
[this.instance, ...args],
|
|
974
|
+
args
|
|
975
|
+
];
|
|
976
|
+
for (var i = 0; i < candidates.length; i++) {
|
|
977
|
+
var candidate = candidates[i];
|
|
978
|
+
if (fn.length === candidate.length || fn.length <= candidate.length && fn.length >= 0) {
|
|
979
|
+
return fn.apply(this.instance, candidate);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
return fn.apply(this.instance, args);
|
|
983
|
+
}
|
|
984
|
+
restart(data) {
|
|
985
|
+
this.stop();
|
|
986
|
+
this.startData = data;
|
|
987
|
+
this.ctx.done = false;
|
|
988
|
+
this.currentState = this.model;
|
|
989
|
+
this.start();
|
|
990
|
+
}
|
|
991
|
+
takeSnapshot() {
|
|
992
|
+
var events = [];
|
|
993
|
+
var currentStateName = this.currentState ? this.currentState.qualifiedName : this.model.qualifiedName;
|
|
994
|
+
var transitionsByEvent = this.model.transitionMap[currentStateName] || {};
|
|
995
|
+
for (var eventName in transitionsByEvent) {
|
|
996
|
+
var transitionList = transitionsByEvent[eventName];
|
|
997
|
+
for (var i = 0; i < transitionList.length; i++) {
|
|
998
|
+
events.push({
|
|
999
|
+
event: eventName,
|
|
1000
|
+
target: transitionList[i].target,
|
|
1001
|
+
guard: !!transitionList[i].guard,
|
|
1002
|
+
schema: this.model.events[eventName] ? this.model.events[eventName].schema : void 0
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
return {
|
|
1007
|
+
id: this.id,
|
|
1008
|
+
qualifiedName: this.model.qualifiedName,
|
|
1009
|
+
state: currentStateName,
|
|
1010
|
+
attributes: Object.assign({}, this.attributes),
|
|
1011
|
+
queueLen: this.queue.len(),
|
|
1012
|
+
events
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
recordHistory(stateName) {
|
|
1016
|
+
if (!stateName) {
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
var child = stateName;
|
|
1020
|
+
var parent = dirname(child);
|
|
1021
|
+
while (parent && parent !== "." && parent !== "/") {
|
|
1022
|
+
var element = this.model.members[parent];
|
|
1023
|
+
if (element && isKind(element.kind, kinds.State)) {
|
|
1024
|
+
this.historyDeep[parent] = stateName;
|
|
1025
|
+
this.historyShallow[parent] = child;
|
|
1026
|
+
}
|
|
1027
|
+
if (parent === this.model.qualifiedName) {
|
|
1028
|
+
break;
|
|
1029
|
+
}
|
|
1030
|
+
child = parent;
|
|
1031
|
+
parent = dirname(parent);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
followHistoryDefault(vertex, event2) {
|
|
1035
|
+
for (var i = 0; i < vertex.transitions.length; i++) {
|
|
1036
|
+
var transitionName = vertex.transitions[i];
|
|
1037
|
+
var transitionCandidate = getTransition(this.model, transitionName);
|
|
1038
|
+
if (!transitionCandidate) {
|
|
1039
|
+
continue;
|
|
1040
|
+
}
|
|
1041
|
+
if (transitionCandidate.guard) {
|
|
1042
|
+
var guard2 = getConstraint(this.model, transitionCandidate.guard);
|
|
1043
|
+
if (guard2 && !guard2.expression(this.ctx, this.instance, event2)) {
|
|
1044
|
+
continue;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
return this.transition(vertex, transitionCandidate, event2);
|
|
1048
|
+
}
|
|
1049
|
+
return void 0;
|
|
1050
|
+
}
|
|
1051
|
+
dispatch(event2) {
|
|
1052
|
+
if (!event2.kind) {
|
|
1053
|
+
event2.kind = kinds.Event;
|
|
1054
|
+
}
|
|
1055
|
+
this.queue.push(event2);
|
|
1056
|
+
this.notify("dispatched", event2.name);
|
|
1057
|
+
if (this.processing) {
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
if (this.currentState.qualifiedName === this.model.qualifiedName && this.ctx.done) {
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
this.processing = true;
|
|
1064
|
+
this.process();
|
|
1065
|
+
}
|
|
1066
|
+
process() {
|
|
1067
|
+
var deferred = new Array(this.queue.len() + 1);
|
|
1068
|
+
var deferredCount = 0;
|
|
1069
|
+
var event2 = this.queue.pop();
|
|
1070
|
+
while (event2) {
|
|
1071
|
+
var currentStateName = this.currentState.qualifiedName;
|
|
1072
|
+
var eventName = event2.name;
|
|
1073
|
+
var deferredLookup = this.model.deferredMap[currentStateName];
|
|
1074
|
+
var isDeferred = deferredLookup && deferredLookup[eventName] === true;
|
|
1075
|
+
if (isDeferred) {
|
|
1076
|
+
deferred[deferredCount++] = event2;
|
|
1077
|
+
event2 = this.queue.pop();
|
|
1078
|
+
continue;
|
|
1079
|
+
}
|
|
1080
|
+
var transitions = this.model.transitionMap[currentStateName][eventName];
|
|
1081
|
+
if (transitions && transitions.length > 0) {
|
|
1082
|
+
for (var i = 0; i < transitions.length; i++) {
|
|
1083
|
+
var transition2 = transitions[i];
|
|
1084
|
+
if (transition2.guard) {
|
|
1085
|
+
var guard2 = getConstraint(this.model, transition2.guard);
|
|
1086
|
+
if (guard2) {
|
|
1087
|
+
try {
|
|
1088
|
+
var guardResult = guard2.expression(
|
|
1089
|
+
this.ctx,
|
|
1090
|
+
this.instance,
|
|
1091
|
+
event2
|
|
1092
|
+
);
|
|
1093
|
+
if (!guardResult) {
|
|
1094
|
+
continue;
|
|
1095
|
+
}
|
|
1096
|
+
} catch (error) {
|
|
1097
|
+
this.dispatch(
|
|
1098
|
+
Object.create(ErrorEvent, {
|
|
1099
|
+
data: { value: error }
|
|
1100
|
+
})
|
|
1101
|
+
);
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
var nextState = this.transition(this.currentState, transition2, event2);
|
|
1107
|
+
if (nextState.qualifiedName !== this.currentState.qualifiedName) {
|
|
1108
|
+
this.currentState = nextState;
|
|
1109
|
+
for (var j = 0; j < deferredCount; j++) {
|
|
1110
|
+
this.queue.push(deferred[j]);
|
|
1111
|
+
}
|
|
1112
|
+
deferredCount = 0;
|
|
1113
|
+
}
|
|
1114
|
+
break;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
this.notify("processed", event2.name);
|
|
1118
|
+
event2 = this.queue.pop();
|
|
1119
|
+
}
|
|
1120
|
+
for (var i = 0; i < deferredCount; i++) {
|
|
1121
|
+
this.queue.push(deferred[i]);
|
|
1122
|
+
}
|
|
1123
|
+
this.processing = false;
|
|
1124
|
+
this.notify("processed", "__next__");
|
|
1125
|
+
}
|
|
1126
|
+
transition(current, transition2, event2) {
|
|
1127
|
+
var path = transition2.paths[current.qualifiedName];
|
|
1128
|
+
if (!path) {
|
|
1129
|
+
path = EMPTY_PATH;
|
|
1130
|
+
}
|
|
1131
|
+
for (var i = 0; i < path.exit.length; i++) {
|
|
1132
|
+
var exitingName = path.exit[i];
|
|
1133
|
+
var exiting = this.model.members[exitingName];
|
|
1134
|
+
if (exiting && isStateElement(exiting)) {
|
|
1135
|
+
this.exit(exiting, event2);
|
|
1136
|
+
this.notify("exited", exitingName);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
for (var i = 0; i < transition2.effect.length; i++) {
|
|
1140
|
+
var effectName = transition2.effect[i];
|
|
1141
|
+
var behavior = getBehavior(this.model, effectName);
|
|
1142
|
+
if (behavior) {
|
|
1143
|
+
this.execute(behavior, event2);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
var enteredState;
|
|
1147
|
+
for (var i = 0; i < path.enter.length; i++) {
|
|
1148
|
+
var enteringName = path.enter[i];
|
|
1149
|
+
var entering = getVertex(this.model, enteringName);
|
|
1150
|
+
if (entering) {
|
|
1151
|
+
var defaultEntry = entering.qualifiedName === transition2.target && transition2.kind !== kinds.Self;
|
|
1152
|
+
enteredState = this.enter(entering, event2, defaultEntry);
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
var finalState = enteredState || getVertex(this.model, transition2.target);
|
|
1156
|
+
return finalState || current;
|
|
1157
|
+
}
|
|
1158
|
+
enter(vertex, event2, defaultEntry) {
|
|
1159
|
+
if (isStateElement(vertex)) {
|
|
1160
|
+
var state2 = vertex;
|
|
1161
|
+
this.recordHistory(state2.qualifiedName);
|
|
1162
|
+
for (var i = 0; i < state2.entry.length; i++) {
|
|
1163
|
+
var entryName = state2.entry[i];
|
|
1164
|
+
var behavior = getBehavior(this.model, entryName);
|
|
1165
|
+
if (behavior) {
|
|
1166
|
+
this.execute(behavior, event2);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
this.notify("entered", state2.qualifiedName);
|
|
1170
|
+
for (var i = 0; i < state2.activities.length; i++) {
|
|
1171
|
+
var activityName = state2.activities[i];
|
|
1172
|
+
var behavior = getBehavior(this.model, activityName);
|
|
1173
|
+
if (behavior) {
|
|
1174
|
+
this.execute(behavior, event2);
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
if (defaultEntry && state2.initial) {
|
|
1178
|
+
var initial2 = getVertex(this.model, state2.initial);
|
|
1179
|
+
if (!initial2) {
|
|
1180
|
+
return state2;
|
|
1181
|
+
}
|
|
1182
|
+
var initialTransition = getTransition(
|
|
1183
|
+
this.model,
|
|
1184
|
+
initial2.transitions[0]
|
|
1185
|
+
);
|
|
1186
|
+
if (initialTransition) {
|
|
1187
|
+
return this.transition(state2, initialTransition, event2);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return state2;
|
|
1191
|
+
}
|
|
1192
|
+
if (isKind(vertex.kind, kinds.Choice) || isKind(vertex.kind, kinds.Junction)) {
|
|
1193
|
+
var choiceVertex = vertex;
|
|
1194
|
+
var chosenTransition = void 0;
|
|
1195
|
+
for (var i = 0; i < choiceVertex.transitions.length; i++) {
|
|
1196
|
+
var transitionName = choiceVertex.transitions[i];
|
|
1197
|
+
var choiceTransition = getTransition(this.model, transitionName);
|
|
1198
|
+
if (!choiceTransition) {
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
if (choiceTransition.guard) {
|
|
1202
|
+
var guard2 = getConstraint(this.model, choiceTransition.guard);
|
|
1203
|
+
if (guard2 && guard2.expression(this.ctx, this.instance, event2)) {
|
|
1204
|
+
chosenTransition = choiceTransition;
|
|
1205
|
+
break;
|
|
1206
|
+
}
|
|
1207
|
+
} else {
|
|
1208
|
+
chosenTransition = choiceTransition;
|
|
1209
|
+
break;
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
if (chosenTransition) {
|
|
1213
|
+
return this.transition(choiceVertex, chosenTransition, event2);
|
|
1214
|
+
}
|
|
1215
|
+
throw new Error("No transition found for choice vertex " + choiceVertex.qualifiedName);
|
|
1216
|
+
}
|
|
1217
|
+
if (isKind(vertex.kind, kinds.ShallowHistory) || isKind(vertex.kind, kinds.DeepHistory)) {
|
|
1218
|
+
var parent = dirname(vertex.qualifiedName);
|
|
1219
|
+
var isShallowHistory = isKind(vertex.kind, kinds.ShallowHistory);
|
|
1220
|
+
var resolved = isShallowHistory ? this.historyShallow[parent] : this.historyDeep[parent];
|
|
1221
|
+
if (!resolved) {
|
|
1222
|
+
var historyResult = this.followHistoryDefault(vertex, event2);
|
|
1223
|
+
if (historyResult) {
|
|
1224
|
+
return historyResult;
|
|
1225
|
+
}
|
|
1226
|
+
var parentState = getState(this.model, parent);
|
|
1227
|
+
if (parentState && parentState.initial) {
|
|
1228
|
+
var initialVertex = getVertex(this.model, parentState.initial);
|
|
1229
|
+
if (initialVertex && initialVertex.transitions.length > 0) {
|
|
1230
|
+
var initialTransition = getTransition(
|
|
1231
|
+
this.model,
|
|
1232
|
+
initialVertex.transitions[0]
|
|
1233
|
+
);
|
|
1234
|
+
if (initialTransition) {
|
|
1235
|
+
return this.transition(
|
|
1236
|
+
parentState,
|
|
1237
|
+
initialTransition,
|
|
1238
|
+
event2
|
|
1239
|
+
);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
return vertex;
|
|
1244
|
+
}
|
|
1245
|
+
var enterPath = [];
|
|
1246
|
+
var currentPath = resolved;
|
|
1247
|
+
while (currentPath && currentPath !== parent && currentPath !== ".") {
|
|
1248
|
+
enterPath.unshift(currentPath);
|
|
1249
|
+
currentPath = dirname(currentPath);
|
|
1250
|
+
}
|
|
1251
|
+
var currentVertex = vertex;
|
|
1252
|
+
for (var i = 0; i < enterPath.length; i++) {
|
|
1253
|
+
var entering = getVertex(this.model, enterPath[i]);
|
|
1254
|
+
if (!entering) {
|
|
1255
|
+
break;
|
|
1256
|
+
}
|
|
1257
|
+
currentVertex = this.enter(
|
|
1258
|
+
entering,
|
|
1259
|
+
event2,
|
|
1260
|
+
isShallowHistory && i === enterPath.length - 1
|
|
1261
|
+
);
|
|
1262
|
+
}
|
|
1263
|
+
return currentVertex;
|
|
1264
|
+
}
|
|
1265
|
+
if (isKind(vertex.kind, kinds.FinalState)) {
|
|
1266
|
+
this.notify("entered", vertex.qualifiedName);
|
|
1267
|
+
if (dirname(vertex.qualifiedName) === this.model.qualifiedName) {
|
|
1268
|
+
this.stop();
|
|
1269
|
+
}
|
|
1270
|
+
return vertex;
|
|
1271
|
+
}
|
|
1272
|
+
return vertex;
|
|
1273
|
+
}
|
|
1274
|
+
exit(state2, event2) {
|
|
1275
|
+
for (var i = 0; i < state2.activities.length; i++) {
|
|
1276
|
+
var activityName = state2.activities[i];
|
|
1277
|
+
var activity2 = getBehavior(this.model, activityName);
|
|
1278
|
+
if (activity2) {
|
|
1279
|
+
this.terminate(activity2);
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
for (var i = 0; i < state2.exit.length; i++) {
|
|
1283
|
+
var exitName = state2.exit[i];
|
|
1284
|
+
var behavior = getBehavior(this.model, exitName);
|
|
1285
|
+
if (behavior) {
|
|
1286
|
+
this.execute(behavior, event2);
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
execute(behavior, event2) {
|
|
1291
|
+
var error = void 0;
|
|
1292
|
+
var operation2 = behavior.operation;
|
|
1293
|
+
var self = this;
|
|
1294
|
+
if (!operation2 && behavior.operationName) {
|
|
1295
|
+
operation2 = function(ctx, instance, evt) {
|
|
1296
|
+
self.invoke(behavior.operationName, ctx, [evt]);
|
|
1297
|
+
};
|
|
1298
|
+
}
|
|
1299
|
+
if (!operation2) {
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
if (isKind(behavior.kind, kinds.Concurrent)) {
|
|
1303
|
+
var controller = new Context();
|
|
1304
|
+
try {
|
|
1305
|
+
var asyncOperationPromise = Promise.resolve(
|
|
1306
|
+
operation2(controller, this.instance, event2)
|
|
1307
|
+
);
|
|
1308
|
+
this.active[behavior.qualifiedName] = {
|
|
1309
|
+
context: controller,
|
|
1310
|
+
promise: asyncOperationPromise.catch(function(error2) {
|
|
1311
|
+
self.dispatch(
|
|
1312
|
+
Object.create(ErrorEvent, {
|
|
1313
|
+
data: { value: error2 }
|
|
1314
|
+
})
|
|
1315
|
+
);
|
|
1316
|
+
}).then(function() {
|
|
1317
|
+
self.notify("executed", behavior.qualifiedName);
|
|
1318
|
+
self.notify(
|
|
1319
|
+
"executed",
|
|
1320
|
+
behavior.Owner ? behavior.Owner() : dirname(behavior.qualifiedName)
|
|
1321
|
+
);
|
|
1322
|
+
})
|
|
1323
|
+
};
|
|
1324
|
+
} catch (err) {
|
|
1325
|
+
error = err;
|
|
1326
|
+
}
|
|
1327
|
+
} else {
|
|
1328
|
+
try {
|
|
1329
|
+
operation2(this.ctx, this.instance, event2);
|
|
1330
|
+
this.notify("executed", behavior.qualifiedName);
|
|
1331
|
+
this.notify("executed", dirname(behavior.qualifiedName));
|
|
1332
|
+
} catch (err) {
|
|
1333
|
+
error = err;
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
if (error) {
|
|
1337
|
+
this.dispatch(
|
|
1338
|
+
Object.create(ErrorEvent, {
|
|
1339
|
+
data: { value: error }
|
|
1340
|
+
})
|
|
1341
|
+
);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
terminate(activity2) {
|
|
1345
|
+
var active = this.active[activity2.qualifiedName];
|
|
1346
|
+
if (active) {
|
|
1347
|
+
active.context.done = true;
|
|
1348
|
+
for (var i = 0; i < active.context.listeners.length; i++) {
|
|
1349
|
+
active.context.listeners[i]();
|
|
1350
|
+
}
|
|
1351
|
+
delete this.active[activity2.qualifiedName];
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
stop() {
|
|
1355
|
+
this.processing = true;
|
|
1356
|
+
while (this.currentState && this.currentState.qualifiedName !== this.model.qualifiedName) {
|
|
1357
|
+
var currentState = getState(this.model, this.currentState.qualifiedName);
|
|
1358
|
+
if (!currentState) {
|
|
1359
|
+
break;
|
|
1360
|
+
}
|
|
1361
|
+
this.exit(currentState, FinalEvent);
|
|
1362
|
+
var parentState = getState(
|
|
1363
|
+
this.model,
|
|
1364
|
+
dirname(this.currentState.qualifiedName)
|
|
1365
|
+
);
|
|
1366
|
+
if (!parentState) {
|
|
1367
|
+
break;
|
|
1368
|
+
}
|
|
1369
|
+
this.currentState = parentState;
|
|
1370
|
+
}
|
|
1371
|
+
this.processing = false;
|
|
1372
|
+
this.ctx.done = true;
|
|
1373
|
+
for (var i = 0; i < this.ctx.listeners.length; i++) {
|
|
1374
|
+
this.ctx.listeners[i]();
|
|
1375
|
+
}
|
|
1376
|
+
delete this.ctx.instances[this.id];
|
|
1377
|
+
}
|
|
1378
|
+
};
|
|
1379
|
+
function find(stack, ...kindsToMatch) {
|
|
1380
|
+
for (var i = stack.length - 1; i >= 0; i--) {
|
|
1381
|
+
var element = stack[i];
|
|
1382
|
+
for (var j = 0; j < kindsToMatch.length; j++) {
|
|
1383
|
+
if (isKind(element.kind, kindsToMatch[j])) {
|
|
1384
|
+
return element;
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
return void 0;
|
|
1389
|
+
}
|
|
1390
|
+
function start(ctxOrInstance, instance, model, maybeConfig) {
|
|
1391
|
+
var context = ctxOrInstance;
|
|
1392
|
+
var runtimeModel = model;
|
|
1393
|
+
var runtimeInstance = instance;
|
|
1394
|
+
if (!(context instanceof Context)) {
|
|
1395
|
+
maybeConfig = model;
|
|
1396
|
+
runtimeModel = instance;
|
|
1397
|
+
runtimeInstance = ctxOrInstance;
|
|
1398
|
+
context = new Context();
|
|
1399
|
+
}
|
|
1400
|
+
var sm = new HSM(
|
|
1401
|
+
context,
|
|
1402
|
+
runtimeInstance,
|
|
1403
|
+
runtimeModel,
|
|
1404
|
+
maybeConfig
|
|
1405
|
+
);
|
|
1406
|
+
sm.start();
|
|
1407
|
+
return runtimeInstance;
|
|
1408
|
+
}
|
|
1409
|
+
function stop(instance) {
|
|
1410
|
+
var runtime = runtimeFor(instance);
|
|
1411
|
+
if (runtime) {
|
|
1412
|
+
runtime.stop();
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
function state(name2, ...partials) {
|
|
1416
|
+
return attachSpec(
|
|
1417
|
+
function(model, stack) {
|
|
1418
|
+
var namespace = find(stack, kinds.State, kinds.Model);
|
|
1419
|
+
var qualifiedName2 = join(namespace.qualifiedName, name2);
|
|
1420
|
+
var stateObj = {
|
|
1421
|
+
qualifiedName: qualifiedName2,
|
|
1422
|
+
kind: kinds.State,
|
|
1423
|
+
transitions: [],
|
|
1424
|
+
entry: [],
|
|
1425
|
+
exit: [],
|
|
1426
|
+
activities: [],
|
|
1427
|
+
deferred: []
|
|
1428
|
+
};
|
|
1429
|
+
model.members[stateObj.qualifiedName] = stateObj;
|
|
1430
|
+
stack.push(stateObj);
|
|
1431
|
+
apply(model, stack, partials);
|
|
1432
|
+
stack.pop();
|
|
1433
|
+
return stateObj;
|
|
1434
|
+
},
|
|
1435
|
+
{
|
|
1436
|
+
kind: "state",
|
|
1437
|
+
name: name2,
|
|
1438
|
+
parts: partials
|
|
1439
|
+
}
|
|
1440
|
+
);
|
|
1441
|
+
}
|
|
1442
|
+
function initial(nameOrPartial, ...partials) {
|
|
1443
|
+
var name2 = ".initial";
|
|
1444
|
+
if (typeof nameOrPartial === "string") {
|
|
1445
|
+
name2 = nameOrPartial;
|
|
1446
|
+
} else if (typeof nameOrPartial === "function") {
|
|
1447
|
+
partials = [nameOrPartial, ...partials];
|
|
1448
|
+
}
|
|
1449
|
+
return attachSpec(
|
|
1450
|
+
function(model, stack) {
|
|
1451
|
+
var state2 = findState(stack);
|
|
1452
|
+
if (!state2) {
|
|
1453
|
+
throw new Error("initial must be declared inside a state");
|
|
1454
|
+
}
|
|
1455
|
+
var initialName = join(state2.qualifiedName, name2);
|
|
1456
|
+
var initialObj = {
|
|
1457
|
+
qualifiedName: initialName,
|
|
1458
|
+
kind: kinds.Initial,
|
|
1459
|
+
transitions: []
|
|
1460
|
+
};
|
|
1461
|
+
model.members[initialName] = initialObj;
|
|
1462
|
+
state2.initial = initialName;
|
|
1463
|
+
var resolvedPartials = [source(initialObj.qualifiedName), on(InitialEvent), ...partials];
|
|
1464
|
+
stack.push(initialObj);
|
|
1465
|
+
var transitionObj = transition(...resolvedPartials)(model, stack);
|
|
1466
|
+
stack.pop();
|
|
1467
|
+
return transitionObj;
|
|
1468
|
+
},
|
|
1469
|
+
{
|
|
1470
|
+
kind: "initial",
|
|
1471
|
+
name: name2,
|
|
1472
|
+
parts: partials
|
|
1473
|
+
}
|
|
1474
|
+
);
|
|
1475
|
+
}
|
|
1476
|
+
function transition(...partials) {
|
|
1477
|
+
return attachSpec(
|
|
1478
|
+
function(model, stack) {
|
|
1479
|
+
var vertex = findVertex(stack);
|
|
1480
|
+
if (!vertex) {
|
|
1481
|
+
throw new Error("transition must be declared inside a vertex");
|
|
1482
|
+
}
|
|
1483
|
+
var name2 = "transition_" + Object.keys(model.members).length;
|
|
1484
|
+
var transitionObj = {
|
|
1485
|
+
qualifiedName: join(vertex.qualifiedName, name2),
|
|
1486
|
+
kind: kinds.Transition,
|
|
1487
|
+
// Will be updated later
|
|
1488
|
+
source: ".",
|
|
1489
|
+
guard: "",
|
|
1490
|
+
effect: [],
|
|
1491
|
+
events: [],
|
|
1492
|
+
paths: {},
|
|
1493
|
+
target: void 0
|
|
1494
|
+
};
|
|
1495
|
+
model.members[transitionObj.qualifiedName] = transitionObj;
|
|
1496
|
+
stack.push(transitionObj);
|
|
1497
|
+
apply(model, stack, partials);
|
|
1498
|
+
stack.pop();
|
|
1499
|
+
if (transitionObj.source == "." || !transitionObj.source) {
|
|
1500
|
+
transitionObj.source = vertex.qualifiedName;
|
|
1501
|
+
}
|
|
1502
|
+
var sourceElement = getVertex(model, transitionObj.source);
|
|
1503
|
+
if (!sourceElement) {
|
|
1504
|
+
throw new Error("invalid transition source " + transitionObj.source);
|
|
1505
|
+
}
|
|
1506
|
+
sourceElement.transitions.push(transitionObj.qualifiedName);
|
|
1507
|
+
model.partials.push(function() {
|
|
1508
|
+
if (transitionObj.target === transitionObj.source) {
|
|
1509
|
+
transitionObj.kind = kinds.Self;
|
|
1510
|
+
} else if (!transitionObj.target) {
|
|
1511
|
+
transitionObj.kind = kinds.Internal;
|
|
1512
|
+
} else if (isAncestor(transitionObj.source, transitionObj.target)) {
|
|
1513
|
+
transitionObj.kind = kinds.Local;
|
|
1514
|
+
} else {
|
|
1515
|
+
transitionObj.kind = kinds.External;
|
|
1516
|
+
}
|
|
1517
|
+
var lcaPath = lca(transitionObj.source, transitionObj.target);
|
|
1518
|
+
var enter = [];
|
|
1519
|
+
if (transitionObj.kind === kinds.Self) {
|
|
1520
|
+
enter.push(sourceElement.qualifiedName);
|
|
1521
|
+
} else {
|
|
1522
|
+
var entering = transitionObj.target;
|
|
1523
|
+
while (entering && entering !== lcaPath && entering !== "/") {
|
|
1524
|
+
enter.unshift(entering);
|
|
1525
|
+
entering = dirname(entering);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
if (sourceElement.kind === kinds.Initial) {
|
|
1529
|
+
transitionObj.paths[dirname(sourceElement.qualifiedName)] = {
|
|
1530
|
+
enter,
|
|
1531
|
+
exit: [sourceElement.qualifiedName]
|
|
1532
|
+
};
|
|
1533
|
+
return transitionObj;
|
|
1534
|
+
}
|
|
1535
|
+
model.partials.push(function() {
|
|
1536
|
+
if (transitionObj.kind === kinds.Internal) {
|
|
1537
|
+
return;
|
|
1538
|
+
}
|
|
1539
|
+
for (var qualifiedName2 in model.members) {
|
|
1540
|
+
var element = getVertex(model, qualifiedName2);
|
|
1541
|
+
if (!element) {
|
|
1542
|
+
continue;
|
|
1543
|
+
}
|
|
1544
|
+
if (transitionObj.source !== qualifiedName2 && !isAncestor(transitionObj.source, qualifiedName2)) {
|
|
1545
|
+
continue;
|
|
1546
|
+
}
|
|
1547
|
+
var exit2 = [];
|
|
1548
|
+
var exiting = element.qualifiedName;
|
|
1549
|
+
while (exiting !== lcaPath && exiting) {
|
|
1550
|
+
exit2.push(exiting);
|
|
1551
|
+
exiting = dirname(exiting);
|
|
1552
|
+
if (exiting === "/") {
|
|
1553
|
+
break;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
transitionObj.paths[element.qualifiedName] = {
|
|
1557
|
+
enter,
|
|
1558
|
+
exit: exit2
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
});
|
|
1562
|
+
});
|
|
1563
|
+
return transitionObj;
|
|
1564
|
+
},
|
|
1565
|
+
{
|
|
1566
|
+
kind: "transition",
|
|
1567
|
+
parts: partials
|
|
1568
|
+
}
|
|
1569
|
+
);
|
|
1570
|
+
}
|
|
1571
|
+
function source(name2) {
|
|
1572
|
+
return attachSpec(
|
|
1573
|
+
function(model, stack) {
|
|
1574
|
+
var transition2 = findTransition(stack);
|
|
1575
|
+
if (!transition2) {
|
|
1576
|
+
return void 0;
|
|
1577
|
+
}
|
|
1578
|
+
var resolvedName = name2;
|
|
1579
|
+
if (!isAbsolute(resolvedName)) {
|
|
1580
|
+
var ancestor = findState(stack);
|
|
1581
|
+
if (ancestor) {
|
|
1582
|
+
resolvedName = join(ancestor.qualifiedName, resolvedName);
|
|
1583
|
+
}
|
|
1584
|
+
} else if (!isAncestor(model.qualifiedName, resolvedName)) {
|
|
1585
|
+
resolvedName = join(model.qualifiedName, resolvedName.slice(1));
|
|
1586
|
+
}
|
|
1587
|
+
transition2.source = resolvedName;
|
|
1588
|
+
return transition2;
|
|
1589
|
+
},
|
|
1590
|
+
{
|
|
1591
|
+
kind: "source",
|
|
1592
|
+
name: name2
|
|
1593
|
+
}
|
|
1594
|
+
);
|
|
1595
|
+
}
|
|
1596
|
+
function target(name2) {
|
|
1597
|
+
return attachSpec(
|
|
1598
|
+
function(model, stack) {
|
|
1599
|
+
var transition2 = findTransition(stack);
|
|
1600
|
+
if (!transition2) {
|
|
1601
|
+
return void 0;
|
|
1602
|
+
}
|
|
1603
|
+
var resolvedName = name2;
|
|
1604
|
+
if (!isAbsolute(resolvedName)) {
|
|
1605
|
+
var ancestor = findState(stack) || findModel(stack);
|
|
1606
|
+
if (ancestor) {
|
|
1607
|
+
resolvedName = join(ancestor.qualifiedName, resolvedName);
|
|
1608
|
+
}
|
|
1609
|
+
} else if (!isAncestor(model.qualifiedName, resolvedName)) {
|
|
1610
|
+
resolvedName = join(model.qualifiedName, resolvedName.slice(1));
|
|
1611
|
+
}
|
|
1612
|
+
transition2.target = resolvedName;
|
|
1613
|
+
return transition2;
|
|
1614
|
+
},
|
|
1615
|
+
{
|
|
1616
|
+
kind: "target",
|
|
1617
|
+
name: name2
|
|
1618
|
+
}
|
|
1619
|
+
);
|
|
1620
|
+
}
|
|
1621
|
+
function on(eventOrRecord) {
|
|
1622
|
+
const event2 = eventOrRecord;
|
|
1623
|
+
return attachSpec(
|
|
1624
|
+
function(model, stack) {
|
|
1625
|
+
var transition2 = findTransition(stack);
|
|
1626
|
+
if (!transition2) {
|
|
1627
|
+
return void 0;
|
|
1628
|
+
}
|
|
1629
|
+
var eventName = typeof event2 === "string" ? event2 : event2.name;
|
|
1630
|
+
transition2.events.push(eventName);
|
|
1631
|
+
if (typeof event2 !== "string") {
|
|
1632
|
+
registerEvent(model, event2);
|
|
1633
|
+
}
|
|
1634
|
+
return transition2;
|
|
1635
|
+
},
|
|
1636
|
+
{
|
|
1637
|
+
kind: "on",
|
|
1638
|
+
name: typeof eventOrRecord === "string" ? eventOrRecord : eventOrRecord.name,
|
|
1639
|
+
schema: typeof eventOrRecord === "string" ? void 0 : eventOrRecord.schema
|
|
1640
|
+
}
|
|
1641
|
+
);
|
|
1642
|
+
}
|
|
1643
|
+
function onSet(name2) {
|
|
1644
|
+
return attachSpec(
|
|
1645
|
+
function(model, stack) {
|
|
1646
|
+
var transition2 = findTransition(stack);
|
|
1647
|
+
if (!transition2) {
|
|
1648
|
+
return void 0;
|
|
1649
|
+
}
|
|
1650
|
+
var qualifiedName2 = qualifyModelName(model, name2);
|
|
1651
|
+
transition2.events.push(qualifiedName2);
|
|
1652
|
+
registerEvent(model, {
|
|
1653
|
+
kind: kinds.ChangeEvent,
|
|
1654
|
+
name: qualifiedName2,
|
|
1655
|
+
source: qualifiedName2
|
|
1656
|
+
});
|
|
1657
|
+
if (!model.attributes[qualifiedName2]) {
|
|
1658
|
+
model.attributes[qualifiedName2] = {
|
|
1659
|
+
name: name2,
|
|
1660
|
+
qualifiedName: qualifiedName2,
|
|
1661
|
+
hasDefault: false
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
return transition2;
|
|
1665
|
+
},
|
|
1666
|
+
{
|
|
1667
|
+
kind: "onSet",
|
|
1668
|
+
name: name2
|
|
1669
|
+
}
|
|
1670
|
+
);
|
|
1671
|
+
}
|
|
1672
|
+
function onCall(name2) {
|
|
1673
|
+
return attachSpec(
|
|
1674
|
+
function(model, stack) {
|
|
1675
|
+
var transition2 = findTransition(stack);
|
|
1676
|
+
if (!transition2) {
|
|
1677
|
+
return void 0;
|
|
1678
|
+
}
|
|
1679
|
+
var qualifiedName2 = qualifyModelName(model, name2);
|
|
1680
|
+
transition2.events.push(qualifiedName2);
|
|
1681
|
+
registerEvent(model, {
|
|
1682
|
+
kind: kinds.CallEvent,
|
|
1683
|
+
name: qualifiedName2,
|
|
1684
|
+
source: qualifiedName2
|
|
1685
|
+
});
|
|
1686
|
+
model.partials.push(function() {
|
|
1687
|
+
if (!model.operations[qualifiedName2]) {
|
|
1688
|
+
throw new Error('missing operation "' + qualifiedName2 + '" for OnCall()');
|
|
1689
|
+
}
|
|
1690
|
+
});
|
|
1691
|
+
return transition2;
|
|
1692
|
+
},
|
|
1693
|
+
{
|
|
1694
|
+
kind: "onCall",
|
|
1695
|
+
name: name2
|
|
1696
|
+
}
|
|
1697
|
+
);
|
|
1698
|
+
}
|
|
1699
|
+
function when(expr) {
|
|
1700
|
+
if (typeof expr === "string") {
|
|
1701
|
+
return onSet(expr);
|
|
1702
|
+
}
|
|
1703
|
+
return function(model, stack) {
|
|
1704
|
+
var transition2 = findTransition(stack);
|
|
1705
|
+
if (!transition2) {
|
|
1706
|
+
return void 0;
|
|
1707
|
+
}
|
|
1708
|
+
var eventName = join(transition2.qualifiedName, "when_" + Object.keys(model.members).length);
|
|
1709
|
+
var event2 = {
|
|
1710
|
+
kind: kinds.TimeEvent,
|
|
1711
|
+
name: eventName
|
|
1712
|
+
};
|
|
1713
|
+
transition2.events.push(eventName);
|
|
1714
|
+
registerEvent(model, event2);
|
|
1715
|
+
model.partials.push(function() {
|
|
1716
|
+
var source2 = getState(model, transition2.source);
|
|
1717
|
+
if (!source2) {
|
|
1718
|
+
return;
|
|
1719
|
+
}
|
|
1720
|
+
pushBehaviors(
|
|
1721
|
+
join(source2.qualifiedName, "activity_when_" + source2.activities.length),
|
|
1722
|
+
kinds.Concurrent,
|
|
1723
|
+
source2.activities,
|
|
1724
|
+
model,
|
|
1725
|
+
[function(ctx, instance, evt) {
|
|
1726
|
+
var signal = expr(ctx, instance, evt);
|
|
1727
|
+
var signalLike = toSignalLike(signal);
|
|
1728
|
+
if (!signalLike) {
|
|
1729
|
+
return;
|
|
1730
|
+
}
|
|
1731
|
+
return new Promise(function(resolve) {
|
|
1732
|
+
var once = function() {
|
|
1733
|
+
if (!ctx.done) {
|
|
1734
|
+
instance.dispatch(event2);
|
|
1735
|
+
}
|
|
1736
|
+
resolve();
|
|
1737
|
+
};
|
|
1738
|
+
if (hasThen(signalLike)) {
|
|
1739
|
+
signalLike.then(once);
|
|
1740
|
+
} else if (hasAddEventListener(signalLike)) {
|
|
1741
|
+
signalLike.addEventListener("done", once);
|
|
1742
|
+
} else if (hasOn(signalLike)) {
|
|
1743
|
+
signalLike.on("ready", once);
|
|
1744
|
+
signalLike.on("done", once);
|
|
1745
|
+
}
|
|
1746
|
+
ctx.addEventListener("done", resolve);
|
|
1747
|
+
});
|
|
1748
|
+
}]
|
|
1749
|
+
);
|
|
1750
|
+
});
|
|
1751
|
+
};
|
|
1752
|
+
}
|
|
1753
|
+
function pushBehaviors(namePrefix, kind2, namesList, model, operations) {
|
|
1754
|
+
for (var i = 0; i < operations.length; i++) {
|
|
1755
|
+
var operation2 = operations[i];
|
|
1756
|
+
var qualifiedName2 = namePrefix + "_" + namesList.length;
|
|
1757
|
+
var behavior = {
|
|
1758
|
+
qualifiedName: qualifiedName2,
|
|
1759
|
+
kind: kind2,
|
|
1760
|
+
operation: typeof operation2 === "function" ? operation2 : null,
|
|
1761
|
+
operationName: typeof operation2 === "string" ? qualifyModelName(model, operation2) : void 0
|
|
1762
|
+
};
|
|
1763
|
+
model.members[qualifiedName2] = behavior;
|
|
1764
|
+
namesList.push(qualifiedName2);
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
function entry(...operations) {
|
|
1768
|
+
return attachSpec(
|
|
1769
|
+
function(model, stack) {
|
|
1770
|
+
var state2 = findState(stack);
|
|
1771
|
+
if (!state2) {
|
|
1772
|
+
return void 0;
|
|
1773
|
+
}
|
|
1774
|
+
pushBehaviors(join(state2.qualifiedName, "entry"), kinds.Sequential, state2.entry, model, operations);
|
|
1775
|
+
return state2;
|
|
1776
|
+
},
|
|
1777
|
+
{
|
|
1778
|
+
kind: "entry",
|
|
1779
|
+
value: operations
|
|
1780
|
+
}
|
|
1781
|
+
);
|
|
1782
|
+
}
|
|
1783
|
+
function exit(...operations) {
|
|
1784
|
+
return attachSpec(
|
|
1785
|
+
function(model, stack) {
|
|
1786
|
+
var state2 = findState(stack);
|
|
1787
|
+
if (!state2) {
|
|
1788
|
+
return void 0;
|
|
1789
|
+
}
|
|
1790
|
+
pushBehaviors(join(state2.qualifiedName, "exit"), kinds.Sequential, state2.exit, model, operations);
|
|
1791
|
+
return state2;
|
|
1792
|
+
},
|
|
1793
|
+
{
|
|
1794
|
+
kind: "exit",
|
|
1795
|
+
value: operations
|
|
1796
|
+
}
|
|
1797
|
+
);
|
|
1798
|
+
}
|
|
1799
|
+
function activity(...operations) {
|
|
1800
|
+
return attachSpec(
|
|
1801
|
+
function(model, stack) {
|
|
1802
|
+
var state2 = findState(stack);
|
|
1803
|
+
if (!state2) {
|
|
1804
|
+
return void 0;
|
|
1805
|
+
}
|
|
1806
|
+
pushBehaviors(join(state2.qualifiedName, "activity"), kinds.Concurrent, state2.activities, model, operations);
|
|
1807
|
+
return state2;
|
|
1808
|
+
},
|
|
1809
|
+
{
|
|
1810
|
+
kind: "activity",
|
|
1811
|
+
value: operations
|
|
1812
|
+
}
|
|
1813
|
+
);
|
|
1814
|
+
}
|
|
1815
|
+
function effect(...operations) {
|
|
1816
|
+
return attachSpec(
|
|
1817
|
+
function(model, stack) {
|
|
1818
|
+
var transition2 = findTransition(stack);
|
|
1819
|
+
if (!transition2) {
|
|
1820
|
+
return void 0;
|
|
1821
|
+
}
|
|
1822
|
+
pushBehaviors(join(transition2.qualifiedName, "effect"), kinds.Sequential, transition2.effect, model, operations);
|
|
1823
|
+
return transition2;
|
|
1824
|
+
},
|
|
1825
|
+
{
|
|
1826
|
+
kind: "effect",
|
|
1827
|
+
value: operations
|
|
1828
|
+
}
|
|
1829
|
+
);
|
|
1830
|
+
}
|
|
1831
|
+
function guard(expression) {
|
|
1832
|
+
return attachSpec(
|
|
1833
|
+
function(model, stack) {
|
|
1834
|
+
var transition2 = findTransition(stack);
|
|
1835
|
+
if (!transition2) {
|
|
1836
|
+
return void 0;
|
|
1837
|
+
}
|
|
1838
|
+
var name2 = join(transition2.qualifiedName, "guard");
|
|
1839
|
+
var constraint = {
|
|
1840
|
+
qualifiedName: name2,
|
|
1841
|
+
kind: kinds.Constraint,
|
|
1842
|
+
expression: typeof expression === "function" ? expression : resolveOperation(model, expression)
|
|
1843
|
+
};
|
|
1844
|
+
model.members[name2] = constraint;
|
|
1845
|
+
transition2.guard = name2;
|
|
1846
|
+
},
|
|
1847
|
+
{
|
|
1848
|
+
kind: "guard",
|
|
1849
|
+
value: expression
|
|
1850
|
+
}
|
|
1851
|
+
);
|
|
1852
|
+
}
|
|
1853
|
+
function attribute(name2, maybeDefault) {
|
|
1854
|
+
var hasDefault = arguments.length > 1;
|
|
1855
|
+
return attachSpec(
|
|
1856
|
+
function(model) {
|
|
1857
|
+
var qualifiedName2 = qualifyModelName(model, name2);
|
|
1858
|
+
model.attributes[qualifiedName2] = {
|
|
1859
|
+
name: name2,
|
|
1860
|
+
qualifiedName: qualifiedName2,
|
|
1861
|
+
hasDefault,
|
|
1862
|
+
defaultValue: maybeDefault
|
|
1863
|
+
};
|
|
1864
|
+
return void 0;
|
|
1865
|
+
},
|
|
1866
|
+
{
|
|
1867
|
+
kind: "attribute",
|
|
1868
|
+
name: name2,
|
|
1869
|
+
value: maybeDefault
|
|
1870
|
+
}
|
|
1871
|
+
);
|
|
1872
|
+
}
|
|
1873
|
+
function operation(name2, implementation) {
|
|
1874
|
+
return attachSpec(
|
|
1875
|
+
function(model) {
|
|
1876
|
+
var qualifiedName2 = qualifyModelName(model, name2);
|
|
1877
|
+
model.operations[qualifiedName2] = {
|
|
1878
|
+
name: name2,
|
|
1879
|
+
qualifiedName: qualifiedName2,
|
|
1880
|
+
implementation
|
|
1881
|
+
};
|
|
1882
|
+
return void 0;
|
|
1883
|
+
},
|
|
1884
|
+
{
|
|
1885
|
+
kind: "operation",
|
|
1886
|
+
name: name2,
|
|
1887
|
+
impl: implementation
|
|
1888
|
+
}
|
|
1889
|
+
);
|
|
1890
|
+
}
|
|
1891
|
+
function after(duration) {
|
|
1892
|
+
return attachSpec(function(model, stack) {
|
|
1893
|
+
var transition2 = findTransition(stack);
|
|
1894
|
+
if (!transition2) {
|
|
1895
|
+
return void 0;
|
|
1896
|
+
}
|
|
1897
|
+
var eventName = join(transition2.qualifiedName, "after_" + Object.keys(model.members).length);
|
|
1898
|
+
var event2 = {
|
|
1899
|
+
name: eventName,
|
|
1900
|
+
kind: kinds.TimeEvent
|
|
1901
|
+
};
|
|
1902
|
+
transition2.events.push(eventName);
|
|
1903
|
+
registerEvent(model, event2);
|
|
1904
|
+
model.partials.push(function() {
|
|
1905
|
+
var source2 = getState(model, transition2.source);
|
|
1906
|
+
if (!source2) {
|
|
1907
|
+
return;
|
|
1908
|
+
}
|
|
1909
|
+
pushBehaviors(
|
|
1910
|
+
join(source2.qualifiedName, "activity_after_" + source2.activities.length),
|
|
1911
|
+
kinds.Concurrent,
|
|
1912
|
+
// Activities can be concurrent/asynchronous
|
|
1913
|
+
source2.activities,
|
|
1914
|
+
model,
|
|
1915
|
+
[
|
|
1916
|
+
function(ctx, instance, evt) {
|
|
1917
|
+
var runtime = runtimeFor(instance);
|
|
1918
|
+
if (!runtime) {
|
|
1919
|
+
return;
|
|
1920
|
+
}
|
|
1921
|
+
var delay = typeof duration === "string" ? coerceDuration(instance.get(duration)) : duration(ctx, instance, evt);
|
|
1922
|
+
return withThenable(delay, function(value) {
|
|
1923
|
+
if (ctx.done) {
|
|
1924
|
+
return;
|
|
1925
|
+
}
|
|
1926
|
+
var coerceDelay = coerceDuration(value);
|
|
1927
|
+
if (coerceDelay <= 0) {
|
|
1928
|
+
return;
|
|
1929
|
+
}
|
|
1930
|
+
return new Promise(function(resolve) {
|
|
1931
|
+
var timeout = runtime.clock.setTimeout(function() {
|
|
1932
|
+
instance.dispatch(event2);
|
|
1933
|
+
resolve();
|
|
1934
|
+
}, coerceDelay);
|
|
1935
|
+
ctx.addEventListener("done", function() {
|
|
1936
|
+
runtime.clock.clearTimeout(timeout);
|
|
1937
|
+
resolve();
|
|
1938
|
+
});
|
|
1939
|
+
});
|
|
1940
|
+
});
|
|
1941
|
+
}
|
|
1942
|
+
]
|
|
1943
|
+
);
|
|
1944
|
+
});
|
|
1945
|
+
}, {
|
|
1946
|
+
kind: "after",
|
|
1947
|
+
value: duration
|
|
1948
|
+
});
|
|
1949
|
+
}
|
|
1950
|
+
function every(duration) {
|
|
1951
|
+
return attachSpec(function(model, stack) {
|
|
1952
|
+
var transition2 = findTransition(stack);
|
|
1953
|
+
if (!transition2) {
|
|
1954
|
+
return void 0;
|
|
1955
|
+
}
|
|
1956
|
+
var eventName = join(transition2.qualifiedName, "every_" + Object.keys(model.members).length);
|
|
1957
|
+
var event2 = {
|
|
1958
|
+
name: eventName,
|
|
1959
|
+
kind: kinds.TimeEvent
|
|
1960
|
+
};
|
|
1961
|
+
transition2.events.push(eventName);
|
|
1962
|
+
registerEvent(model, event2);
|
|
1963
|
+
model.partials.push(function() {
|
|
1964
|
+
var source2 = getState(model, transition2.source);
|
|
1965
|
+
if (!source2) {
|
|
1966
|
+
return;
|
|
1967
|
+
}
|
|
1968
|
+
pushBehaviors(
|
|
1969
|
+
join(source2.qualifiedName, "activity_every_" + source2.activities.length),
|
|
1970
|
+
kinds.Concurrent,
|
|
1971
|
+
// Activities can be concurrent/asynchronous
|
|
1972
|
+
source2.activities,
|
|
1973
|
+
model,
|
|
1974
|
+
[
|
|
1975
|
+
function(ctx, instance, evt) {
|
|
1976
|
+
var runtime = runtimeFor(instance);
|
|
1977
|
+
if (!runtime) {
|
|
1978
|
+
return;
|
|
1979
|
+
}
|
|
1980
|
+
var interval = typeof duration === "string" ? coerceDuration(instance.get(duration)) : duration(ctx, instance, evt);
|
|
1981
|
+
return withThenable(interval, function(value) {
|
|
1982
|
+
if (ctx.done) {
|
|
1983
|
+
return;
|
|
1984
|
+
}
|
|
1985
|
+
var coerceInterval = coerceDuration(value);
|
|
1986
|
+
if (coerceInterval <= 0) {
|
|
1987
|
+
return;
|
|
1988
|
+
}
|
|
1989
|
+
return new Promise(function(resolve) {
|
|
1990
|
+
var timeout = runtime.clock.setTimeout(function tick() {
|
|
1991
|
+
if (ctx.done) {
|
|
1992
|
+
runtime.clock.clearTimeout(timeout);
|
|
1993
|
+
resolve();
|
|
1994
|
+
return;
|
|
1995
|
+
}
|
|
1996
|
+
instance.dispatch(event2);
|
|
1997
|
+
timeout = runtime.clock.setTimeout(tick, coerceInterval);
|
|
1998
|
+
}, coerceInterval);
|
|
1999
|
+
ctx.addEventListener("done", function() {
|
|
2000
|
+
runtime.clock.clearTimeout(timeout);
|
|
2001
|
+
resolve();
|
|
2002
|
+
});
|
|
2003
|
+
});
|
|
2004
|
+
});
|
|
2005
|
+
}
|
|
2006
|
+
]
|
|
2007
|
+
);
|
|
2008
|
+
});
|
|
2009
|
+
}, {
|
|
2010
|
+
kind: "every",
|
|
2011
|
+
value: duration
|
|
2012
|
+
});
|
|
2013
|
+
}
|
|
2014
|
+
function at(timepoint) {
|
|
2015
|
+
return attachSpec(function(model, stack) {
|
|
2016
|
+
var transition2 = findTransition(stack);
|
|
2017
|
+
if (!transition2) {
|
|
2018
|
+
return void 0;
|
|
2019
|
+
}
|
|
2020
|
+
var eventName = join(transition2.qualifiedName, "at_" + Object.keys(model.members).length);
|
|
2021
|
+
var event2 = {
|
|
2022
|
+
name: eventName,
|
|
2023
|
+
kind: kinds.TimeEvent
|
|
2024
|
+
};
|
|
2025
|
+
transition2.events.push(eventName);
|
|
2026
|
+
registerEvent(model, event2);
|
|
2027
|
+
model.partials.push(function() {
|
|
2028
|
+
var source2 = getState(model, transition2.source);
|
|
2029
|
+
if (!source2) {
|
|
2030
|
+
return;
|
|
2031
|
+
}
|
|
2032
|
+
pushBehaviors(
|
|
2033
|
+
join(source2.qualifiedName, "activity_at_" + source2.activities.length),
|
|
2034
|
+
kinds.Concurrent,
|
|
2035
|
+
source2.activities,
|
|
2036
|
+
model,
|
|
2037
|
+
[function(ctx, instance, evt) {
|
|
2038
|
+
var runtime = runtimeFor(instance);
|
|
2039
|
+
if (!runtime) {
|
|
2040
|
+
return;
|
|
2041
|
+
}
|
|
2042
|
+
var timepointValue = typeof timepoint === "string" ? coerceTimepoint(instance.get(timepoint)) : timepoint(ctx, instance, evt);
|
|
2043
|
+
return withThenable(timepointValue, function(value) {
|
|
2044
|
+
if (ctx.done) {
|
|
2045
|
+
return;
|
|
2046
|
+
}
|
|
2047
|
+
var deadline = coerceTimepoint(value);
|
|
2048
|
+
var delay = deadline - runtime.clock.now();
|
|
2049
|
+
if (delay <= 0) {
|
|
2050
|
+
instance.dispatch(event2);
|
|
2051
|
+
return;
|
|
2052
|
+
}
|
|
2053
|
+
return new Promise(function(resolve) {
|
|
2054
|
+
var timeout = runtime.clock.setTimeout(function() {
|
|
2055
|
+
instance.dispatch(event2);
|
|
2056
|
+
resolve();
|
|
2057
|
+
}, delay);
|
|
2058
|
+
ctx.addEventListener("done", function() {
|
|
2059
|
+
runtime.clock.clearTimeout(timeout);
|
|
2060
|
+
resolve();
|
|
2061
|
+
});
|
|
2062
|
+
});
|
|
2063
|
+
});
|
|
2064
|
+
}]
|
|
2065
|
+
);
|
|
2066
|
+
});
|
|
2067
|
+
}, {
|
|
2068
|
+
kind: "at",
|
|
2069
|
+
value: timepoint
|
|
2070
|
+
});
|
|
2071
|
+
}
|
|
2072
|
+
function defer(...eventNames) {
|
|
2073
|
+
var names = eventNames;
|
|
2074
|
+
return attachSpec(
|
|
2075
|
+
function(model, stack) {
|
|
2076
|
+
var state2 = findState(stack);
|
|
2077
|
+
if (!state2) {
|
|
2078
|
+
return void 0;
|
|
2079
|
+
}
|
|
2080
|
+
for (var i = 0; i < names.length; i++) {
|
|
2081
|
+
state2.deferred.push(names[i]);
|
|
2082
|
+
}
|
|
2083
|
+
return state2;
|
|
2084
|
+
},
|
|
2085
|
+
{
|
|
2086
|
+
kind: "defer",
|
|
2087
|
+
eventNames
|
|
2088
|
+
}
|
|
2089
|
+
);
|
|
2090
|
+
}
|
|
2091
|
+
function final(name2) {
|
|
2092
|
+
return attachSpec(
|
|
2093
|
+
function(model, stack) {
|
|
2094
|
+
var parent = findState(stack);
|
|
2095
|
+
if (!parent) {
|
|
2096
|
+
return void 0;
|
|
2097
|
+
}
|
|
2098
|
+
var qualifiedName2 = join(parent.qualifiedName, name2);
|
|
2099
|
+
var finalState = {
|
|
2100
|
+
qualifiedName: qualifiedName2,
|
|
2101
|
+
kind: kinds.FinalState,
|
|
2102
|
+
entry: [],
|
|
2103
|
+
exit: [],
|
|
2104
|
+
activities: [],
|
|
2105
|
+
deferred: [],
|
|
2106
|
+
transitions: [],
|
|
2107
|
+
initial: void 0
|
|
2108
|
+
};
|
|
2109
|
+
model.members[qualifiedName2] = finalState;
|
|
2110
|
+
return finalState;
|
|
2111
|
+
},
|
|
2112
|
+
{
|
|
2113
|
+
kind: "final",
|
|
2114
|
+
name: name2
|
|
2115
|
+
}
|
|
2116
|
+
);
|
|
2117
|
+
}
|
|
2118
|
+
function shallowHistory(elementOrName) {
|
|
2119
|
+
var partials = slice(arguments, 1);
|
|
2120
|
+
var name2 = "";
|
|
2121
|
+
if (typeof elementOrName === "string") {
|
|
2122
|
+
name2 = elementOrName;
|
|
2123
|
+
} else if (typeof elementOrName === "function") {
|
|
2124
|
+
partials.unshift(elementOrName);
|
|
2125
|
+
}
|
|
2126
|
+
return function(model, stack) {
|
|
2127
|
+
var owner = findState(stack);
|
|
2128
|
+
if (!owner) {
|
|
2129
|
+
return void 0;
|
|
2130
|
+
}
|
|
2131
|
+
if (!name2) {
|
|
2132
|
+
name2 = "shallow_history_" + Object.keys(model.members).length;
|
|
2133
|
+
}
|
|
2134
|
+
var qualifiedName2 = join(owner.qualifiedName, name2);
|
|
2135
|
+
var history = {
|
|
2136
|
+
qualifiedName: qualifiedName2,
|
|
2137
|
+
kind: kinds.ShallowHistory,
|
|
2138
|
+
transitions: []
|
|
2139
|
+
};
|
|
2140
|
+
model.members[qualifiedName2] = history;
|
|
2141
|
+
stack.push(history);
|
|
2142
|
+
apply(model, stack, partials);
|
|
2143
|
+
stack.pop();
|
|
2144
|
+
return history;
|
|
2145
|
+
};
|
|
2146
|
+
}
|
|
2147
|
+
function deepHistory(elementOrName) {
|
|
2148
|
+
var partials = slice(arguments, 1);
|
|
2149
|
+
var name2 = "";
|
|
2150
|
+
if (typeof elementOrName === "string") {
|
|
2151
|
+
name2 = elementOrName;
|
|
2152
|
+
} else if (typeof elementOrName === "function") {
|
|
2153
|
+
partials.unshift(elementOrName);
|
|
2154
|
+
}
|
|
2155
|
+
return function(model, stack) {
|
|
2156
|
+
var owner = findState(stack);
|
|
2157
|
+
if (!owner) {
|
|
2158
|
+
return void 0;
|
|
2159
|
+
}
|
|
2160
|
+
if (!name2) {
|
|
2161
|
+
name2 = "deep_history_" + Object.keys(model.members).length;
|
|
2162
|
+
}
|
|
2163
|
+
var qualifiedName2 = join(owner.qualifiedName, name2);
|
|
2164
|
+
var history = {
|
|
2165
|
+
qualifiedName: qualifiedName2,
|
|
2166
|
+
kind: kinds.DeepHistory,
|
|
2167
|
+
transitions: []
|
|
2168
|
+
};
|
|
2169
|
+
model.members[qualifiedName2] = history;
|
|
2170
|
+
stack.push(history);
|
|
2171
|
+
apply(model, stack, partials);
|
|
2172
|
+
stack.pop();
|
|
2173
|
+
return history;
|
|
2174
|
+
};
|
|
2175
|
+
}
|
|
2176
|
+
function choice(elementOrName) {
|
|
2177
|
+
var partials = slice(arguments, 1);
|
|
2178
|
+
var name2 = "";
|
|
2179
|
+
if (typeof elementOrName === "string") {
|
|
2180
|
+
name2 = elementOrName;
|
|
2181
|
+
} else if (typeof elementOrName === "function") {
|
|
2182
|
+
partials.unshift(elementOrName);
|
|
2183
|
+
}
|
|
2184
|
+
return function(model, stack) {
|
|
2185
|
+
var owner = find(stack, kinds.Transition, kinds.State);
|
|
2186
|
+
if (!owner) {
|
|
2187
|
+
return void 0;
|
|
2188
|
+
}
|
|
2189
|
+
if (isTransitionElement(owner)) {
|
|
2190
|
+
var transition2 = owner;
|
|
2191
|
+
var source2 = transition2.source;
|
|
2192
|
+
var ownerVertex = getVertex(model, source2);
|
|
2193
|
+
if (!ownerVertex) {
|
|
2194
|
+
return void 0;
|
|
2195
|
+
}
|
|
2196
|
+
if (isKind(ownerVertex.kind, kinds.Pseudostate)) {
|
|
2197
|
+
owner = find(stack, kinds.State);
|
|
2198
|
+
} else if (isStateElement(ownerVertex)) {
|
|
2199
|
+
owner = ownerVertex;
|
|
2200
|
+
} else {
|
|
2201
|
+
return void 0;
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
if (!owner || !isStateElement(owner)) {
|
|
2205
|
+
return void 0;
|
|
2206
|
+
}
|
|
2207
|
+
if (name2 === "") {
|
|
2208
|
+
name2 = "choice_" + Object.keys(model.members).length;
|
|
2209
|
+
}
|
|
2210
|
+
var qualifiedName2 = join(owner.qualifiedName, name2);
|
|
2211
|
+
var choice2 = {
|
|
2212
|
+
qualifiedName: qualifiedName2,
|
|
2213
|
+
kind: kinds.Choice,
|
|
2214
|
+
transitions: []
|
|
2215
|
+
};
|
|
2216
|
+
model.members[qualifiedName2] = choice2;
|
|
2217
|
+
stack.push(choice2);
|
|
2218
|
+
apply(model, stack, partials);
|
|
2219
|
+
stack.pop();
|
|
2220
|
+
return choice2;
|
|
2221
|
+
};
|
|
2222
|
+
}
|
|
2223
|
+
function dispatchAll(ctx, event2) {
|
|
2224
|
+
for (var id2 in ctx.instances) {
|
|
2225
|
+
var instance = ctx.instances[id2];
|
|
2226
|
+
instance.dispatch(event2);
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
function define(name2, ...partials) {
|
|
2230
|
+
var model = {
|
|
2231
|
+
qualifiedName: join("/", name2),
|
|
2232
|
+
kind: kinds.Model,
|
|
2233
|
+
members: {},
|
|
2234
|
+
transitions: [],
|
|
2235
|
+
entry: [],
|
|
2236
|
+
exit: [],
|
|
2237
|
+
activities: [],
|
|
2238
|
+
deferred: [],
|
|
2239
|
+
initial: "",
|
|
2240
|
+
transitionMap: {},
|
|
2241
|
+
deferredMap: {},
|
|
2242
|
+
events: {},
|
|
2243
|
+
attributes: {},
|
|
2244
|
+
operations: {},
|
|
2245
|
+
partials: []
|
|
2246
|
+
};
|
|
2247
|
+
model.members[model.qualifiedName] = model;
|
|
2248
|
+
registerEvent(model, InitialEvent);
|
|
2249
|
+
registerEvent(model, FinalEvent);
|
|
2250
|
+
registerEvent(model, ErrorEvent);
|
|
2251
|
+
var stack = [model];
|
|
2252
|
+
apply(model, stack, partials);
|
|
2253
|
+
while (model.partials.length > 0) {
|
|
2254
|
+
var currentPartials = model.partials.slice();
|
|
2255
|
+
model.partials = [];
|
|
2256
|
+
for (var i = 0; i < currentPartials.length; i++) {
|
|
2257
|
+
currentPartials[i](model, stack);
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
buildTransitionTable(model);
|
|
2261
|
+
buildDeferredTable(model);
|
|
2262
|
+
return model;
|
|
2263
|
+
}
|
|
2264
|
+
function dispatchTo(ctx, event2, ...ids) {
|
|
2265
|
+
if (!ids.length) {
|
|
2266
|
+
return dispatchAll(ctx, event2);
|
|
2267
|
+
}
|
|
2268
|
+
for (var i = 0; i < ids.length; i++) {
|
|
2269
|
+
var instance = ctx.instances[ids[i]];
|
|
2270
|
+
if (instance) {
|
|
2271
|
+
instance.dispatch(event2);
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
function get(ctxOrInstance, maybeInstanceOrName, maybeName) {
|
|
2276
|
+
var instance = ctxOrInstance instanceof Context ? maybeInstanceOrName : ctxOrInstance;
|
|
2277
|
+
var name2 = ctxOrInstance instanceof Context ? maybeName : maybeInstanceOrName;
|
|
2278
|
+
if (ctxOrInstance instanceof Context && typeof maybeName !== "string") {
|
|
2279
|
+
return void 0;
|
|
2280
|
+
}
|
|
2281
|
+
if (!name2) {
|
|
2282
|
+
return void 0;
|
|
2283
|
+
}
|
|
2284
|
+
if (!instance) {
|
|
2285
|
+
return void 0;
|
|
2286
|
+
}
|
|
2287
|
+
var runtime = runtimeFor(instance);
|
|
2288
|
+
return runtime ? runtime.get(name2) : void 0;
|
|
2289
|
+
}
|
|
2290
|
+
function set(ctxOrInstance, maybeInstanceOrName, maybeNameOrValue, maybeValue) {
|
|
2291
|
+
var instance = ctxOrInstance instanceof Context ? maybeInstanceOrName : ctxOrInstance;
|
|
2292
|
+
var name2 = ctxOrInstance instanceof Context ? maybeNameOrValue : maybeInstanceOrName;
|
|
2293
|
+
var value = ctxOrInstance instanceof Context ? maybeValue : maybeNameOrValue;
|
|
2294
|
+
if (!instance) {
|
|
2295
|
+
return;
|
|
2296
|
+
}
|
|
2297
|
+
var runtime = runtimeFor(instance);
|
|
2298
|
+
if (runtime) {
|
|
2299
|
+
runtime.set(name2, value);
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
function call(ctxOrInstance, maybeInstanceOrName, maybeName, ...args) {
|
|
2303
|
+
var instance = ctxOrInstance instanceof Context ? maybeInstanceOrName : ctxOrInstance;
|
|
2304
|
+
var name2 = ctxOrInstance instanceof Context ? maybeName : maybeInstanceOrName;
|
|
2305
|
+
if (!instance) {
|
|
2306
|
+
return void 0;
|
|
2307
|
+
}
|
|
2308
|
+
var callArgs = slice(arguments, ctxOrInstance instanceof Context ? 3 : 2);
|
|
2309
|
+
var runtime = runtimeFor(instance);
|
|
2310
|
+
if (runtime) {
|
|
2311
|
+
return runtime.call(name2, ...callArgs);
|
|
2312
|
+
}
|
|
2313
|
+
}
|
|
2314
|
+
function restart(instance, data) {
|
|
2315
|
+
var runtime = runtimeFor(instance);
|
|
2316
|
+
if (runtime) {
|
|
2317
|
+
runtime.restart(data);
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
function takeSnapshot(ctxOrInstance, maybeInstance) {
|
|
2321
|
+
var instance = ctxOrInstance instanceof Context ? maybeInstance : ctxOrInstance;
|
|
2322
|
+
var runtime = runtimeFor(instance);
|
|
2323
|
+
return runtime ? runtime.takeSnapshot() : {
|
|
2324
|
+
id: "",
|
|
2325
|
+
qualifiedName: "",
|
|
2326
|
+
state: "",
|
|
2327
|
+
attributes: {},
|
|
2328
|
+
queueLen: 0,
|
|
2329
|
+
events: []
|
|
2330
|
+
};
|
|
2331
|
+
}
|
|
2332
|
+
function afterProcess(ctx, instance, maybeEvent) {
|
|
2333
|
+
return new Promise(function(resolve) {
|
|
2334
|
+
var runtime = runtimeFor(instance);
|
|
2335
|
+
if (!runtime) {
|
|
2336
|
+
resolve();
|
|
2337
|
+
return;
|
|
2338
|
+
}
|
|
2339
|
+
if (maybeEvent) {
|
|
2340
|
+
runtime.onAfter("processed", maybeEvent.name, function() {
|
|
2341
|
+
resolve();
|
|
2342
|
+
});
|
|
2343
|
+
return;
|
|
2344
|
+
}
|
|
2345
|
+
if (!runtime.processing) {
|
|
2346
|
+
resolve();
|
|
2347
|
+
return;
|
|
2348
|
+
}
|
|
2349
|
+
runtime.onAfter("processed", "__next__", function() {
|
|
2350
|
+
resolve();
|
|
2351
|
+
});
|
|
2352
|
+
});
|
|
2353
|
+
}
|
|
2354
|
+
function afterDispatch(ctx, instance, event2) {
|
|
2355
|
+
return new Promise(function(resolve) {
|
|
2356
|
+
var runtime = runtimeFor(instance);
|
|
2357
|
+
if (!runtime) {
|
|
2358
|
+
resolve();
|
|
2359
|
+
return;
|
|
2360
|
+
}
|
|
2361
|
+
runtime.onAfter("dispatched", event2.name, function() {
|
|
2362
|
+
resolve();
|
|
2363
|
+
});
|
|
2364
|
+
});
|
|
2365
|
+
}
|
|
2366
|
+
function afterEntry(ctx, instance, stateName) {
|
|
2367
|
+
return new Promise(function(resolve) {
|
|
2368
|
+
var runtime = runtimeFor(instance);
|
|
2369
|
+
if (!runtime) {
|
|
2370
|
+
resolve();
|
|
2371
|
+
return;
|
|
2372
|
+
}
|
|
2373
|
+
runtime.onAfter("entered", stateName, function() {
|
|
2374
|
+
resolve();
|
|
2375
|
+
});
|
|
2376
|
+
});
|
|
2377
|
+
}
|
|
2378
|
+
function afterExit(ctx, instance, stateName) {
|
|
2379
|
+
return new Promise(function(resolve) {
|
|
2380
|
+
var runtime = runtimeFor(instance);
|
|
2381
|
+
if (!runtime) {
|
|
2382
|
+
resolve();
|
|
2383
|
+
return;
|
|
2384
|
+
}
|
|
2385
|
+
runtime.onAfter("exited", stateName, function() {
|
|
2386
|
+
resolve();
|
|
2387
|
+
});
|
|
2388
|
+
});
|
|
2389
|
+
}
|
|
2390
|
+
function afterExecuted(ctx, instance, stateOrBehavior) {
|
|
2391
|
+
return new Promise(function(resolve) {
|
|
2392
|
+
var runtime = runtimeFor(instance);
|
|
2393
|
+
if (!runtime) {
|
|
2394
|
+
resolve();
|
|
2395
|
+
return;
|
|
2396
|
+
}
|
|
2397
|
+
runtime.onAfter("executed", stateOrBehavior, function() {
|
|
2398
|
+
resolve();
|
|
2399
|
+
});
|
|
2400
|
+
});
|
|
2401
|
+
}
|
|
2402
|
+
function id(instance) {
|
|
2403
|
+
var runtime = runtimeFor(instance);
|
|
2404
|
+
return runtime ? runtime.id : "";
|
|
2405
|
+
}
|
|
2406
|
+
function qualifiedName(instance) {
|
|
2407
|
+
var runtime = runtimeFor(instance);
|
|
2408
|
+
return runtime ? runtime.model.qualifiedName : "";
|
|
2409
|
+
}
|
|
2410
|
+
function name(instance) {
|
|
2411
|
+
var runtime = runtimeFor(instance);
|
|
2412
|
+
return runtime ? runtime.name : "";
|
|
2413
|
+
}
|
|
2414
|
+
function clock(instance) {
|
|
2415
|
+
if (instance instanceof Group) {
|
|
2416
|
+
return instance.clock();
|
|
2417
|
+
}
|
|
2418
|
+
var runtime = runtimeFor(instance);
|
|
2419
|
+
return runtime ? runtime.clock : DefaultClock;
|
|
2420
|
+
}
|
|
2421
|
+
var Group = class _Group {
|
|
2422
|
+
instances = [];
|
|
2423
|
+
__hsm_group_members;
|
|
2424
|
+
constructor(...instances) {
|
|
2425
|
+
for (var i = 0; i < instances.length; i++) {
|
|
2426
|
+
var instance = instances[i];
|
|
2427
|
+
if (!instance) {
|
|
2428
|
+
continue;
|
|
2429
|
+
}
|
|
2430
|
+
if (instance instanceof _Group) {
|
|
2431
|
+
for (var j = 0; j < instance.instances.length; j++) {
|
|
2432
|
+
this.instances.push(instance.instances[j]);
|
|
2433
|
+
}
|
|
2434
|
+
continue;
|
|
2435
|
+
}
|
|
2436
|
+
this.instances.push(instance);
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
dispatch(event2) {
|
|
2440
|
+
for (var i = 0; i < this.instances.length; i++) {
|
|
2441
|
+
this.instances[i].dispatch(event2);
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
set(name2, value) {
|
|
2445
|
+
for (var i = 0; i < this.instances.length; i++) {
|
|
2446
|
+
this.instances[i].set(name2, value);
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
call(name2, ...args) {
|
|
2450
|
+
if (!this.instances.length) {
|
|
2451
|
+
return void 0;
|
|
2452
|
+
}
|
|
2453
|
+
return this.instances[0].call(name2, ...args);
|
|
2454
|
+
}
|
|
2455
|
+
stop() {
|
|
2456
|
+
for (var i = 0; i < this.instances.length; i++) {
|
|
2457
|
+
stop(this.instances[i]);
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
restart(data) {
|
|
2461
|
+
for (var i = 0; i < this.instances.length; i++) {
|
|
2462
|
+
restart(this.instances[i], data);
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
takeSnapshot() {
|
|
2466
|
+
var members = [];
|
|
2467
|
+
var queueLen = 0;
|
|
2468
|
+
var events = [];
|
|
2469
|
+
var ids = [];
|
|
2470
|
+
var qualifiedNames = [];
|
|
2471
|
+
var states = [];
|
|
2472
|
+
for (var i = 0; i < this.instances.length; i++) {
|
|
2473
|
+
var snapshot = this.instances[i].takeSnapshot();
|
|
2474
|
+
members.push(snapshot);
|
|
2475
|
+
queueLen += snapshot.queueLen;
|
|
2476
|
+
ids.push(snapshot.id);
|
|
2477
|
+
qualifiedNames.push(snapshot.qualifiedName);
|
|
2478
|
+
states.push(snapshot.state);
|
|
2479
|
+
for (var j = 0; j < snapshot.events.length; j++) {
|
|
2480
|
+
events.push(snapshot.events[j]);
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
return {
|
|
2484
|
+
id: ids.join(","),
|
|
2485
|
+
qualifiedName: qualifiedNames.join(","),
|
|
2486
|
+
state: states.join(" | "),
|
|
2487
|
+
attributes: {},
|
|
2488
|
+
queueLen,
|
|
2489
|
+
events,
|
|
2490
|
+
members
|
|
2491
|
+
};
|
|
2492
|
+
}
|
|
2493
|
+
clock() {
|
|
2494
|
+
return DefaultClock;
|
|
2495
|
+
}
|
|
2496
|
+
};
|
|
2497
|
+
function makeGroup() {
|
|
2498
|
+
return new Group(...slice(arguments, 0));
|
|
2499
|
+
}
|
|
2500
|
+
var NullKind = kinds.Null;
|
|
2501
|
+
var ElementKind = kinds.Element;
|
|
2502
|
+
var PartialKind = kinds.Partial;
|
|
2503
|
+
var VertexKind = kinds.Vertex;
|
|
2504
|
+
var ConstraintKind = kinds.Constraint;
|
|
2505
|
+
var BehaviorKind = kinds.Behavior;
|
|
2506
|
+
var NamespaceKind = kinds.Namespace;
|
|
2507
|
+
var ConcurrentKind = kinds.Concurrent;
|
|
2508
|
+
var SequentialKind = kinds.Sequential;
|
|
2509
|
+
var StateMachineKind = kinds.StateMachine;
|
|
2510
|
+
var AttributeKind = kinds.Attribute;
|
|
2511
|
+
var StateKind = kinds.State;
|
|
2512
|
+
var ModelKind = kinds.Model;
|
|
2513
|
+
var TransitionKind = kinds.Transition;
|
|
2514
|
+
var InternalKind = kinds.Internal;
|
|
2515
|
+
var ExternalKind = kinds.External;
|
|
2516
|
+
var LocalKind = kinds.Local;
|
|
2517
|
+
var SelfKind = kinds.Self;
|
|
2518
|
+
var EventKind = kinds.Event;
|
|
2519
|
+
var CompletionEventKind = kinds.CompletionEvent;
|
|
2520
|
+
var ChangeEventKind = kinds.ChangeEvent;
|
|
2521
|
+
var ErrorEventKind = kinds.ErrorEvent;
|
|
2522
|
+
var TimeEventKind = kinds.TimeEvent;
|
|
2523
|
+
var CallEventKind = kinds.CallEvent;
|
|
2524
|
+
var PseudostateKind = kinds.Pseudostate;
|
|
2525
|
+
var InitialKind = kinds.Initial;
|
|
2526
|
+
var FinalStateKind = kinds.FinalState;
|
|
2527
|
+
var ChoiceKind = kinds.Choice;
|
|
2528
|
+
var JunctionKind = kinds.Junction;
|
|
2529
|
+
var DeepHistoryKind = kinds.DeepHistory;
|
|
2530
|
+
var ShallowHistoryKind = kinds.ShallowHistory;
|
|
2531
|
+
var Define = define;
|
|
2532
|
+
var State = state;
|
|
2533
|
+
var Final = final;
|
|
2534
|
+
var ShallowHistory = shallowHistory;
|
|
2535
|
+
var DeepHistory = deepHistory;
|
|
2536
|
+
var Choice = choice;
|
|
2537
|
+
var Transition = transition;
|
|
2538
|
+
var Initial = initial;
|
|
2539
|
+
var Event = event;
|
|
2540
|
+
var On = on;
|
|
2541
|
+
var OnCall = onCall;
|
|
2542
|
+
var OnSet = onSet;
|
|
2543
|
+
var When = when;
|
|
2544
|
+
var After = after;
|
|
2545
|
+
var Every = every;
|
|
2546
|
+
var At = at;
|
|
2547
|
+
var Target = target;
|
|
2548
|
+
var Source = source;
|
|
2549
|
+
var Entry = entry;
|
|
2550
|
+
var Exit = exit;
|
|
2551
|
+
var Activity = activity;
|
|
2552
|
+
var Effect = effect;
|
|
2553
|
+
var Guard = guard;
|
|
2554
|
+
var Defer = defer;
|
|
2555
|
+
var Attribute = attribute;
|
|
2556
|
+
var Operation = operation;
|
|
2557
|
+
var DispatchAll = dispatchAll;
|
|
2558
|
+
var DispatchTo = dispatchTo;
|
|
2559
|
+
var Get = get;
|
|
2560
|
+
var Set = set;
|
|
2561
|
+
var Call = call;
|
|
2562
|
+
var Restart = restart;
|
|
2563
|
+
var TakeSnapshot = takeSnapshot;
|
|
2564
|
+
var MakeGroup = makeGroup;
|
|
2565
|
+
var LCA = lca;
|
|
2566
|
+
var IsAncestor = isAncestor;
|
|
2567
|
+
var ID = id;
|
|
2568
|
+
var QualifiedName = qualifiedName;
|
|
2569
|
+
var Name = name;
|
|
2570
|
+
var Clock = clock;
|
|
2571
|
+
|
|
2572
|
+
// src/muid.ts
|
|
2573
|
+
var defaultEpochValue = 17e11;
|
|
2574
|
+
var make64 = (high, low) => ({
|
|
2575
|
+
high: high >>> 0,
|
|
2576
|
+
low: low >>> 0
|
|
2577
|
+
});
|
|
2578
|
+
var from32 = (value) => make64(0, value >>> 0);
|
|
2579
|
+
var add64 = (a, b) => {
|
|
2580
|
+
const low = a.low + b.low >>> 0;
|
|
2581
|
+
const carry = a.low + b.low > 4294967295 ? 1 : 0;
|
|
2582
|
+
const high = a.high + b.high + carry >>> 0;
|
|
2583
|
+
return make64(high, low);
|
|
2584
|
+
};
|
|
2585
|
+
var sub64 = (a, b) => {
|
|
2586
|
+
const low = a.low - b.low >>> 0;
|
|
2587
|
+
const borrow = a.low < b.low ? 1 : 0;
|
|
2588
|
+
const high = a.high - b.high - borrow >>> 0;
|
|
2589
|
+
return make64(high, low);
|
|
2590
|
+
};
|
|
2591
|
+
var shl64 = (value, bits) => {
|
|
2592
|
+
if (bits === 0) {
|
|
2593
|
+
return value;
|
|
2594
|
+
}
|
|
2595
|
+
if (bits >= 64) {
|
|
2596
|
+
return make64(0, 0);
|
|
2597
|
+
}
|
|
2598
|
+
if (bits >= 32) {
|
|
2599
|
+
return make64(value.low << bits - 32, 0);
|
|
2600
|
+
}
|
|
2601
|
+
const high = value.high << bits | value.low >>> 32 - bits;
|
|
2602
|
+
const low = value.low << bits;
|
|
2603
|
+
return make64(high >>> 0, low >>> 0);
|
|
2604
|
+
};
|
|
2605
|
+
var shr64 = (value, bits) => {
|
|
2606
|
+
if (bits === 0) {
|
|
2607
|
+
return value;
|
|
2608
|
+
}
|
|
2609
|
+
if (bits >= 64) {
|
|
2610
|
+
return make64(0, 0);
|
|
2611
|
+
}
|
|
2612
|
+
if (bits >= 32) {
|
|
2613
|
+
return make64(0, value.high >>> bits - 32);
|
|
2614
|
+
}
|
|
2615
|
+
const low = value.low >>> bits | value.high << 32 - bits;
|
|
2616
|
+
const high = value.high >>> bits;
|
|
2617
|
+
return make64(high >>> 0, low >>> 0);
|
|
2618
|
+
};
|
|
2619
|
+
var or64 = (a, b) => make64((a.high | b.high) >>> 0, (a.low | b.low) >>> 0);
|
|
2620
|
+
var and64 = (a, b) => make64((a.high & b.high) >>> 0, (a.low & b.low) >>> 0);
|
|
2621
|
+
var cmp64 = (a, b) => {
|
|
2622
|
+
if (a.high < b.high) {
|
|
2623
|
+
return -1;
|
|
2624
|
+
}
|
|
2625
|
+
if (a.high > b.high) {
|
|
2626
|
+
return 1;
|
|
2627
|
+
}
|
|
2628
|
+
if (a.low < b.low) {
|
|
2629
|
+
return -1;
|
|
2630
|
+
}
|
|
2631
|
+
if (a.low > b.low) {
|
|
2632
|
+
return 1;
|
|
2633
|
+
}
|
|
2634
|
+
return 0;
|
|
2635
|
+
};
|
|
2636
|
+
var gte64 = (a, b) => cmp64(a, b) >= 0;
|
|
2637
|
+
var toHex64 = (value) => {
|
|
2638
|
+
if (value.high === 0) {
|
|
2639
|
+
return value.low.toString(16);
|
|
2640
|
+
}
|
|
2641
|
+
const highHex = value.high.toString(16);
|
|
2642
|
+
let lowHex = value.low.toString(16);
|
|
2643
|
+
while (lowHex.length < 8) {
|
|
2644
|
+
lowHex = `0${lowHex}`;
|
|
2645
|
+
}
|
|
2646
|
+
return `${highHex}${lowHex}`;
|
|
2647
|
+
};
|
|
2648
|
+
var toString64 = (value) => {
|
|
2649
|
+
if (value.high === 0) {
|
|
2650
|
+
return value.low.toString();
|
|
2651
|
+
}
|
|
2652
|
+
if (value.high < 2097152) {
|
|
2653
|
+
return (value.high * 4294967296 + value.low).toString();
|
|
2654
|
+
}
|
|
2655
|
+
return `0x${toHex64(value)}`;
|
|
2656
|
+
};
|
|
2657
|
+
var toBase32_64 = (value) => {
|
|
2658
|
+
if (value.high === 0) {
|
|
2659
|
+
return value.low.toString(32);
|
|
2660
|
+
}
|
|
2661
|
+
const hex = toHex64(value);
|
|
2662
|
+
let num = 0;
|
|
2663
|
+
let result = "";
|
|
2664
|
+
for (let index = 0; index < hex.length; index += 1) {
|
|
2665
|
+
num = num * 16 + Number.parseInt(hex[index], 16);
|
|
2666
|
+
if (num >= 32) {
|
|
2667
|
+
result += (num % 32).toString(32);
|
|
2668
|
+
num = Math.floor(num / 32);
|
|
2669
|
+
}
|
|
2670
|
+
}
|
|
2671
|
+
if (num > 0) {
|
|
2672
|
+
result = `${num.toString(32)}${result}`;
|
|
2673
|
+
}
|
|
2674
|
+
return result || "0";
|
|
2675
|
+
};
|
|
2676
|
+
var hashString = (value) => {
|
|
2677
|
+
let hash = make64(2166136261, 0);
|
|
2678
|
+
const prime = from32(16777619);
|
|
2679
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
2680
|
+
const char = from32(value.charCodeAt(index));
|
|
2681
|
+
hash = and64(or64(hash, char), make64(0, 4294967295));
|
|
2682
|
+
if (hash.high === 0) {
|
|
2683
|
+
hash = from32(hash.low * prime.low >>> 0);
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
return hash;
|
|
2687
|
+
};
|
|
2688
|
+
var getMachineIdentifier = () => {
|
|
2689
|
+
if (typeof process !== "undefined") {
|
|
2690
|
+
const identifier = process.env.HOSTNAME ?? process.env.COMPUTERNAME ?? process.release?.name;
|
|
2691
|
+
if (identifier) {
|
|
2692
|
+
return identifier;
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2695
|
+
if (typeof navigator !== "undefined") {
|
|
2696
|
+
return `${navigator.userAgent}${navigator.platform}${navigator.hardwareConcurrency ?? ""}`;
|
|
2697
|
+
}
|
|
2698
|
+
return `js-${Math.random().toString(36).slice(2)}`;
|
|
2699
|
+
};
|
|
2700
|
+
var getRandomBytes = (length2) => {
|
|
2701
|
+
const cryptoApi = globalThis.crypto;
|
|
2702
|
+
if (cryptoApi?.getRandomValues) {
|
|
2703
|
+
const values = new Uint8Array(length2);
|
|
2704
|
+
cryptoApi.getRandomValues(values);
|
|
2705
|
+
return Array.from(values);
|
|
2706
|
+
}
|
|
2707
|
+
return Array.from({ length: length2 }, () => Math.floor(Math.random() * 256));
|
|
2708
|
+
};
|
|
2709
|
+
var getDefaultConfig = () => {
|
|
2710
|
+
const epoch = make64(
|
|
2711
|
+
Math.floor(defaultEpochValue / 4294967296),
|
|
2712
|
+
defaultEpochValue % 4294967296
|
|
2713
|
+
);
|
|
2714
|
+
const timestampBitLen = 41;
|
|
2715
|
+
const machineIDBitLen = 14;
|
|
2716
|
+
const maxMachineID = (1 << machineIDBitLen) - 1;
|
|
2717
|
+
const identifier = getMachineIdentifier();
|
|
2718
|
+
let machineID;
|
|
2719
|
+
if (identifier) {
|
|
2720
|
+
const hash = hashString(identifier);
|
|
2721
|
+
machineID = from32(hash.low & maxMachineID);
|
|
2722
|
+
} else {
|
|
2723
|
+
const randomBytes = getRandomBytes(4);
|
|
2724
|
+
let randomValue = 0;
|
|
2725
|
+
for (let index = 0; index < 4; index += 1) {
|
|
2726
|
+
randomValue = randomValue << 8 | randomBytes[index];
|
|
2727
|
+
}
|
|
2728
|
+
machineID = from32(randomValue & maxMachineID);
|
|
2729
|
+
}
|
|
2730
|
+
return {
|
|
2731
|
+
epoch,
|
|
2732
|
+
machineID,
|
|
2733
|
+
machineIDBitLen,
|
|
2734
|
+
timestampBitLen
|
|
2735
|
+
};
|
|
2736
|
+
};
|
|
2737
|
+
var MUID = class {
|
|
2738
|
+
value;
|
|
2739
|
+
constructor(value) {
|
|
2740
|
+
if (typeof value === "number") {
|
|
2741
|
+
this.value = from32(value);
|
|
2742
|
+
return;
|
|
2743
|
+
}
|
|
2744
|
+
if (value && typeof value.high === "number" && typeof value.low === "number") {
|
|
2745
|
+
this.value = make64(value.high, value.low);
|
|
2746
|
+
return;
|
|
2747
|
+
}
|
|
2748
|
+
this.value = make64(0, 0);
|
|
2749
|
+
}
|
|
2750
|
+
toString() {
|
|
2751
|
+
return toBase32_64(this.value);
|
|
2752
|
+
}
|
|
2753
|
+
toHex() {
|
|
2754
|
+
return toHex64(this.value);
|
|
2755
|
+
}
|
|
2756
|
+
toDecimal() {
|
|
2757
|
+
return toString64(this.value);
|
|
2758
|
+
}
|
|
2759
|
+
valueOf() {
|
|
2760
|
+
return this.value;
|
|
2761
|
+
}
|
|
2762
|
+
};
|
|
2763
|
+
var Generator = class {
|
|
2764
|
+
timestampBitLen;
|
|
2765
|
+
machineIDBitLen;
|
|
2766
|
+
epoch;
|
|
2767
|
+
shardIndex;
|
|
2768
|
+
shardBitLen;
|
|
2769
|
+
counterBitLen;
|
|
2770
|
+
timestampBitShift;
|
|
2771
|
+
machineIDShift;
|
|
2772
|
+
shardIndexShift;
|
|
2773
|
+
counterBitMask;
|
|
2774
|
+
machineID;
|
|
2775
|
+
state;
|
|
2776
|
+
constructor(config = {}, shardIndex = 0, shardBitLen = 0) {
|
|
2777
|
+
this.timestampBitLen = config.timestampBitLen ?? 41;
|
|
2778
|
+
this.machineIDBitLen = config.machineIDBitLen ?? 14;
|
|
2779
|
+
this.epoch = config.epoch ?? make64(
|
|
2780
|
+
Math.floor(defaultEpochValue / 4294967296),
|
|
2781
|
+
defaultEpochValue % 4294967296
|
|
2782
|
+
);
|
|
2783
|
+
this.shardIndex = shardIndex;
|
|
2784
|
+
this.shardBitLen = shardBitLen;
|
|
2785
|
+
this.counterBitLen = 64 - this.timestampBitLen - this.machineIDBitLen - this.shardBitLen;
|
|
2786
|
+
this.timestampBitShift = this.machineIDBitLen + this.shardBitLen + this.counterBitLen;
|
|
2787
|
+
this.machineIDShift = this.shardBitLen + this.counterBitLen;
|
|
2788
|
+
this.shardIndexShift = this.counterBitLen;
|
|
2789
|
+
this.counterBitMask = this.counterBitLen >= 32 ? make64(4294967295, 4294967295) : sub64(shl64(from32(1), this.counterBitLen), from32(1));
|
|
2790
|
+
const machineIDMask = this.machineIDBitLen >= 32 ? make64(4294967295, 4294967295) : sub64(shl64(from32(1), this.machineIDBitLen), from32(1));
|
|
2791
|
+
this.machineID = and64(config.machineID ?? from32(0), machineIDMask);
|
|
2792
|
+
this.shardIndex = (this.shardIndex & (1 << Math.min(this.shardBitLen, 31)) - 1) >>> 0;
|
|
2793
|
+
this.state = from32(1);
|
|
2794
|
+
}
|
|
2795
|
+
id() {
|
|
2796
|
+
let now = sub64(from32(Date.now() & 4294967295), this.epoch);
|
|
2797
|
+
const dateNow = Date.now();
|
|
2798
|
+
if (dateNow > 4294967295) {
|
|
2799
|
+
now = sub64(
|
|
2800
|
+
make64(Math.floor(dateNow / 4294967296), dateNow & 4294967295),
|
|
2801
|
+
this.epoch
|
|
2802
|
+
);
|
|
2803
|
+
}
|
|
2804
|
+
const lastTimestamp = shr64(this.state, this.counterBitLen);
|
|
2805
|
+
let counter2 = and64(this.state, this.counterBitMask);
|
|
2806
|
+
if (cmp64(now, lastTimestamp) < 0) {
|
|
2807
|
+
now = lastTimestamp;
|
|
2808
|
+
}
|
|
2809
|
+
if (cmp64(now, lastTimestamp) === 0) {
|
|
2810
|
+
if (gte64(counter2, this.counterBitMask)) {
|
|
2811
|
+
now = add64(now, from32(1));
|
|
2812
|
+
counter2 = from32(1);
|
|
2813
|
+
} else {
|
|
2814
|
+
counter2 = add64(counter2, from32(1));
|
|
2815
|
+
}
|
|
2816
|
+
} else {
|
|
2817
|
+
counter2 = from32(1);
|
|
2818
|
+
}
|
|
2819
|
+
this.state = or64(shl64(now, this.counterBitLen), counter2);
|
|
2820
|
+
const timestampPart = shl64(now, this.timestampBitShift);
|
|
2821
|
+
const machineIDPart = shl64(this.machineID, this.machineIDShift);
|
|
2822
|
+
const shardIndexPart = shl64(from32(this.shardIndex), this.shardIndexShift);
|
|
2823
|
+
return new MUID(
|
|
2824
|
+
or64(or64(or64(timestampPart, machineIDPart), shardIndexPart), counter2)
|
|
2825
|
+
);
|
|
2826
|
+
}
|
|
2827
|
+
};
|
|
2828
|
+
var ShardedGenerators = class {
|
|
2829
|
+
pool;
|
|
2830
|
+
size;
|
|
2831
|
+
index = 0;
|
|
2832
|
+
constructor() {
|
|
2833
|
+
const numCPU = typeof navigator !== "undefined" && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4;
|
|
2834
|
+
const shardBits = numCPU > 1 ? Math.min(Math.ceil(Math.log2(numCPU)), 5) : 0;
|
|
2835
|
+
const defaultConfig = getDefaultConfig();
|
|
2836
|
+
this.size = 1 << shardBits;
|
|
2837
|
+
this.pool = [];
|
|
2838
|
+
for (let index = 0; index < this.size; index += 1) {
|
|
2839
|
+
this.pool.push(new Generator(defaultConfig, index, shardBits));
|
|
2840
|
+
}
|
|
2841
|
+
}
|
|
2842
|
+
next() {
|
|
2843
|
+
const generator = this.pool[this.index];
|
|
2844
|
+
this.index = (this.index + 1) % this.size;
|
|
2845
|
+
return generator;
|
|
2846
|
+
}
|
|
2847
|
+
};
|
|
2848
|
+
var defaultGenerator = new Generator(getDefaultConfig(), 0, 0);
|
|
2849
|
+
var make = () => defaultGenerator.id();
|
|
2850
|
+
var newGenerator = (config = {}, shardIndex = 0, shardBitLen = 0) => new Generator(
|
|
2851
|
+
{
|
|
2852
|
+
...getDefaultConfig(),
|
|
2853
|
+
...config
|
|
2854
|
+
},
|
|
2855
|
+
shardIndex,
|
|
2856
|
+
shardBitLen
|
|
2857
|
+
);
|
|
2858
|
+
var makeMuid = (prefix) => `${prefix}-${make().toString()}`;
|
|
2859
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2860
|
+
0 && (module.exports = {
|
|
2861
|
+
Activity,
|
|
2862
|
+
After,
|
|
2863
|
+
At,
|
|
2864
|
+
Attribute,
|
|
2865
|
+
AttributeKind,
|
|
2866
|
+
BehaviorKind,
|
|
2867
|
+
Call,
|
|
2868
|
+
CallEventKind,
|
|
2869
|
+
ChangeEventKind,
|
|
2870
|
+
Choice,
|
|
2871
|
+
ChoiceKind,
|
|
2872
|
+
Clock,
|
|
2873
|
+
CompletionEventKind,
|
|
2874
|
+
ConcurrentKind,
|
|
2875
|
+
ConstraintKind,
|
|
2876
|
+
Context,
|
|
2877
|
+
DeepHistory,
|
|
2878
|
+
DeepHistoryKind,
|
|
2879
|
+
DefaultClock,
|
|
2880
|
+
Defer,
|
|
2881
|
+
Define,
|
|
2882
|
+
DispatchAll,
|
|
2883
|
+
DispatchTo,
|
|
2884
|
+
Effect,
|
|
2885
|
+
ElementKind,
|
|
2886
|
+
Entry,
|
|
2887
|
+
ErrorEvent,
|
|
2888
|
+
ErrorEventKind,
|
|
2889
|
+
Event,
|
|
2890
|
+
EventKind,
|
|
2891
|
+
Every,
|
|
2892
|
+
Exit,
|
|
2893
|
+
ExternalKind,
|
|
2894
|
+
Final,
|
|
2895
|
+
FinalEvent,
|
|
2896
|
+
FinalStateKind,
|
|
2897
|
+
Generator,
|
|
2898
|
+
Get,
|
|
2899
|
+
Group,
|
|
2900
|
+
Guard,
|
|
2901
|
+
ID,
|
|
2902
|
+
Initial,
|
|
2903
|
+
InitialEvent,
|
|
2904
|
+
InitialKind,
|
|
2905
|
+
Instance,
|
|
2906
|
+
InternalKind,
|
|
2907
|
+
IsAncestor,
|
|
2908
|
+
IsKind,
|
|
2909
|
+
JunctionKind,
|
|
2910
|
+
Kinds,
|
|
2911
|
+
LCA,
|
|
2912
|
+
LocalKind,
|
|
2913
|
+
MUID,
|
|
2914
|
+
MakeGroup,
|
|
2915
|
+
MakeKind,
|
|
2916
|
+
ModelKind,
|
|
2917
|
+
Name,
|
|
2918
|
+
NamespaceKind,
|
|
2919
|
+
NullKind,
|
|
2920
|
+
On,
|
|
2921
|
+
OnCall,
|
|
2922
|
+
OnSet,
|
|
2923
|
+
Operation,
|
|
2924
|
+
PartialKind,
|
|
2925
|
+
PseudostateKind,
|
|
2926
|
+
QualifiedName,
|
|
2927
|
+
Queue,
|
|
2928
|
+
Restart,
|
|
2929
|
+
SelfKind,
|
|
2930
|
+
SequentialKind,
|
|
2931
|
+
Set,
|
|
2932
|
+
ShallowHistory,
|
|
2933
|
+
ShallowHistoryKind,
|
|
2934
|
+
ShardedGenerators,
|
|
2935
|
+
Source,
|
|
2936
|
+
State,
|
|
2937
|
+
StateKind,
|
|
2938
|
+
StateMachineKind,
|
|
2939
|
+
TakeSnapshot,
|
|
2940
|
+
Target,
|
|
2941
|
+
TimeEventKind,
|
|
2942
|
+
Transition,
|
|
2943
|
+
TransitionKind,
|
|
2944
|
+
VertexKind,
|
|
2945
|
+
When,
|
|
2946
|
+
activity,
|
|
2947
|
+
add64,
|
|
2948
|
+
after,
|
|
2949
|
+
afterDispatch,
|
|
2950
|
+
afterEntry,
|
|
2951
|
+
afterExecuted,
|
|
2952
|
+
afterExit,
|
|
2953
|
+
afterProcess,
|
|
2954
|
+
and64,
|
|
2955
|
+
apply,
|
|
2956
|
+
at,
|
|
2957
|
+
attribute,
|
|
2958
|
+
call,
|
|
2959
|
+
choice,
|
|
2960
|
+
clock,
|
|
2961
|
+
cmp64,
|
|
2962
|
+
deepHistory,
|
|
2963
|
+
defer,
|
|
2964
|
+
define,
|
|
2965
|
+
dirname,
|
|
2966
|
+
dispatchAll,
|
|
2967
|
+
dispatchTo,
|
|
2968
|
+
effect,
|
|
2969
|
+
entry,
|
|
2970
|
+
event,
|
|
2971
|
+
every,
|
|
2972
|
+
exit,
|
|
2973
|
+
final,
|
|
2974
|
+
find,
|
|
2975
|
+
from32,
|
|
2976
|
+
get,
|
|
2977
|
+
getDefaultConfig,
|
|
2978
|
+
guard,
|
|
2979
|
+
id,
|
|
2980
|
+
initial,
|
|
2981
|
+
isAbsolute,
|
|
2982
|
+
isAncestor,
|
|
2983
|
+
isKind,
|
|
2984
|
+
join,
|
|
2985
|
+
kinds,
|
|
2986
|
+
lca,
|
|
2987
|
+
make,
|
|
2988
|
+
make64,
|
|
2989
|
+
makeGroup,
|
|
2990
|
+
makeKind,
|
|
2991
|
+
makeMuid,
|
|
2992
|
+
name,
|
|
2993
|
+
newGenerator,
|
|
2994
|
+
on,
|
|
2995
|
+
onCall,
|
|
2996
|
+
onSet,
|
|
2997
|
+
operation,
|
|
2998
|
+
or64,
|
|
2999
|
+
qualifiedName,
|
|
3000
|
+
restart,
|
|
3001
|
+
set,
|
|
3002
|
+
shallowHistory,
|
|
3003
|
+
shl64,
|
|
3004
|
+
shr64,
|
|
3005
|
+
source,
|
|
3006
|
+
start,
|
|
3007
|
+
state,
|
|
3008
|
+
stop,
|
|
3009
|
+
sub64,
|
|
3010
|
+
takeSnapshot,
|
|
3011
|
+
target,
|
|
3012
|
+
transition,
|
|
3013
|
+
when
|
|
3014
|
+
});
|
|
3015
|
+
//# sourceMappingURL=index.cjs.map
|