Skip to content

Commit 1c0dd43

Browse files
committed
more docs
1 parent 14a2027 commit 1c0dd43

File tree

10 files changed

+299
-50
lines changed

10 files changed

+299
-50
lines changed

dflow.ts

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
/**
77
* Includes JSON data types and `undefined`.
8+
*
9+
* @see {@link https://fibo.github.io/dflow/#dflowdata}
810
*/
911
export type DflowData =
1012
| undefined
@@ -19,6 +21,11 @@ export type DflowObject = { [Key in string]: DflowData };
1921

2022
export type DflowArray = DflowData[];
2123

24+
/**
25+
* Dflow data types represent values that can be serialized as JSON.
26+
*
27+
* @see {@link https://fibo.github.io/dflow/#dflowdatatype}
28+
*/
2229
export type DflowDataType =
2330
| "null"
2431
| "boolean"
@@ -30,7 +37,11 @@ export type DflowDataType =
3037
// Inputs, outputs, links and nodes.
3138
// ////////////////////////////////////////////////////////////////////
3239

33-
/** Connects two nodes in the graph. */
40+
/**
41+
* Connects two nodes in the graph.
42+
*
43+
* @see {@link https://fibo.github.io/dflow/#dflowlink}
44+
*/
3445
export type DflowLink = [
3546
sourceNodeId: string,
3647
sourcePosition: number,
@@ -46,6 +57,8 @@ export type DflowLink = [
4657
* ```json
4758
* { "name": "label", "types": ["string"] }
4859
* ```
60+
*
61+
* @see {@link https://fibo.github.io/dflow/#dflowinput}
4962
*/
5063
export type DflowInput = {
5164
/** Ignored by Dflow, but could be used by UI. */
@@ -70,6 +83,8 @@ export type DflowInput = {
7083
* ```json
7184
* { "name": "sum", "types": ["number"] }
7285
* ```
86+
*
87+
* @see {@link https://fibo.github.io/dflow/#dflowoutput}
7388
*/
7489
export type DflowOutput = {
7590
/** Ignored by Dflow, but could be used by UI. */
@@ -78,7 +93,11 @@ export type DflowOutput = {
7893
types: DflowDataType[];
7994
};
8095

81-
/** Defines a block of code: it can have inputs and outputs. */
96+
/**
97+
* Defines a block of code: it can have inputs and outputs.
98+
*
99+
* @see {@link https://fibo.github.io/dflow/#dflownode}
100+
*/
82101
export type DflowNode = {
83102
kind: string;
84103
inputs?: DflowInput[];
@@ -102,6 +121,8 @@ export type DflowGraph = {
102121
* A `Dflow` represents a program as an executable graph.
103122
* A graph can contain nodes and links.
104123
* Nodes are executed, sorted by their connections.
124+
*
125+
* @see {@link https://fibo.github.io/dflow/#api}
105126
*/
106127
export class Dflow {
107128
/** Node definitions indexed by node kind. */
@@ -155,6 +176,11 @@ export class Dflow {
155176
*/
156177
ERR?: (...data: any[]) => void;
157178

179+
/**
180+
* Dflow constructor requires a list of node definitions which is an `Array<DflowNode>`.
181+
*
182+
* @see {@link https://fibo.github.io/dflow/#constructor}
183+
*/
158184
constructor(nodeDefinitions: Array<DflowNode>) {
159185
// Add given node definitions, followed by builtin nodes.
160186
for (const nodeDefinition of nodeDefinitions)
@@ -227,18 +253,20 @@ export class Dflow {
227253
// Output types are stored in output items.
228254
const sourceOutput = this.#outputs.get(sourceNodeId)?.[sourcePosition];
229255
if (!sourceOutput) return false;
230-
const sourceTypes = sourceOutput.types;
231-
232-
// If source can have any type or
233-
// target can have any type,
256+
// If source can have any type or target can have any type,
234257
// then source and target are compatible.
235-
if (!sourceTypes.length || !targetTypes.length) return true;
236-
258+
if (!sourceOutput.types.length || !targetTypes.length) return true;
237259
// Check if target accepts some of the `dataType` source can have.
238-
return targetTypes.some((dataType) => sourceTypes.includes(dataType));
260+
return targetTypes.some((dataType) =>
261+
sourceOutput.types.includes(dataType)
262+
);
239263
}
240264

241-
/** Create a new node. Returns node id. */
265+
/**
266+
* Create a new node. Returns node id.
267+
*
268+
* @see {@link https://fibo.github.io/dflow/#dflow.node}
269+
*/
242270
node(kind: string, wantedId?: string): string {
243271
const nodeDef = this.#nodeDefinitions.get(kind);
244272
if (!nodeDef) throw new Error("Cannot create node", { cause: { kind } });
@@ -286,7 +314,11 @@ export class Dflow {
286314
return id;
287315
}
288316

289-
/** Delete node or link with given id. */
317+
/**
318+
* Delete node or link with given id.
319+
*
320+
* @see {@link https://fibo.github.io/dflow/#dflow.delete}
321+
*/
290322
delete(id: string) {
291323
// Delete node.
292324
if (this.#kinds.delete(id)) {
@@ -306,7 +338,11 @@ export class Dflow {
306338
if (targetInput) targetInput.source = undefined;
307339
}
308340

309-
/** Create a new data node. Returns node id. */
341+
/**
342+
* Create a new data node. Returns node id.
343+
*
344+
* @see {@link https://fibo.github.io/dflow/#dflow.data}
345+
*/
310346
data(value: unknown, wantedId?: string): string {
311347
const id = this.#newId(this.#kinds, "n", wantedId);
312348
this.#kinds.set(id, "data");
@@ -324,7 +360,11 @@ export class Dflow {
324360
return id;
325361
}
326362

327-
/** Create a new link and connect two nodes. Returns link id. */
363+
/**
364+
* Create a new link and connect two nodes. Returns link id.
365+
*
366+
* @see {@link https://fibo.github.io/dflow/#dflow.link}
367+
*/
328368
link(
329369
source: string | [nodeId: string, position: number],
330370
target: string | [nodeId: string, position: number],
@@ -368,7 +408,11 @@ export class Dflow {
368408
throw error;
369409
}
370410

371-
/** Execute all nodes, sorted by their connections. */
411+
/**
412+
* Execute all nodes, sorted by their connections.
413+
*
414+
* @see {@link https://fibo.github.io/dflow/#dflow.run}
415+
*/
372416
async run(): Promise<void> {
373417
// Reset errors.
374418
this.#errors.clear();
@@ -431,7 +475,11 @@ export class Dflow {
431475
}
432476
}
433477

434-
/** A graph contains nodes and links. */
478+
/**
479+
* A graph contains nodes and links.
480+
*
481+
* @see {@link https://fibo.github.io/dflow/#dflow.graph}
482+
*/
435483
get graph(): DflowGraph {
436484
const node: DflowGraph["node"] = {};
437485
const data: DflowGraph["data"] = {};
@@ -447,12 +495,20 @@ export class Dflow {
447495
};
448496
}
449497

450-
/** Get error messages from last run, indexed by node id. */
498+
/**
499+
* Get error messages from last run, indexed by node id.
500+
*
501+
* @see {@link https://fibo.github.io/dflow/#dflow.error}
502+
*/
451503
get error(): Record<string, string> {
452504
return Object.fromEntries(this.#errors.entries());
453505
}
454506

455-
/** Get output data of last run, indexed by node id. */
507+
/**
508+
* Get output data of last run, indexed by node id.
509+
*
510+
* @see {@link https://fibo.github.io/dflow/#dflow.out}
511+
*/
456512
get out(): Record<string, DflowArray> {
457513
const out: Record<string, DflowArray> = {};
458514
for (const nodeId of this.#kinds.keys()) {

docs/examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ Then you can run examples via an npm script:
99

1010
npm run example:hello_world
1111
npm run example:usage
12-
npm run example:context
1312
npm run example:async_nodes
13+
npm run example:dynamic_math_nodes
14+
npm run example:context
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Dflow } from "dflow";
2+
3+
// Generate Dflow nodes for all Math properties and functions.
4+
5+
const mathNodes = Object.getOwnPropertyNames(Math).map((key) => {
6+
// @ts-expect-error: expression of type 'string' can't be used to index type 'Math'
7+
const item = Math[key];
8+
9+
const kind = `Math.${key}`;
10+
const outputs = [Dflow.output("number")];
11+
12+
// If the item is a number, create a constant node.
13+
if (typeof item === "number") {
14+
return {
15+
kind,
16+
outputs,
17+
run: () => item
18+
};
19+
}
20+
21+
// If the item is a function, create a function node.
22+
if (typeof item === "function") {
23+
return {
24+
kind,
25+
// Get the number of inputs from the function's length property.
26+
inputs: Array(item.length).fill(Dflow.input("number")),
27+
outputs,
28+
run: (...args: number[]) => {
29+
return item(...args);
30+
}
31+
};
32+
}
33+
// Not needed, just to make TS happy.
34+
throw new Error(`Unsupported Math property: ${key}`);
35+
});
36+
37+
// Create a Dflow instance with the generated node definitions.
38+
const dflow = new Dflow(mathNodes);
39+
40+
// Compute Math.trunc(Math.E)
41+
const nodeId1 = dflow.node("Math.E");
42+
const nodeId2 = dflow.node("Math.trunc");
43+
dflow.link(nodeId1, nodeId2);
44+
45+
dflow.run();
46+
47+
console.log(dflow.out);
48+
// { n0: [ 2.718281828459045 ], n1: [ 2 ] }

docs/examples/inject_doc_snippets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const inputContent = await readFile(filepath, { encoding: "utf8" });
1515

1616
for await (const line of inputContent.split("\n")) {
1717
const snippetStartMatch = line.match(/START file:([\w_\.\/]+)/);
18-
const snippetEndMatch = line.match(/END file:/);
18+
const snippetEndMatch = line.match(/END snippet/);
1919
const isInSnippet = snippetRelativePath !== "";
2020

2121
if (snippetStartMatch) {

docs/examples/snippets/mathPI.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Dflow, type DflowNode } from "dflow";
2+
3+
const MathPI: DflowNode = {
4+
kind: "mathPI",
5+
outputs: [Dflow.output("number", { name: "π" })],
6+
run: () => Math.PI
7+
};

docs/examples/snippets/print.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Dflow, type DflowNode } from "dflow";
2+
3+
const Print: DflowNode = {
4+
kind: "print",
5+
inputs: [Dflow.input("string")],
6+
run: (message: string) => {
7+
console.log(message);
8+
}
9+
};

docs/examples/snippets/sum.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Dflow, type DflowNode } from "dflow";
2+
3+
const Sum: DflowNode = {
4+
kind: "sum",
5+
inputs: [Dflow.input("number"), Dflow.input("number")],
6+
outputs: [Dflow.output("number")],
7+
run(a: number, b: number) {
8+
return a + b;
9+
}
10+
};

0 commit comments

Comments
 (0)