{"size": 1741, "ext": "js", "max_stars_repo_path": "examples/abstract.js", "max_stars_repo_name": "fibo/dflow", "max_stars_repo_stars_event_min_datetime": "2015-04-26T01:54:17.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-23T11:27:35.000Z", "max_issues_repo_path": "examples/abstract.js", "max_issues_repo_name": "fibo/dflow", "max_issues_repo_issues_event_min_datetime": "2015-07-24T20:06:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-01T12:24:39.000Z", "max_forks_repo_path": "examples/abstract.js", "max_forks_repo_name": "fibo/dflow", "max_forks_repo_forks_event_min_datetime": "2015-10-04T17:59:20.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-30T17:14:38.000Z"}
{"max_stars_count": 76, "max_issues_count": 88, "max_forks_count": 12, "avg_line_length": 18.1354166667, "max_line_length": 55, "alphanum_fraction": 0.5588742102}
import { DflowHost, DflowNode } from "../dflow.js";

class NumNode extends DflowNode {
  static kind = "Num";

  run() {}
}

class SumNode extends DflowNode {
  static kind = "Sum";

  run() {
    let sum = 0;

    for (const input of this.inputs) {
      const inputData = input.data;
      if (typeof inputData === "number") {
        sum += inputData;
      }
    }

    const output = this.output(0);
    if (output !== null) {
      output.data = sum;
    }
  }
}

function sleep(seconds = 1) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, seconds * 1000);
  });
}

class SleepNode extends DflowNode {
  kind = "Sleep";
  static meta = {
    isAsync: true,
  };

  constructor(serializedNode) {
    super(serializedNode, SleepNode.meta);
  }

  async run() {
    console.log("sleep node start");
    await sleep();
    console.log("sleep node end");
  }
}

const nodesCatalog = {
  [NumNode.kind]: NumNode,
  [SumNode.kind]: SumNode,
  [SleepNode.kind]: SleepNode,
};

async function runGraph() {
  const dflow = new DflowHost(nodesCatalog);

  dflow.newNode({
    id: "num",
    name: "Hello world",
    kind: NumNode.kind,
    outputs: [{ id: "out", data: 2 }],
  });
  const sumNode = dflow.newNode({
    id: "sum",
    kind: SumNode.kind,
    inputs: [{ id: "in1" }, { id: "in2" }],
    outputs: [{ id: "out" }],
  });
  dflow.newEdge({
    id: "e1",
    source: ["num", "out"],
    target: ["sum", "in1"],
  });
  dflow.newEdge({
    id: "e2",
    source: ["num", "out"],
    target: ["sum", "in2"],
  });

  // Add also an async node.
  dflow.newNode({ id: "sleep", kind: SleepNode.kind });

  await dflow.run();

  const sum = sumNode.output(0);
  console.log(sum.data); // 4
}

runGraph();
