Skip to content

corbin-c/workerify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Workerify

Workerifiy is an ES module to wrap any function or object into a Web Worker. It avoids the pain of creating a separate worker file and handling messages. One pass a function or an object to Workerify, which returns an asynchronous function. This function may be called just like the original one but will be processed in the dedicated Web Worker; it will return promises.

Context

It is possible to pass an additional context argument to Workerify. This context is an array of objects or functions, which may be required for the main target functions.

Instances

An extra instances parameter may be passed to Workerify. It indicates the number of instances of the worker to be created. Multiple instances are handled seamlessly for the user.

Example

First, import Workerify from here with:

import { Workerify } from "https://corbin-c.github.io/workerify/workerify.js";

Then, we'll create here an async context. We declare here an example object, with various methods. This object is passed to Workerify. The original example object may then be called without further trouble:

(async () => {
  let example = {
    square: (x) => x*x,
    cube:(x) => x*x*x,
    sum:(...args) => args.reduce((accumulator,current) => accumulator+current),
    multiply:(x,y) => x*y,
  }
  example = Workerify(example);
  console.log(await example.square(2));
  console.log(await example.cube(2));
  console.log(await example.sum(1,2,3,4,5));
  console.log(await example.multiply(2,5));
})();

About

get web workers from existing functions

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published