An easy way to do workers kinda??? I dunno, I just wanted to see if it would work.
var runner = new Runnable();
const myWorkerFunction = runner.add(() => console.log('hello from webworker'));
myWorkerFunction();
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);
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));
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));
Create a new runnable to spin up workers, and be able to attach functions to them.
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.
- worker()
-
worker
Runnable
Kind: global class
-
Runnable
- new Runnable()
-
.add(func) ⇒
function
Constructor
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. |
Utilities for jsrunnable
Kind: global class
-
Utils
-
.funcToString(func) ⇒
string
-
.buildWorker(workerFunc) ⇒
Worker
-
.functionToMessage(func) ⇒
Object
-
.randomId(prefix) ⇒
String
-
.funcToString(func) ⇒
Stringifies a function
Kind: static method of Utils
Returns: string
- Stringified function.
Param | Type | Description |
---|---|---|
func | function |
Function to stringify. |
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. |
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 |
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
Kind: global function
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. |
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. |
Create the function from the message object
Kind: inner method of worker
Param | Type | Description |
---|---|---|
message | Object |
Message object from main thread. |
Call the function from the message object.
Kind: inner method of worker
Param | Type | Description |
---|---|---|
message | Object |
Message object from main thread. |