This package has been deprecated

Author message:

comlink v4 supports node officially

node-comlink
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

node-comlink Build Status

An adapter for node child process or worker threads to work with Comlink

Install

npm i --save node-comlink

Usage

// main.js
const { fork } = require('child_process');
const { NodeMessageAdapter, patchMessageChannel } = require('node-comlink');
patchMessageChannel(); // need patch it before load comlink
const Comlink = require('comlinkjs/umd/comlink.js');

const worker = fork(`${__dirname}/worker.js`);

async function f() {
    const MyClass = Comlink.proxy(new NodeMessageAdapter(worker));
    // `instance` is an instance of `MyClass` that lives in the worker!
    const instance = await new MyClass();
    await instance.logSomething(); // logs “myValue = 42”

    worker.kill();
};

f();
// worker.js
const { NodeMessageAdapter, patchMessageChannel } = require('node-comlink');
patchMessageChannel(); // need patch it before load comlink
const Comlink = require('comlinkjs/umd/comlink.js');

const myValue = 42;
class MyClass {
    logSomething() {
        console.log(`myValue = ${myValue}`);
    }
}

Comlink.expose(MyClass, new NodeMessageAdapter());

Child Process and Worker Threads

Worker Threads is a new feature added in node 10.5.0, it comes with native MessageChannel/MessagePort/Transferable support. But it is still an experimental feature and needs --experimental-worker flag to enable, e.g. node --experimental-worker main.js. Therefore Child Process is the default option currently.

// To go with Worker Threads:
const { NodeMessageAdapter, patchMessageChannel } = require('node-comlink/lib/worker_threads');

Examples

Readme

Keywords

Package Sidebar

Install

npm i node-comlink

Weekly Downloads

1

Version

0.3.0

License

MIT

Unpacked Size

14.1 kB

Total Files

11

Last publish

Collaborators

  • rocwind