@mantaq/traversal 0.1.0 → 0.1.2
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/dist/index.d.mts +16 -8
- package/dist/index.mjs +26 -36
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyActor, Context, Snapshot } from "@mantaq/core";
|
|
1
|
+
import { AnyActor, Context, ErrorInfo, InternalEvent, Snapshot, TransitionInfo } from "@mantaq/core";
|
|
2
2
|
//#region src/types.d.ts
|
|
3
3
|
interface GraphNode {
|
|
4
4
|
id: string;
|
|
@@ -49,7 +49,7 @@ interface HistoryEntry {
|
|
|
49
49
|
//#region src/graph.d.ts
|
|
50
50
|
declare const INITIAL_NODE_ID = "__initial__";
|
|
51
51
|
declare function collectActiveStates(snapshot: Snapshot, prefix: string, activeSet: Set<string>): void;
|
|
52
|
-
declare function buildGraph(actor: AnyActor
|
|
52
|
+
declare function buildGraph<C>(actor: AnyActor<C>, options?: {
|
|
53
53
|
internalIds?: Set<string>;
|
|
54
54
|
sampleContext?: Record<string, unknown>;
|
|
55
55
|
sampleContexts?: Record<string, Record<string, unknown>>;
|
|
@@ -73,21 +73,29 @@ declare class History {
|
|
|
73
73
|
}
|
|
74
74
|
//#endregion
|
|
75
75
|
//#region src/instrument.d.ts
|
|
76
|
-
interface InstrumentedActor {
|
|
76
|
+
interface InstrumentedActor<C = Record<string, unknown>> {
|
|
77
77
|
history: History;
|
|
78
78
|
send(event: unknown): void;
|
|
79
79
|
state: AnyActor["state"];
|
|
80
|
-
snapshot(): Snapshot<
|
|
80
|
+
snapshot(): Snapshot<C>;
|
|
81
81
|
regions: Record<string, AnyActor>;
|
|
82
|
-
context?:
|
|
82
|
+
context?: C;
|
|
83
83
|
clock: AnyActor["clock"];
|
|
84
|
-
on(event: "change", fn: (snapshot: Snapshot<
|
|
85
|
-
on(event: "
|
|
84
|
+
on(event: "change", fn: (snapshot: Snapshot<C>, prev: Snapshot<C>) => void): () => void;
|
|
85
|
+
on(event: "transition", fn: (info: TransitionInfo) => void): () => void;
|
|
86
86
|
on(event: "done", fn: () => void): () => void;
|
|
87
|
+
on(event: "error", fn: (info: ErrorInfo) => void): () => void;
|
|
88
|
+
on(event: "output", fn: (event: InternalEvent) => void): () => void;
|
|
89
|
+
recover(target: {
|
|
90
|
+
state: AnyActor["state"];
|
|
91
|
+
context: C;
|
|
92
|
+
}): void;
|
|
87
93
|
settled(): Promise<void>;
|
|
88
94
|
options?: AnyActor["options"];
|
|
95
|
+
inject(event: InternalEvent): void;
|
|
96
|
+
dispose(): void;
|
|
89
97
|
}
|
|
90
|
-
declare function instrument(actor: AnyActor): InstrumentedActor
|
|
98
|
+
declare function instrument<C>(actor: AnyActor<C>): InstrumentedActor<C>;
|
|
91
99
|
//#endregion
|
|
92
100
|
//#region src/algorithms.d.ts
|
|
93
101
|
declare function reachable(graph: ActorGraph, fromId: string, toId: string): boolean;
|
package/dist/index.mjs
CHANGED
|
@@ -223,11 +223,7 @@ function buildGraph(actor, options) {
|
|
|
223
223
|
edges
|
|
224
224
|
};
|
|
225
225
|
}), (error) => {
|
|
226
|
-
|
|
227
|
-
return {
|
|
228
|
-
nodes: [],
|
|
229
|
-
edges: []
|
|
230
|
-
};
|
|
226
|
+
throw error;
|
|
231
227
|
}, (result) => result);
|
|
232
228
|
}
|
|
233
229
|
//#endregion
|
|
@@ -275,8 +271,8 @@ var History = class {
|
|
|
275
271
|
};
|
|
276
272
|
//#endregion
|
|
277
273
|
//#region src/instrument.ts
|
|
278
|
-
function wrapWithProxy(actor,
|
|
279
|
-
const
|
|
274
|
+
function wrapWithProxy(actor, history) {
|
|
275
|
+
const origSend = actor.send.bind(actor);
|
|
280
276
|
return {
|
|
281
277
|
get history() {
|
|
282
278
|
return history;
|
|
@@ -297,42 +293,41 @@ function wrapWithProxy(actor, hooks) {
|
|
|
297
293
|
return actor.options;
|
|
298
294
|
},
|
|
299
295
|
send(event) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
try {
|
|
303
|
-
origSend(event);
|
|
304
|
-
} finally {
|
|
305
|
-
pendingEventId.value = void 0;
|
|
306
|
-
}
|
|
296
|
+
trackSendEvent(history, event);
|
|
297
|
+
origSend(event);
|
|
307
298
|
},
|
|
308
299
|
snapshot() {
|
|
309
300
|
return actor.snapshot();
|
|
310
301
|
},
|
|
311
302
|
on: actor.on.bind(actor),
|
|
303
|
+
inject: actor.inject.bind(actor),
|
|
304
|
+
dispose: actor.dispose.bind(actor),
|
|
305
|
+
recover: actor.recover.bind(actor),
|
|
312
306
|
settled: actor.settled.bind(actor)
|
|
313
307
|
};
|
|
314
308
|
}
|
|
315
|
-
function recordTransition(history,
|
|
309
|
+
function recordTransition(history, rec) {
|
|
316
310
|
history.append({
|
|
317
311
|
type: "transition",
|
|
318
312
|
data: {
|
|
319
|
-
from,
|
|
320
|
-
event:
|
|
321
|
-
to,
|
|
313
|
+
from: rec.from,
|
|
314
|
+
event: rec.event,
|
|
315
|
+
to: rec.to,
|
|
322
316
|
timestamp: Date.now()
|
|
323
317
|
}
|
|
324
318
|
});
|
|
319
|
+
if (!rec.transitioned) return;
|
|
325
320
|
history.append({
|
|
326
321
|
type: "state_visit",
|
|
327
322
|
data: {
|
|
328
|
-
stateName: to,
|
|
323
|
+
stateName: rec.to,
|
|
329
324
|
timestamp: Date.now()
|
|
330
325
|
}
|
|
331
326
|
});
|
|
332
|
-
history.append({
|
|
327
|
+
if ((rec.effects[rec.to] ?? []).length > 0) history.append({
|
|
333
328
|
type: "effect",
|
|
334
329
|
data: {
|
|
335
|
-
stateName: to,
|
|
330
|
+
stateName: rec.to,
|
|
336
331
|
timestamp: Date.now()
|
|
337
332
|
}
|
|
338
333
|
});
|
|
@@ -346,13 +341,9 @@ function trackSendEvent(history, event) {
|
|
|
346
341
|
timestamp: Date.now()
|
|
347
342
|
}
|
|
348
343
|
});
|
|
349
|
-
return { type: eventId };
|
|
350
344
|
}
|
|
351
345
|
function instrument(actor) {
|
|
352
346
|
const history = new History();
|
|
353
|
-
const origSend = actor.send.bind(actor);
|
|
354
|
-
let prevStateName = actor.state.name;
|
|
355
|
-
const pendingEventId = { value: void 0 };
|
|
356
347
|
history.append({
|
|
357
348
|
type: "state_visit",
|
|
358
349
|
data: {
|
|
@@ -360,17 +351,16 @@ function instrument(actor) {
|
|
|
360
351
|
timestamp: Date.now()
|
|
361
352
|
}
|
|
362
353
|
});
|
|
363
|
-
const wrapped = wrapWithProxy(actor,
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
}
|
|
354
|
+
const wrapped = wrapWithProxy(actor, history);
|
|
355
|
+
const effectsByState = actor.options?.effects ?? {};
|
|
356
|
+
actor.on("transition", ({ event, from, to, transitioned }) => {
|
|
357
|
+
recordTransition(history, {
|
|
358
|
+
from,
|
|
359
|
+
to,
|
|
360
|
+
event: event.type,
|
|
361
|
+
transitioned,
|
|
362
|
+
effects: effectsByState
|
|
363
|
+
});
|
|
374
364
|
});
|
|
375
365
|
return wrapped;
|
|
376
366
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mantaq/traversal",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Graph extraction, traversal algorithms, and runtime tracking for mantaq actors.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@mantaq/core": "0.
|
|
24
|
-
"@mantaq/utils": "0.0.
|
|
23
|
+
"@mantaq/core": "0.3.0",
|
|
24
|
+
"@mantaq/utils": "0.0.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^26.2.0",
|