@mantaq/traversal 0.1.0 → 0.1.1

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 CHANGED
@@ -1,4 +1,4 @@
1
- import { AnyActor, Context, Snapshot } from "@mantaq/core";
1
+ import { AnyActor, Context, 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, options?: {
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,25 @@ 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<Record<string, unknown>>;
80
+ snapshot(): Snapshot<C>;
81
81
  regions: Record<string, AnyActor>;
82
- context?: Record<string, unknown>;
82
+ context?: C;
83
83
  clock: AnyActor["clock"];
84
- on(event: "change", fn: (snapshot: Snapshot<Record<string, unknown>>, prev: Snapshot<Record<string, unknown>>) => void): () => void;
85
- on(event: "error", fn: (error: unknown) => void): () => void;
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
+ recover(target: {
88
+ state: AnyActor["state"];
89
+ context: C;
90
+ }): void;
87
91
  settled(): Promise<void>;
88
92
  options?: AnyActor["options"];
89
93
  }
90
- declare function instrument(actor: AnyActor): InstrumentedActor;
94
+ declare function instrument<C>(actor: AnyActor<C>): InstrumentedActor<C>;
91
95
  //#endregion
92
96
  //#region src/algorithms.d.ts
93
97
  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
- console.error("[mantaq/traversal] buildGraph failed:", error);
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, hooks) {
279
- const { history, origSend, pendingEventId } = hooks;
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,39 @@ function wrapWithProxy(actor, hooks) {
297
293
  return actor.options;
298
294
  },
299
295
  send(event) {
300
- const { type: eventType } = trackSendEvent(history, event);
301
- pendingEventId.value = eventType;
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
+ recover: actor.recover.bind(actor),
312
304
  settled: actor.settled.bind(actor)
313
305
  };
314
306
  }
315
- function recordTransition(history, from, to, eventId) {
307
+ function recordTransition(history, rec) {
316
308
  history.append({
317
309
  type: "transition",
318
310
  data: {
319
- from,
320
- event: eventId,
321
- to,
311
+ from: rec.from,
312
+ event: rec.event,
313
+ to: rec.to,
322
314
  timestamp: Date.now()
323
315
  }
324
316
  });
317
+ if (!rec.transitioned) return;
325
318
  history.append({
326
319
  type: "state_visit",
327
320
  data: {
328
- stateName: to,
321
+ stateName: rec.to,
329
322
  timestamp: Date.now()
330
323
  }
331
324
  });
332
- history.append({
325
+ if ((rec.effects[rec.to] ?? []).length > 0) history.append({
333
326
  type: "effect",
334
327
  data: {
335
- stateName: to,
328
+ stateName: rec.to,
336
329
  timestamp: Date.now()
337
330
  }
338
331
  });
@@ -346,13 +339,9 @@ function trackSendEvent(history, event) {
346
339
  timestamp: Date.now()
347
340
  }
348
341
  });
349
- return { type: eventId };
350
342
  }
351
343
  function instrument(actor) {
352
344
  const history = new History();
353
- const origSend = actor.send.bind(actor);
354
- let prevStateName = actor.state.name;
355
- const pendingEventId = { value: void 0 };
356
345
  history.append({
357
346
  type: "state_visit",
358
347
  data: {
@@ -360,17 +349,16 @@ function instrument(actor) {
360
349
  timestamp: Date.now()
361
350
  }
362
351
  });
363
- const wrapped = wrapWithProxy(actor, {
364
- history,
365
- origSend,
366
- pendingEventId
367
- });
368
- actor.on("change", () => {
369
- const currentName = actor.state.name;
370
- if (currentName !== prevStateName) {
371
- recordTransition(history, prevStateName, currentName, pendingEventId.value ?? "unknown");
372
- prevStateName = currentName;
373
- }
352
+ const wrapped = wrapWithProxy(actor, history);
353
+ const effectsByState = actor.options?.effects ?? {};
354
+ actor.on("transition", ({ event, from, to, transitioned }) => {
355
+ recordTransition(history, {
356
+ from,
357
+ to,
358
+ event: event.type,
359
+ transitioned,
360
+ effects: effectsByState
361
+ });
374
362
  });
375
363
  return wrapped;
376
364
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mantaq/traversal",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Graph extraction, traversal algorithms, and runtime tracking for mantaq actors.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -20,7 +20,7 @@
20
20
  "access": "public"
21
21
  },
22
22
  "dependencies": {
23
- "@mantaq/core": "0.1.0",
23
+ "@mantaq/core": "0.2.0",
24
24
  "@mantaq/utils": "0.0.2"
25
25
  },
26
26
  "devDependencies": {