0.4.0 • Published 5 years ago

jsrunnable v0.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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, workerCountnumber)

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

Functions

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.

ParamTypeDescription
funcfunctionFunction 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.

ParamTypeDescription
funcfunctionFunction to stringify.

Utils.buildWorker(workerFunc) ⇒ Worker

Build a worker containing the given function.

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

ParamTypeDescription
workerFuncfunctionThe 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.

ParamType
funcfunction

Utils.randomId(prefix) ⇒ String

Returns a random id.

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

ParamTypeDescription
prefixStringA 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

ParamTypeDescription
messageObjectMessage 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

ParamTypeDescription
messageObjectthe message which called
errObject | StringThe error to post to main thread.

worker~compile(message)

Create the function from the message object

Kind: inner method of worker

ParamTypeDescription
messageObjectMessage object from main thread.

worker~call(message)

Call the function from the message object.

Kind: inner method of worker

ParamTypeDescription
messageObjectMessage object from main thread.
0.4.0

5 years ago

0.3.0

5 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago