piemachine 0.0.2 → 0.0.4

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.
@@ -1,24 +1,27 @@
1
1
  import { ConditionalFunc, Edge, PieMachineConfig } from "./types.js";
2
- export declare class GoToNode<T, N extends string> {
3
- to: N;
2
+ export declare class GoToNode<T> {
3
+ to: string;
4
4
  data: T;
5
- constructor(to: N, data: T);
5
+ constructor(to: string, data: T);
6
6
  }
7
- export declare function goToNode<T, N extends string>(to: N, data: T): GoToNode<T, N>;
8
- export declare class PieMachine<T, N extends string> {
7
+ export declare function goToNode<T>(to: string, data: T): GoToNode<T>;
8
+ export declare class PieMachine<T> {
9
9
  private nodes;
10
10
  private edges;
11
11
  private config;
12
12
  private statelogClient;
13
- constructor(nodes: readonly N[], config?: PieMachineConfig<T>);
14
- node(id: N, func: (data: T) => Promise<T | GoToNode<T, N>>): void;
15
- edge(from: N, to: N): void;
16
- conditionalEdge<const Adjacent extends N>(from: N, adjacentNodes: readonly Adjacent[], to?: ConditionalFunc<T, Adjacent>): void;
13
+ private nodesTraversed;
14
+ constructor(nodes: readonly string[], config?: PieMachineConfig<T>);
15
+ node(id: string, func: (data: T) => Promise<T | GoToNode<T>>): void;
16
+ edge(from: string, to: string): void;
17
+ conditionalEdge<const Adjacent extends string>(from: string, adjacentNodes: readonly Adjacent[], to?: ConditionalFunc<T, Adjacent>): void;
17
18
  debug(message: string, data?: T): void;
18
- run(startId: N, input: T): Promise<T>;
19
- runAndValidate(nodeFunc: (data: T) => Promise<T | GoToNode<T, N>>, currentId: N, _data: T, retries?: number): Promise<T | GoToNode<T, N>>;
19
+ getNodesTraversed(): readonly string[];
20
+ run(startId: string, input: T): Promise<T>;
21
+ runAndValidate(nodeFunc: (data: T) => Promise<T | GoToNode<T>>, currentId: string, _data: T, retries?: number): Promise<T | GoToNode<T>>;
20
22
  prettyPrint(): void;
21
- prettyPrintEdge(edge: Edge<T, N>): string;
23
+ prettyPrintEdge(edge: Edge<T, string>): string;
22
24
  toMermaid(): string;
25
+ merge(another: PieMachine<T>): void;
23
26
  private validateGoToNodeTarget;
24
27
  }
@@ -26,6 +26,7 @@ export class PieMachine {
26
26
  this.nodes = {};
27
27
  this.edges = {};
28
28
  this.statelogClient = null;
29
+ this.nodesTraversed = [];
29
30
  this.config = config;
30
31
  if (config.statelog) {
31
32
  this.statelogClient = new StatelogClient({
@@ -67,9 +68,13 @@ export class PieMachine {
67
68
  }
68
69
  //this.statelogClient?.debug(message, data || {});
69
70
  }
71
+ getNodesTraversed() {
72
+ return this.nodesTraversed;
73
+ }
70
74
  run(startId, input) {
71
75
  return __awaiter(this, void 0, void 0, function* () {
72
76
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
77
+ this.nodesTraversed = [];
73
78
  const jsonEdges = {};
74
79
  for (const from in this.edges) {
75
80
  jsonEdges[from] = edgeToJSON(this.edges[from]);
@@ -82,6 +87,7 @@ export class PieMachine {
82
87
  let currentId = startId;
83
88
  let data = input;
84
89
  while (currentId) {
90
+ this.nodesTraversed.push(currentId);
85
91
  const nodeFunc = this.nodes[currentId];
86
92
  if (!nodeFunc) {
87
93
  throw new PieMachineError(`Node function for ${currentId} not found.`);
@@ -174,7 +180,11 @@ export class PieMachine {
174
180
  currentId = nextId;
175
181
  }
176
182
  else {
177
- throw new PieMachineError(`Expected ${currentId} to return a GoToNode, as no function was specified for the conditional edges to ${edge.adjacentNodes.join(", ")}.`);
183
+ this.debug(`Exiting graph from node: ${color.green(currentId)}`);
184
+ currentId = null;
185
+ /* throw new PieMachineError(
186
+ `Expected ${currentId} to return a GoToNode, as no function was specified for the conditional edges to ${edge.adjacentNodes.join(", ")}.`,
187
+ ); */
178
188
  }
179
189
  }
180
190
  }
@@ -239,6 +249,22 @@ export class PieMachine {
239
249
  }
240
250
  return mermaid;
241
251
  }
252
+ merge(another) {
253
+ for (const nodeId in another.nodes) {
254
+ if (this.nodes[nodeId]) {
255
+ throw new PieMachineError(`Node ${nodeId} already exists in the current PieMachine.`);
256
+ }
257
+ this.nodes[nodeId] =
258
+ another.nodes[nodeId];
259
+ }
260
+ for (const from in another.edges) {
261
+ if (this.edges[from]) {
262
+ throw new PieMachineError(`Edge from ${from} already exists in the current PieMachine.`);
263
+ }
264
+ this.edges[from] =
265
+ another.edges[from];
266
+ }
267
+ }
242
268
  validateGoToNodeTarget(to, edge) {
243
269
  if (!isRegularEdge(edge)) {
244
270
  if (edge.adjacentNodes.includes(to)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piemachine",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "A general-purpose library for defining and running state machines. Easy as pie.",
5
5
  "homepage": "https://github.com/egonSchiele/piemachine",
6
6
  "scripts": {