new TaskProcessor
A wrapper around a web worker that allows scheduling tasks for a given worker, returning results asynchronously via a promise. The Worker is not constructed until a task is scheduled.
Parameters:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
workerName |
String | The name of the worker. This is expected to be a script in the Workers folder. | ||
maximumActiveTasks |
Number |
<optional> |
5 | The maximum number of active tasks. Once exceeded, scheduleTask will not queue any more tasks, allowing work to be rescheduled in future frames. |
Source:
Methods
-
<static> prototype.scheduleTask
-
Schedule a task to be processed by the web worker asynchronously. If there are currently more tasks active than the maximum set by the constructor, will immediately return undefined. Otherwise, returns a promise that will resolve to the result posted back by the worker when finished.
Parameters:
Name Type Argument Description parameters
* Any input data that will be posted to the worker. transferableObjects
Array <optional>
An array of objects contained in parameters that should be transferred to the worker instead of copied. Returns:
Promise Either a promise that will resolve to the result when available, or undefined if there are too many active tasks,Example
var taskProcessor = new TaskProcessor('myWorkerName'); var promise = taskProcessor.scheduleTask({ someParameter : true, another : 'hello' }); if (typeof promise === 'undefined') { // too many active tasks - try again later } else { when(promise, function(result) { // use the result of the task }); }