@bian-womp/spark-graph 0.2.47 → 0.2.48

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
@@ -566,7 +566,7 @@ class GraphRuntime {
566
566
  if (!this.paused) {
567
567
  // Only schedule if all inbound inputs are present (or there are none)
568
568
  if (anyChanged && this.allInboundHaveValue(nodeId))
569
- this.scheduleInputsChanged(nodeId);
569
+ this.scheduleInputsChangedInternal(nodeId);
570
570
  // Recompute dynamic handles for this node when its direct inputs change
571
571
  if (anyChanged)
572
572
  this.scheduleRecomputeHandles(nodeId);
@@ -650,10 +650,10 @@ class GraphRuntime {
650
650
  state: node.state,
651
651
  setState: (next) => Object.assign(node.state, next),
652
652
  emit: emitHandler,
653
- invalidateDownstream: () => this.invalidateDownstream(nodeId),
653
+ invalidateDownstream: () => this.invalidateDownstreamInternal(nodeId),
654
654
  scheduleInputsChanged: () => {
655
655
  if (this.allInboundHaveValue(nodeId)) {
656
- this.scheduleInputsChanged(nodeId);
656
+ this.scheduleInputsChangedInternal(nodeId);
657
657
  }
658
658
  },
659
659
  getInput: (handle) => inputs[handle],
@@ -663,7 +663,7 @@ class GraphRuntime {
663
663
  reportProgress,
664
664
  };
665
665
  }
666
- scheduleInputsChanged(nodeId) {
666
+ scheduleInputsChangedInternal(nodeId) {
667
667
  const node = this.nodes.get(nodeId);
668
668
  if (!node)
669
669
  return;
@@ -867,7 +867,7 @@ class GraphRuntime {
867
867
  }
868
868
  return effective;
869
869
  }
870
- invalidateDownstream(nodeId) {
870
+ invalidateDownstreamInternal(nodeId) {
871
871
  // Notifies dependents; for now we propagate current outputs
872
872
  for (const e of this.edges.filter((e) => e.source.nodeId === nodeId)) {
873
873
  const value = this.getOutput(nodeId, e.source.handle);
@@ -960,7 +960,7 @@ class GraphRuntime {
960
960
  // Recompute dynamic handles for the destination node on input change
961
961
  this.scheduleRecomputeHandles(e.target.nodeId);
962
962
  if (!this.paused && this.allInboundHaveValue(e.target.nodeId))
963
- this.scheduleInputsChanged(e.target.nodeId);
963
+ this.scheduleInputsChangedInternal(e.target.nodeId);
964
964
  }
965
965
  };
966
966
  if (e.convertAsync) {
@@ -1162,7 +1162,7 @@ class GraphRuntime {
1162
1162
  }
1163
1163
  }
1164
1164
  // Invalidate downstream for this node so UI refreshes
1165
- this.invalidateDownstream(nodeId);
1165
+ this.invalidateDownstreamInternal(nodeId);
1166
1166
  }
1167
1167
  launch(invalidate = false) {
1168
1168
  // call onActivated for nodes that implement it
@@ -1177,7 +1177,7 @@ class GraphRuntime {
1177
1177
  // After activation, schedule nodes that have all inbound inputs present
1178
1178
  for (const nodeId of this.nodes.keys()) {
1179
1179
  if (this.allInboundHaveValue(nodeId))
1180
- this.scheduleInputsChanged(nodeId);
1180
+ this.scheduleInputsChangedInternal(nodeId);
1181
1181
  }
1182
1182
  }
1183
1183
  }
@@ -1190,9 +1190,9 @@ class GraphRuntime {
1190
1190
  typeof event === "object" &&
1191
1191
  event.type === "invalidate") {
1192
1192
  if (this.allInboundHaveValue(nodeId))
1193
- this.scheduleInputsChanged(nodeId);
1193
+ this.scheduleInputsChangedInternal(nodeId);
1194
1194
  else
1195
- this.invalidateDownstream(nodeId);
1195
+ this.invalidateDownstreamInternal(nodeId);
1196
1196
  }
1197
1197
  else {
1198
1198
  node.runtime.onExternalEvent?.(event, node.state);
@@ -1295,11 +1295,11 @@ class GraphRuntime {
1295
1295
  resume() {
1296
1296
  this.paused = false;
1297
1297
  }
1298
- __unsafe_invalidateDownstream(nodeId) {
1299
- this.invalidateDownstream(nodeId);
1298
+ invalidateDownstream(nodeId) {
1299
+ this.invalidateDownstreamInternal(nodeId);
1300
1300
  }
1301
- __unsafe_scheduleInputsChanged(nodeId) {
1302
- this.scheduleInputsChanged(nodeId);
1301
+ scheduleInputsChanged(nodeId) {
1302
+ this.scheduleInputsChangedInternal(nodeId);
1303
1303
  }
1304
1304
  // Hydrate inputs/outputs without triggering computation; optionally re-emit outputs downstream
1305
1305
  hydrate(payload, opts) {
@@ -1496,7 +1496,7 @@ class GraphRuntime {
1496
1496
  if (bucketsForNode.size === 0)
1497
1497
  this.arrayInputBuckets.delete(nodeId);
1498
1498
  }
1499
- this.scheduleInputsChanged(nodeId);
1499
+ this.scheduleInputsChangedInternal(nodeId);
1500
1500
  }
1501
1501
  }
1502
1502
  // Re-emit outputs when per-handle target sets change (precise and simple)
@@ -1539,7 +1539,7 @@ class GraphRuntime {
1539
1539
  if (val !== undefined)
1540
1540
  this.propagate(nodeId, handle, val);
1541
1541
  else if (this.allInboundHaveValue(nodeId))
1542
- this.scheduleInputsChanged(nodeId);
1542
+ this.scheduleInputsChangedInternal(nodeId);
1543
1543
  }
1544
1544
  }
1545
1545
  }
@@ -1919,7 +1919,7 @@ class BatchedEngine extends AbstractEngine {
1919
1919
  this.dirtyNodes.clear();
1920
1920
  this.graphRuntime.resume();
1921
1921
  for (const n of nodes)
1922
- this.graphRuntime.__unsafe_scheduleInputsChanged(n);
1922
+ this.graphRuntime.scheduleInputsChanged(n);
1923
1923
  await this.graphRuntime.whenIdle();
1924
1924
  this.graphRuntime.pause();
1925
1925
  }
@@ -1940,7 +1940,7 @@ class PullEngine extends AbstractEngine {
1940
1940
  // Pull API
1941
1941
  async computeNode(nodeId) {
1942
1942
  this.graphRuntime.resume();
1943
- this.graphRuntime.__unsafe_scheduleInputsChanged(nodeId);
1943
+ this.graphRuntime.scheduleInputsChanged(nodeId);
1944
1944
  await this.graphRuntime.whenIdle();
1945
1945
  this.graphRuntime.pause();
1946
1946
  }
@@ -1969,7 +1969,7 @@ class HybridEngine extends AbstractEngine {
1969
1969
  const nodes = Array.from(this.dirtyNodes);
1970
1970
  this.dirtyNodes.clear();
1971
1971
  for (const n of nodes)
1972
- this.graphRuntime.__unsafe_scheduleInputsChanged(n);
1972
+ this.graphRuntime.scheduleInputsChanged(n);
1973
1973
  if (this.flushTimer) {
1974
1974
  clearTimeout(this.flushTimer);
1975
1975
  this.flushTimer = undefined;
@@ -1999,14 +1999,14 @@ class HybridEngine extends AbstractEngine {
1999
1999
  const nodes = Array.from(this.dirtyNodes);
2000
2000
  this.dirtyNodes.clear();
2001
2001
  for (const n of nodes)
2002
- this.graphRuntime.__unsafe_scheduleInputsChanged(n);
2002
+ this.graphRuntime.scheduleInputsChanged(n);
2003
2003
  this.flushTimer = undefined;
2004
2004
  }, windowMs);
2005
2005
  }
2006
2006
  super.setInputs(nodeId, inputs);
2007
2007
  this.dirtyNodes.add(nodeId);
2008
2008
  if (!this.batching)
2009
- this.graphRuntime.__unsafe_scheduleInputsChanged(nodeId);
2009
+ this.graphRuntime.scheduleInputsChanged(nodeId);
2010
2010
  }
2011
2011
  triggerExternal(nodeId, event) {
2012
2012
  super.triggerExternal(nodeId, event);
@@ -2043,12 +2043,41 @@ class StepEngine extends AbstractEngine {
2043
2043
  this.dirtyNodes.clear();
2044
2044
  this.graphRuntime.resume();
2045
2045
  for (const n of nodes)
2046
- this.graphRuntime.__unsafe_scheduleInputsChanged(n);
2046
+ this.graphRuntime.scheduleInputsChanged(n);
2047
2047
  await this.graphRuntime.whenIdle();
2048
2048
  this.graphRuntime.pause();
2049
2049
  }
2050
2050
  }
2051
2051
 
2052
+ /**
2053
+ * Creates an Engine instance for the given GraphRuntime based on engine configuration.
2054
+ * This is the single source of truth for engine creation, used by both local and remote runners.
2055
+ */
2056
+ function createEngine(runtime, config) {
2057
+ const engineKind = config?.engine ?? "push";
2058
+ const batched = config?.batched ?? { flushIntervalMs: 0 };
2059
+ const hybrid = config?.hybrid ?? { windowMs: 250, batchThreshold: 3 };
2060
+ switch (engineKind) {
2061
+ case "push":
2062
+ return new PushEngine(runtime);
2063
+ case "batched":
2064
+ return new BatchedEngine(runtime, {
2065
+ flushIntervalMs: batched.flushIntervalMs,
2066
+ });
2067
+ case "pull":
2068
+ return new PullEngine(runtime);
2069
+ case "hybrid":
2070
+ return new HybridEngine(runtime, {
2071
+ windowMs: hybrid.windowMs,
2072
+ batchThreshold: hybrid.batchThreshold,
2073
+ });
2074
+ case "step":
2075
+ return new StepEngine(runtime);
2076
+ default:
2077
+ throw new Error(`Unknown engine kind: ${engineKind}`);
2078
+ }
2079
+ }
2080
+
2052
2081
  const ComputeCategory = {
2053
2082
  id: "compute",
2054
2083
  displayName: "Compute",
@@ -3460,6 +3489,7 @@ exports.Registry = Registry;
3460
3489
  exports.StepEngine = StepEngine;
3461
3490
  exports.createAsyncGraphDef = createAsyncGraphDef;
3462
3491
  exports.createAsyncGraphRegistry = createAsyncGraphRegistry;
3492
+ exports.createEngine = createEngine;
3463
3493
  exports.createProgressGraphDef = createProgressGraphDef;
3464
3494
  exports.createProgressGraphRegistry = createProgressGraphRegistry;
3465
3495
  exports.createSimpleGraphDef = createSimpleGraphDef;