@bian-womp/spark-graph 0.2.46 → 0.2.47

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
@@ -639,6 +639,30 @@ class GraphRuntime {
639
639
  },
640
640
  };
641
641
  }
642
+ createExecutionContext(nodeId, node, inputs, runId, abortSignal, options) {
643
+ const emitHandler = options?.emitHandler ??
644
+ ((handle, value) => this.propagate(nodeId, handle, value));
645
+ const reportProgress = options?.reportProgress ??
646
+ ((p) => {
647
+ node.stats.progress = Math.max(0, Math.min(1, Number(p) || 0));
648
+ });
649
+ return {
650
+ state: node.state,
651
+ setState: (next) => Object.assign(node.state, next),
652
+ emit: emitHandler,
653
+ invalidateDownstream: () => this.invalidateDownstream(nodeId),
654
+ scheduleInputsChanged: () => {
655
+ if (this.allInboundHaveValue(nodeId)) {
656
+ this.scheduleInputsChanged(nodeId);
657
+ }
658
+ },
659
+ getInput: (handle) => inputs[handle],
660
+ environment: this.environment,
661
+ runId,
662
+ abortSignal,
663
+ reportProgress,
664
+ };
665
+ }
642
666
  scheduleInputsChanged(nodeId) {
643
667
  const node = this.nodes.get(nodeId);
644
668
  if (!node)
@@ -681,24 +705,13 @@ class GraphRuntime {
681
705
  if (policy.timeoutMs && policy.timeoutMs > 0) {
682
706
  timeoutId = setTimeout(() => controller.abort("timeout"), policy.timeoutMs);
683
707
  }
684
- const ctx = {
685
- state: node.state,
686
- environment: this.environment,
687
- runId,
688
- abortSignal: controller.signal,
689
- setState: (next) => Object.assign(node.state, next),
690
- emit: (handle, value) => {
708
+ const ctx = this.createExecutionContext(nodeId, node, capturedInputs, runId, controller.signal, {
709
+ emitHandler: (handle, value) => {
691
710
  const m = policy.asyncConcurrency ?? "switch";
692
711
  if (m !== "merge" && runId !== node.latestRunId)
693
712
  return;
694
713
  this.propagate(nodeId, handle, value);
695
714
  },
696
- invalidateDownstream: () => this.invalidateDownstream(nodeId),
697
- scheduleInputsChanged: () => {
698
- if (this.allInboundHaveValue(nodeId)) {
699
- this.scheduleInputsChanged(nodeId);
700
- }
701
- },
702
715
  reportProgress: (p) => {
703
716
  node.stats.progress = Math.max(0, Math.min(1, Number(p) || 0));
704
717
  this.emit("stats", {
@@ -708,10 +721,11 @@ class GraphRuntime {
708
721
  progress: node.stats.progress,
709
722
  });
710
723
  },
711
- };
724
+ });
712
725
  const exec = async (attempt) => {
713
726
  let hadError = false;
714
727
  try {
728
+ node.lifecycle?.prepare?.(node.params ?? {}, ctx);
715
729
  await node.runtime.onInputsChanged?.(capturedInputs, ctx);
716
730
  }
717
731
  catch (err) {
@@ -1153,10 +1167,10 @@ class GraphRuntime {
1153
1167
  launch(invalidate = false) {
1154
1168
  // call onActivated for nodes that implement it
1155
1169
  for (const node of this.nodes.values()) {
1156
- node.lifecycle?.init?.(node.params ?? {}, {
1157
- state: node.state,
1158
- setState: (next) => Object.assign(node.state, next),
1159
- });
1170
+ const effectiveInputs = this.getEffectiveInputs(node.nodeId);
1171
+ const ctrl = new AbortController();
1172
+ const ctx = this.createExecutionContext(node.nodeId, node, effectiveInputs, `${node.nodeId}:init`, ctrl.signal);
1173
+ node.lifecycle?.prepare?.(node.params ?? {}, ctx);
1160
1174
  node.runtime.onActivated?.();
1161
1175
  }
1162
1176
  if (invalidate) {
@@ -1399,10 +1413,10 @@ class GraphRuntime {
1399
1413
  };
1400
1414
  this.nodes.set(n.nodeId, rn);
1401
1415
  // Activate new node
1402
- rn.lifecycle?.init?.(rn.params ?? {}, {
1403
- state: rn.state,
1404
- setState: (next) => Object.assign(rn.state, next),
1405
- });
1416
+ const effectiveInputs = this.getEffectiveInputs(rn.nodeId);
1417
+ const ctrl = new AbortController();
1418
+ const ctx = this.createExecutionContext(rn.nodeId, rn, effectiveInputs, `${rn.nodeId}:init`, ctrl.signal);
1419
+ rn.lifecycle?.prepare?.(rn.params ?? {}, ctx);
1406
1420
  rn.runtime.onActivated?.();
1407
1421
  }
1408
1422
  else {