port_agent
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "port_agent", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A RPC-like facility for making inter-thread function calls.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -6,11 +6,44 @@ # Port Agent | ||
## Features | ||
- Port Agent will marshall return values and Errors (stack traces) back to the caller. | ||
- Registered functions (i.e., Agent.register) are persistent. | ||
- Port Agent will marshall the return value or `Error` back to the caller. | ||
- Registered functions (i.e., Agent.register) are persistent. | ||
- Late binding registrants will be called with previously awaited invocations. | ||
## Table of Contents | ||
1. [API](#api) | ||
2. [Examples](#examples) | ||
## API | ||
### The `Agent` Class | ||
#### port_agent.Agent(port) | ||
- port `<threads.MessagePort>` or `<threads.Worker>` The message port. | ||
#### agent.call\<T\>(name, ...args) | ||
- name `<string>` The name of the registered function. | ||
- ...args `<Array<unknown>>` Arguments to be passed to the registered function. | ||
- Returns: `<Promise<T>>` | ||
#### agent.register(name, fn) | ||
- name `<string>` The name of the registered function. | ||
- fn `<(...args: Array<any>) => any>` The registered function. | ||
- Returns: `<void>` | ||
#### agent.deregister(name) | ||
- name `<string>` The name of the registered function. | ||
- Returns: `<void>` | ||
## Examples | ||
### A Simple Example | ||
### An Example | ||
`./tests/test/index.ts` | ||
```ts | ||
import { Worker, isMainThread, parentPort } from 'node:worker_threads'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { strict as assert } from 'node:assert'; | ||
import { Agent } from 'port_agent'; | ||
if (isMainThread) { // This is the Main Thread. | ||
@@ -25,3 +58,5 @@ void (async () => { | ||
const greeting = await agent.call<string>('hello_world', 'again, another'); | ||
console.log(greeting); | ||
await agent.call('error', 'To err is Human.'); | ||
@@ -37,3 +72,3 @@ } | ||
const greeting = await agent.call<string>('hello_world', 'another'); | ||
const greeting = await agent.call<string>('hello_world', 'another'); // This call will be invoked once the `hello_world` function has been bound in the Worker. | ||
console.log(greeting); | ||
@@ -56,3 +91,3 @@ })(); | ||
} | ||
} | ||
} | ||
``` | ||
@@ -80,3 +115,3 @@ | ||
#### Run Test | ||
#### Run the test. | ||
You can run the test using: | ||
@@ -83,0 +118,0 @@ ```bash |
10256
115