@naoak/workerize-transferable
Helper functions for workerlize-loader to support transferables
Last updated 4 years ago by naoak .
MIT · Original npm · Tarball · package.json
$ npm install @naoak/workerize-transferable 
SYNC missed versions from official npm registry.

workerize-transferable

Build Status

Helper functions for workerlize-loader to support transferables.

Install

npm i -D workerize-loader
npm i -S @naoak/workerize-transferable

Usage

worker.js:

import { setupTransferableMethodsOnWorker } from "@naoak/workerize-transferable";

export function increment(buffer) {
  buffer[0] += 1;
  return buffer;
}

setupTransferableMethodsOnWorker({
  // the name of function which use some transferables
  increment: {
    // specify an instance of the function
    fn: increment,
    // pick a transferable object from the result which is an instance of Int32Array
    pickTransferablesFromResult: (result) => [result.buffer],
  },
});

index.js:

import worker from "workerize-loader!./worker";
import { setupTransferableMethodsOnMain } from "@naoak/workerize-transferable";

let instance = worker(); // `new` is optional

setupTransferableMethodsOnMain({
  // worker instance
  instance,
  // the name of method which use some transferables
  increment: {
    // pick a transferable object from the method parameters
    pickTransferablesFromParams: (params) => [params[0].buffer],
  },
});

(async () => {
  const buffer = new Int32Array([0]);
  const res = await instance.increment(await instance.increment(buffer));
  console.log(buffer.length); // 0
  console.log(res.length); // 1
  console.log(res[0]); // 2
})();

See workerize-loader for details how to setup workerize-loader.

Current Tags

  • 0.1.0                                ...           latest (3 years ago)

6 Versions

  • 0.1.0                                ...           3 years ago
  • 0.0.6                                ...           4 years ago
  • 0.0.5                                ...           4 years ago
  • 0.0.4                                ...           4 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (5)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE