Skip to content
Webpack loader to offload modules to Worker threads seamlessly using Comlink.
Branch: master
Clone or download
Latest commit 6aa4568 Sep 4, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Add ?multi option to disable singleton behavior Sep 4, 2018
test Initial release! Jun 12, 2018
.editorconfig Initial release! Jun 12, 2018
.gitignore Initial release! Jun 12, 2018
.travis.yml Initial release! Jun 12, 2018
CONTRIBUTING.md Initial release! Jun 12, 2018
LICENSE Initial release! Jun 12, 2018
README.md Update README.md Jun 13, 2018
package.json 1.1.0 Sep 4, 2018

README.md

comlink-loader

🛰 comlink-loader 📡

Offload modules to Worker threads seamlessly using Comlink.

Features

  • Offload almost any module into a Worker with little or no usage change
  • Supports arbitrary classes, objects & functions (await new Foo())
  • Works beautifully with async/await
  • Built-in code-splitting: workers are lazy-loaded

Installation

npm install -D comlink-loader

Usage

The goal of comlink-loader is to make the fact that a module is running inside a Worker nearly transparent to the developer.

In the example below, the sole difference between running MyClass on a Worker thread instead of the main thread is that instantiation and method calls must be prefixed with await. This is required because Worker interactions are inherently asynchronous.

my-class.js: (gets moved into a worker)

// Dependencies get bundled into the worker:
import rnd from 'random-int';

// Export as you would in a normal module:
export class MyClass {
  constructor(value = rnd()) {
    this.value = value;
  }
  increment() {
    this.value++;
  }
  // Tip: async functions make the interface identical
  async getValue() {
    return this.value;
  }
}

main.js: (our demo, on the main thread)

import { MyClass } from 'comlink-loader!./my-class';

const inst = await new MyClass(42); // notice the await

await inst.increment();

await inst.getValue();  // 43

License

Apache-2.0

You can’t perform that action at this time.