From 196f306445fb1900cd369cf94533820be7ad0270 Mon Sep 17 00:00:00 2001 From: Michiel van der Velde Date: Mon, 11 Sep 2023 15:54:53 +0200 Subject: [PATCH] chore(release): v0.0.3 --- .github/workflows/ci.yaml | 2 +- API.md | 18 +++++++++--------- package-lock.json | 4 ++-- package.json | 4 ++-- src/Node.ts | 26 +++++++++++++++++++++----- tests/node.ts | 2 +- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 87bca67..2494bf4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -7,7 +7,7 @@ on: branches: [ main ] jobs: - build: + nodejs: runs-on: ubuntu-latest diff --git a/API.md b/API.md index 80b6f44..5ddf7e2 100644 --- a/API.md +++ b/API.md @@ -43,29 +43,29 @@ nodeA.link(nodeB); nodeA.link(nodeB, (node, ctx) => ctx.n > 0); ``` -## Touch a Node +## Sink Output ```ts import Node from "@wecandobetter/node"; const node = new Node<{ n: number }>({ id: "A" }); -// The context is passed to the activation function and middleware -const context = { n: 5 }; - -// Touch the node with the context -await node.touch(context); +// Sinks are executed when the node is activated and has completed processing +node.sink((ctx) => console.log(`Sink output: ${ctx.n}`)); ``` -## Sink Output +## Touch a Node ```ts import Node from "@wecandobetter/node"; const node = new Node<{ n: number }>({ id: "A" }); -// Sinks are executed when the node is activated and has completed processing -node.sink((ctx) => console.log(`Sink output: ${ctx.n}`)); +// The context is passed to the activation function and middleware +const context = { n: 5 }; + +// Touch the node with the context +await node.touch(context); ``` ## Explore Nodes diff --git a/package-lock.json b/package-lock.json index 3666796..9d16ded 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@wecandobetter/node", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@wecandobetter/node", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "devDependencies": { "@types/jest": "^29.5.4", diff --git a/package.json b/package.json index 6766d12..f2836f1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@wecandobetter/node", "description": "Node is a graph execution engine.", - "version": "0.0.2", + "version": "0.0.3", "type": "module", "license": "MIT", "main": "dist/index.js", @@ -10,7 +10,7 @@ "build:ts": "tsc -p tsconfig.build.json", "test": "npm run build && jest", "test:coverage": "npm run test -- --coverage", - "publish:npm": "npm run build:ts && npm publish" + "publish:npm": "npm run test && npm publish" }, "repository": { "type": "git", diff --git a/src/Node.ts b/src/Node.ts index bd77bf3..72fe562 100644 --- a/src/Node.ts +++ b/src/Node.ts @@ -43,12 +43,11 @@ export class Node { /** The metadata of the node. */ readonly metadata: Record = {}; - /** The middleware stack. */ - readonly stack: Processor[] = []; - /** The activation function. */ shouldActivate: ShouldActivate; + /** The middleware stack. */ + readonly #stack: Processor[] = []; /** The outputs of the node. */ readonly #outputs = new Map, ShouldActivate>(); /** The sinks of the node. */ @@ -59,6 +58,13 @@ export class Node { this.shouldActivate = shouldActivate ?? true; } + /** + * The middleware stack. + */ + get stack(): ReadonlyArray> { + return this.#stack; + } + /** * The outputs of the node. */ @@ -73,6 +79,16 @@ export class Node { return this.#sinks; } + /** + * Adds one or more processors to the middleware stack. The processors will be + * executed in the order they are added. + * @param processors The processors to add. + */ + use(...processors: Processor[]): Node { + this.#stack.push(...processors); + return this; + } + /** * Links the given node to this node. When the node is activated, the given node * will be touched with the same context. If the given node is already linked @@ -129,7 +145,7 @@ export class Node { * Clears the middleware stack. */ clearStack(): Node { - this.stack.length = 0; + this.#stack.length = 0; return this; } @@ -157,7 +173,7 @@ export class Node { } // NOTE: Errors are handled by the pipeline. - await pipe(...this.stack)(ctx); + await pipe(...this.#stack)(ctx); if (this.#outputs.size) { // Touch all outputs in parallel. We don't need to wait for them to diff --git a/tests/node.ts b/tests/node.ts index 0a25b91..926e0d5 100644 --- a/tests/node.ts +++ b/tests/node.ts @@ -17,7 +17,7 @@ describe("Node API", () => { const node = new Node<{ n: number }>({ id: "A" }); const context = { n: 5 }; - node.stack.push(async (ctx, next) => { + node.use(async (ctx, next) => { ctx.n += 10; await next(); });