Skip to content

Commit

Permalink
chore(release): v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
MichielvdVelde committed Sep 11, 2023
1 parent 2c77e15 commit 196f306
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ main ]

jobs:
build:
nodejs:

runs-on: ubuntu-latest

Expand Down
18 changes: 9 additions & 9 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
26 changes: 21 additions & 5 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ export class Node<T> {
/** The metadata of the node. */
readonly metadata: Record<string, unknown> = {};

/** The middleware stack. */
readonly stack: Processor<T>[] = [];

/** The activation function. */
shouldActivate: ShouldActivate<T>;

/** The middleware stack. */
readonly #stack: Processor<T>[] = [];
/** The outputs of the node. */
readonly #outputs = new Map<Node<T>, ShouldActivate<T>>();
/** The sinks of the node. */
Expand All @@ -59,6 +58,13 @@ export class Node<T> {
this.shouldActivate = shouldActivate ?? true;
}

/**
* The middleware stack.
*/
get stack(): ReadonlyArray<Processor<T>> {
return this.#stack;
}

/**
* The outputs of the node.
*/
Expand All @@ -73,6 +79,16 @@ export class Node<T> {
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<T>[]): Node<T> {
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
Expand Down Expand Up @@ -129,7 +145,7 @@ export class Node<T> {
* Clears the middleware stack.
*/
clearStack(): Node<T> {
this.stack.length = 0;
this.#stack.length = 0;
return this;
}

Expand Down Expand Up @@ -157,7 +173,7 @@ export class Node<T> {
}

// 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
Expand Down
2 changes: 1 addition & 1 deletion tests/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 196f306

Please sign in to comment.