port_agent
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -55,3 +55,8 @@ "use strict"; | ||
if (fn) { | ||
await this.tryPost(fn, message); | ||
try { | ||
await this.tryPost(fn, message); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
@@ -58,0 +63,0 @@ else { |
{ | ||
"name": "port_agent", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "A RPC-like facility for making inter-thread function calls.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -8,7 +8,5 @@ # Port Agent | ||
## Example | ||
This example should log the string "Hello another world!" to the console. | ||
`index.js` | ||
```js | ||
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads'; | ||
import { Worker, isMainThread, parentPort } from 'node:worker_threads'; | ||
import { fileURLToPath } from 'node:url'; | ||
@@ -18,9 +16,21 @@ import { Agent } from 'port_agent'; | ||
if (isMainThread) { | ||
const worker = new Worker(fileURLToPath(import.meta.url)); | ||
const agent = new Agent(worker); | ||
worker.on('online', async ()=>{ | ||
(async () => { | ||
const worker = new Worker(fileURLToPath(import.meta.url)); | ||
const agent = new Agent(worker); | ||
worker.on('online', async () => { | ||
try { | ||
let greeting = await agent.call('hello_world', 'again, another'); | ||
console.log(greeting); | ||
await agent.call('error', 'again, another'); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
finally { | ||
worker.terminate(); | ||
} | ||
}); | ||
let greeting = await agent.call('hello_world', 'another'); | ||
console.log(greeting); | ||
worker.terminate(); | ||
}); | ||
})(); | ||
} else { | ||
@@ -30,4 +40,21 @@ (async () => { | ||
await agent.register('hello_world', (value) => `Hello ${value} world!`); | ||
await agent.register('error', (value) => { | ||
throw new Error('To err is Human.'); | ||
}); | ||
})(); | ||
} | ||
``` | ||
``` | ||
This example should log to the console: | ||
```bash | ||
Hello another world! | ||
Hello again, another world! | ||
Error: To err is Human. | ||
at file:///index.js:30:19 | ||
at Agent.tryPost (/index.js:82:33) | ||
at MessagePort.<anonymous> (/index.js:56:36) | ||
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20) | ||
at exports.emitMessage (node:internal/per_context/messageport:23:28) | ||
``` | ||
@@ -85,3 +85,8 @@ /* eslint-disable @typescript-eslint/no-unsafe-argument */ | ||
if (fn) { | ||
await this.tryPost(fn, message); | ||
try { | ||
await this.tryPost(fn, message); | ||
} | ||
catch(err) { | ||
console.error(err); | ||
} | ||
} | ||
@@ -88,0 +93,0 @@ else { |
@@ -1,2 +0,2 @@ | ||
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads'; | ||
import { Worker, isMainThread, parentPort } from 'node:worker_threads'; | ||
import { fileURLToPath } from 'node:url'; | ||
@@ -6,9 +6,21 @@ import { Agent } from 'port_agent'; | ||
if (isMainThread) { | ||
const worker = new Worker(fileURLToPath(import.meta.url)); | ||
const agent = new Agent(worker); | ||
worker.on('online', async ()=>{ | ||
(async () => { | ||
const worker = new Worker(fileURLToPath(import.meta.url)); | ||
const agent = new Agent(worker); | ||
worker.on('online', async () => { | ||
try { | ||
let greeting = await agent.call('hello_world', 'again, another'); | ||
console.log(greeting); | ||
await agent.call('error', 'again, another'); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
finally { | ||
worker.terminate(); | ||
} | ||
}); | ||
let greeting = await agent.call('hello_world', 'another'); | ||
console.log(greeting); | ||
worker.terminate(); | ||
}); | ||
})(); | ||
} else { | ||
@@ -18,3 +30,6 @@ (async () => { | ||
await agent.register('hello_world', (value) => `Hello ${value} world!`); | ||
await agent.register('error', (value) => { | ||
throw new Error('To err is Human.'); | ||
}); | ||
})(); | ||
} |
16603
434
58