@mantaq/traversal 0.0.1 → 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/dist/index.d.mts +3 -3
- package/dist/index.mjs +17 -13
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyActor, Snapshot } from "@mantaq/core";
|
|
1
|
+
import { AnyActor, Context, Snapshot } from "@mantaq/core";
|
|
2
2
|
//#region src/types.d.ts
|
|
3
3
|
interface GraphNode {
|
|
4
4
|
id: string;
|
|
@@ -77,11 +77,11 @@ interface InstrumentedActor {
|
|
|
77
77
|
history: History;
|
|
78
78
|
send(event: unknown): void;
|
|
79
79
|
state: AnyActor["state"];
|
|
80
|
-
snapshot(): Snapshot
|
|
80
|
+
snapshot(): Snapshot<Record<string, unknown>>;
|
|
81
81
|
regions: Record<string, AnyActor>;
|
|
82
82
|
context?: Record<string, unknown>;
|
|
83
83
|
clock: AnyActor["clock"];
|
|
84
|
-
on(event: "change", fn: (snapshot: Snapshot) => void): () => void;
|
|
84
|
+
on(event: "change", fn: (snapshot: Snapshot<Record<string, unknown>>, prev: Snapshot<Record<string, unknown>>) => void): () => void;
|
|
85
85
|
on(event: "error", fn: (error: unknown) => void): () => void;
|
|
86
86
|
on(event: "done", fn: () => void): () => void;
|
|
87
87
|
settled(): Promise<void>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Context } from "@mantaq/core";
|
|
1
2
|
import { Either } from "@mantaq/utils";
|
|
2
3
|
//#region src/graph.ts
|
|
3
4
|
const INITIAL_NODE_ID = "__initial__";
|
|
@@ -7,7 +8,7 @@ function collectActiveStates(snapshot, prefix, activeSet) {
|
|
|
7
8
|
if (!currentName) return;
|
|
8
9
|
const fullId = nodeId(prefix, currentName);
|
|
9
10
|
activeSet.add(fullId);
|
|
10
|
-
if (snapshot.regions && !snapshot.done) for (const [regionName, regionSnap] of Object.entries(snapshot.regions)) collectActiveStates(regionSnap, regionName, activeSet);
|
|
11
|
+
if (snapshot.regions && !snapshot.done && !snapshot.error) for (const [regionName, regionSnap] of Object.entries(snapshot.regions)) collectActiveStates(regionSnap, regionName, activeSet);
|
|
11
12
|
}
|
|
12
13
|
function nodeId(prefix, name) {
|
|
13
14
|
return prefix ? `${prefix}.${name}` : name;
|
|
@@ -23,15 +24,18 @@ function buildNodesFromStates(states, activeSet, pathPrefix) {
|
|
|
23
24
|
};
|
|
24
25
|
});
|
|
25
26
|
}
|
|
26
|
-
function invokeHandler(handler, eventId,
|
|
27
|
+
function invokeHandler(handler, eventId, context, actor) {
|
|
27
28
|
return Either.from(() => {
|
|
28
|
-
const result = handler({
|
|
29
|
-
|
|
29
|
+
const result = handler({
|
|
30
|
+
type: eventId,
|
|
31
|
+
payload: {}
|
|
32
|
+
}, {
|
|
33
|
+
context: new Context(() => context, () => {}),
|
|
30
34
|
actor
|
|
31
35
|
});
|
|
32
36
|
return {
|
|
33
37
|
targetName: result?.state?.name,
|
|
34
|
-
emitNames: result?.emit?.map((e) => e.
|
|
38
|
+
emitNames: result?.emit?.map((e) => e.type).filter((type) => Boolean(type)) ?? []
|
|
35
39
|
};
|
|
36
40
|
});
|
|
37
41
|
}
|
|
@@ -58,8 +62,8 @@ function processStateTransitions(stateTransitions, pass) {
|
|
|
58
62
|
for (const [eventId, handler] of Object.entries(stateTransitions)) {
|
|
59
63
|
if (typeof handler !== "function") continue;
|
|
60
64
|
for (const ctxName of pass.traversal.contextNames) {
|
|
61
|
-
const
|
|
62
|
-
const handled = Either.getOrElse(invokeHandler(handler, eventId,
|
|
65
|
+
const sampleContext = pass.traversal.contexts[ctxName];
|
|
66
|
+
const handled = Either.getOrElse(invokeHandler(handler, eventId, sampleContext, pass.traversal.actor), () => ({
|
|
63
67
|
targetName: void 0,
|
|
64
68
|
emitNames: []
|
|
65
69
|
}));
|
|
@@ -78,8 +82,8 @@ function mergeAnyTransitions(anyTransitions, stateTransitionedEvents, pass) {
|
|
|
78
82
|
if (typeof handler !== "function") continue;
|
|
79
83
|
if (stateTransitionedEvents.has(eventId)) continue;
|
|
80
84
|
for (const ctxName of pass.traversal.contextNames) {
|
|
81
|
-
const
|
|
82
|
-
const handled = Either.getOrElse(invokeHandler(handler, eventId,
|
|
85
|
+
const sampleContext = pass.traversal.contexts[ctxName];
|
|
86
|
+
const handled = Either.getOrElse(invokeHandler(handler, eventId, sampleContext, pass.traversal.actor), () => ({
|
|
83
87
|
targetName: void 0,
|
|
84
88
|
emitNames: []
|
|
85
89
|
}));
|
|
@@ -293,8 +297,8 @@ function wrapWithProxy(actor, hooks) {
|
|
|
293
297
|
return actor.options;
|
|
294
298
|
},
|
|
295
299
|
send(event) {
|
|
296
|
-
const {
|
|
297
|
-
pendingEventId.value =
|
|
300
|
+
const { type: eventType } = trackSendEvent(history, event);
|
|
301
|
+
pendingEventId.value = eventType;
|
|
298
302
|
try {
|
|
299
303
|
origSend(event);
|
|
300
304
|
} finally {
|
|
@@ -334,7 +338,7 @@ function recordTransition(history, from, to, eventId) {
|
|
|
334
338
|
});
|
|
335
339
|
}
|
|
336
340
|
function trackSendEvent(history, event) {
|
|
337
|
-
const eventId = event?.
|
|
341
|
+
const eventId = event?.type ?? "unknown";
|
|
338
342
|
history.append({
|
|
339
343
|
type: "send",
|
|
340
344
|
data: {
|
|
@@ -342,7 +346,7 @@ function trackSendEvent(history, event) {
|
|
|
342
346
|
timestamp: Date.now()
|
|
343
347
|
}
|
|
344
348
|
});
|
|
345
|
-
return {
|
|
349
|
+
return { type: eventId };
|
|
346
350
|
}
|
|
347
351
|
function instrument(actor) {
|
|
348
352
|
const history = new History();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mantaq/traversal",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
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.0
|
|
24
|
-
"@mantaq/utils": "0.0.
|
|
23
|
+
"@mantaq/core": "0.1.0",
|
|
24
|
+
"@mantaq/utils": "0.0.2"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^26.2.0",
|