@bian-womp/spark-graph 0.3.44 → 0.3.45

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/lib/cjs/index.cjs CHANGED
@@ -495,7 +495,8 @@ Registry.idCounter = 0;
495
495
  * This is the ONLY place where nodes, edges, and resolvedByNode are directly updated.
496
496
  */
497
497
  class Graph {
498
- constructor(registry) {
498
+ constructor(eventEmitter, registry) {
499
+ this.eventEmitter = eventEmitter;
499
500
  this.registry = registry;
500
501
  this.nodes = new Map();
501
502
  this.edges = [];
@@ -552,7 +553,7 @@ class Graph {
552
553
  /**
553
554
  * Update node inputs
554
555
  */
555
- updateNodeInput(nodeId, handle, value) {
556
+ updateNodeInput(nodeId, handle, value, calculated = true) {
556
557
  const node = this.getNodeMutable(nodeId);
557
558
  if (!node)
558
559
  return;
@@ -562,15 +563,16 @@ class Graph {
562
563
  else {
563
564
  node.inputs[handle] = value;
564
565
  }
565
- }
566
- /**
567
- * Delete a node input handle
568
- */
569
- deleteNodeInput(nodeId, handle) {
570
- const node = this.nodes.get(nodeId);
571
- if (!node)
572
- return;
573
- delete node.inputs[handle];
566
+ if (calculated) {
567
+ this.updateNodeLastInputAt(nodeId, handle, Date.now());
568
+ }
569
+ this.eventEmitter.emit("value", {
570
+ nodeId,
571
+ handle,
572
+ value,
573
+ io: "input",
574
+ runtimeTypeId: unwrapTypeId(value),
575
+ });
574
576
  }
575
577
  /**
576
578
  * Update node outputs
@@ -579,7 +581,19 @@ class Graph {
579
581
  const node = this.nodes.get(nodeId);
580
582
  if (!node)
581
583
  return;
582
- node.outputs[handle] = value;
584
+ if (value === undefined) {
585
+ delete node.outputs[handle];
586
+ }
587
+ else {
588
+ node.outputs[handle] = value;
589
+ }
590
+ this.eventEmitter.emit("value", {
591
+ nodeId,
592
+ handle,
593
+ value,
594
+ io: "output",
595
+ runtimeTypeId: unwrapTypeId(value),
596
+ });
583
597
  }
584
598
  /**
585
599
  * Update node state
@@ -652,9 +666,8 @@ class Graph {
652
666
  const node = this.nodes.get(nodeId);
653
667
  if (!node)
654
668
  return;
655
- if (!node.lastInputAt) {
669
+ if (!node.lastInputAt)
656
670
  node.lastInputAt = {};
657
- }
658
671
  node.lastInputAt[handle] = timestamp;
659
672
  }
660
673
  /**
@@ -1977,13 +1990,6 @@ class EdgePropagator {
1977
1990
  return false;
1978
1991
  }
1979
1992
  this.graph.updateNodeOutput(srcNodeId, srcHandle, value);
1980
- this.eventEmitter.emit("value", {
1981
- nodeId: srcNodeId,
1982
- handle: srcHandle,
1983
- value,
1984
- io: "output",
1985
- runtimeTypeId: unwrapTypeId(value),
1986
- });
1987
1993
  return true;
1988
1994
  }
1989
1995
  /**
@@ -2205,16 +2211,7 @@ class EdgePropagator {
2205
2211
  * Set target input value and emit event
2206
2212
  */
2207
2213
  setTargetInput(edge, dstNode, value) {
2208
- const now = Date.now();
2209
2214
  this.graph.updateNodeInput(edge.target.nodeId, edge.target.handle, value);
2210
- this.graph.updateNodeLastInputAt(edge.target.nodeId, edge.target.handle, now);
2211
- this.eventEmitter.emit("value", {
2212
- nodeId: edge.target.nodeId,
2213
- handle: edge.target.handle,
2214
- value,
2215
- io: "input",
2216
- runtimeTypeId: unwrapTypeId(value),
2217
- });
2218
2215
  this.handleResolver.scheduleRecomputeHandles(edge.target.nodeId);
2219
2216
  }
2220
2217
  /**
@@ -3024,8 +3021,8 @@ class GraphRuntime {
3024
3021
  this.pauseRefCount = 0;
3025
3022
  this.persistentPauseToken = null;
3026
3023
  // Initialize components
3027
- this.graph = new Graph();
3028
3024
  this.eventEmitter = new EventEmitter();
3025
+ this.graph = new Graph(this.eventEmitter);
3029
3026
  this.runContextManager = new RunContextManager(this.graph, "debug");
3030
3027
  this.handleResolver = new HandleResolver(this.graph, this.eventEmitter, this.runContextManager, this);
3031
3028
  this.edgePropagator = new EdgePropagator(this.graph, this.eventEmitter, this.runContextManager, this.handleResolver, this);
@@ -3439,14 +3436,7 @@ class GraphRuntime {
3439
3436
  const clonedValue = structuredClone(v);
3440
3437
  const same = valuesEqual(prev, clonedValue);
3441
3438
  if (!same) {
3442
- this.graph.updateNodeInput(nodeId, h, clonedValue);
3443
- this.eventEmitter.emit("value", {
3444
- nodeId,
3445
- handle: h,
3446
- value: clonedValue,
3447
- io: "input",
3448
- runtimeTypeId: unwrapTypeId(clonedValue),
3449
- });
3439
+ this.graph.updateNodeInput(nodeId, h, clonedValue, false);
3450
3440
  nodeChanged = true;
3451
3441
  }
3452
3442
  }
@@ -3461,13 +3451,6 @@ class GraphRuntime {
3461
3451
  for (const [h, v] of Object.entries(map || {})) {
3462
3452
  const clonedValue = structuredClone(v);
3463
3453
  this.graph.updateNodeOutput(nodeId, h, clonedValue);
3464
- this.eventEmitter.emit("value", {
3465
- nodeId,
3466
- handle: h,
3467
- value: clonedValue,
3468
- io: "output",
3469
- runtimeTypeId: unwrapTypeId(clonedValue),
3470
- });
3471
3454
  }
3472
3455
  }
3473
3456
  // Trigger handle resolution for nodes with changed inputs
@@ -3657,7 +3640,7 @@ class GraphRuntime {
3657
3640
  if (!currSet.has(handle)) {
3658
3641
  const node = this.graph.getNode(nodeId);
3659
3642
  if (node && handle in node.inputs) {
3660
- this.graph.deleteNodeInput(nodeId, handle);
3643
+ this.graph.updateNodeInput(nodeId, handle, undefined);
3661
3644
  changed = true;
3662
3645
  }
3663
3646
  }