jsrunnable v0.4.0
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
- Runnable
- new Runnable()
- .add(func) ⇒ function
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
- .buildWorker(workerFunc) ⇒ Worker
- .functionToMessage(func) ⇒ Object
- .randomId(prefix) ⇒ String
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. |