Package Exports
- @naoak/workerize-transferable
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@naoak/workerize-transferable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
workerize-transferable
Helper functions for workerlize-loader to support transferables.
Install
npm i -D workerize-loader
npm i -S @naoak/workerize-transferableUsage
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.