jsrunnable

0.4.0 • Public • Published

JS Runnable

An easy way to do workers kinda??? I dunno, I just wanted to see if it would work.

Basic Usage:

var runner = new Runnable();
const myWorkerFunction = runner.add(() => console.log('hello from webworker'));
myWorkerFunction();

You can pass arguments:

var runner = new Runnable();
const myWorkerFunction = runner.add((a,b,c,d) => console.log('hello from webworker', a, b, c, d));
myWorkerFunction(1,2,3,4);

You can return results (as a promise):

var runner = new Runnable();
const myWorkerFunction = runner.add((a,b,c,d) => { return a + b + c + d; });
myWorkerFunction(1,2,3,4).then(result => console.log(result));

It can run on multiple workers:

var runner = new Runnable();
var workerCount = 4;
const myWorkerFunction = runner.add((a,b,c,d) => { return a + b + c + d; }, workerCount);
myWorkerFunction(1,2,3,4).then(result => console.log(result));
myWorkerFunction(4,4,4,4).then(result => console.log(result));

API:

new Runnable()

Create a new runnable to spin up workers, and be able to attach functions to them.

runnable.add([function], workerCount[number])

Attach a function to the runnable; It returns a wrapped function, that you can call to run the function on the web worker. This call will return a promise resolved with the result of the function call on the worker. The worker count can be anything, but will limit internally to the number of available threads.

Classes

Runnable

Runnable

Utils

Utilities for jsrunnable

Functions

worker()

worker

Runnable

Runnable

Kind: global class

new Runnable()

Constructor

runnable.add(func) ⇒ function

Add functions to workers to call.

Kind: instance method of Runnable
Returns: function - A wrapped function that calls the worker and returns results in a promise.

Param Type Description
func function Function to assign to workers.

Utils

Utilities for jsrunnable

Kind: global class

Utils.funcToString(func) ⇒ string

Stringifies a function

Kind: static method of Utils
Returns: string - Stringified function.

Param Type Description
func function Function to stringify.

Utils.buildWorker(workerFunc) ⇒ Worker

Build a worker containing the given function.

Kind: static method of Utils
Returns: Worker - worker The worker.

Param Type Description
workerFunc function The function to build a worker for.

Utils.functionToMessage(func) ⇒ Object

Turn a function into an object for sending to a worker.

Kind: static method of Utils
Returns: Object - Function message object.

Param Type
func function

Utils.randomId(prefix) ⇒ String

Returns a random id.

Kind: static method of Utils
Returns: String - A string id.

Param Type Description
prefix String A string to prefix the id with.

worker()

worker

Kind: global function

worker~postResult(message, result)

Posts the result of a called worker function back to the main thread.

Kind: inner method of worker

Param Type Description
message Object Message object for function called.
result * The result of the function call.

worker~postError(message, err)

Post an error back to the main thread.

Kind: inner method of worker

Param Type Description
message Object the message which called
err Object | String The error to post to main thread.

worker~compile(message)

Create the function from the message object

Kind: inner method of worker

Param Type Description
message Object Message object from main thread.

worker~call(message)

Call the function from the message object.

Kind: inner method of worker

Param Type Description
message Object Message object from main thread.

Readme

Keywords

none

Package Sidebar

Install

npm i jsrunnable

Weekly Downloads

0

Version

0.4.0

License

MIT

Unpacked Size

51.6 kB

Total Files

14

Last publish

Collaborators

  • vantreeseba