🛰 comlink-loader 📡
Offload modules to Worker threads seamlessly using Comlink.
comlink-loader
THIS IS A FORKED VERSION OF The reason for this fork is to patch this bug in the original one.
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:; // Export as you would in a normal module: { thisvalue = value; } { thisvalue++; } // Tip: async functions make the interface identical async { return thisvalue; }
main.js: (our demo, on the main thread)
; const inst = await 42; // notice the await await inst; await inst; // 43
License
Apache-2.0