worker-task-runner
Run a task inside a web worker
Your app can send to Web Worker a message with a script URL and data. Worker will execute the task and return the result back.
Worker uses StealJS module loader to asynchronously load the task module. Data is
sent via postMessage
.
The package also provides helpers for sending SharedArrayBuffer
to the worker task runner.
See demo demo/shared-array-task/demo.js
for an example.
API
Message format
type
message type, values are defined insrc/constants.js
:- from Worker:
ready
,task-result
- to Worker:
run-task
- from Worker:
taskUrl
{String} represents a URL to a task file that Worker will load and execute.taskData
{Any} data to sent to Worker to pass to the given task.result
{Any} contains data that worker produced as a result of executing the given task.
Helpers
- Main:
create
- creates a worker, returns a Task that will be resolved when the worker sends the READY event.runTask
- runs a task in a worker. Expects: a worker instance and a URL of a task module that will be run in the worker.
- Utils:
execTask
- forks a task and prints out how much time it took to execute it.
- SharedArrayBuffer:
createBuffer
bufferToArray
Example:
worker worker { if evdatatype === 'ready' console if evdatatype === 'task-result' console }
Example:
Low-level
You can use Worker
directly and just use the message API
const worker = "worker-task-runner/src/worker.js"const taskUrl = "demo/task.js"const data = 1 2 3 4 const myCallback = console worker { if evdata === "ready" // Send a task script URL to worker along with data. Note: type has to be `run-task` worker if evdata && evdatatype === "task-result" }
Functional-style with Tasks
You can use the following helpers to work in a functiona style using data.task
's Task:
// Import helpers:const create runTask execTask = // Define your task URL and data:const workerUrl = "worker-task-runner/src/worker.js"const taskUrl = "demo/task.js"const data = 1 2 3 4 // Define your main app that creates a worker and runs a task:const app = // Execute the application (see `data.task`):app
Or if you prefer a Promise:
// TODO :)
SharedArrayBuffer
Main app may look like this:
const create runTask createBuffer bufferToArray = const execTask = // Create shared array buffer to send to worker:const data = buffer: type: 'Int32Array' // Define the main app: create a worker and run a task:const app = // Execute the application (see `data.task`):
The worker task may look like:
const Task = const bufferToArray = // testTask :: a -> Task bconst testTask = { // Convert the buffer array to a typed array: let array = // Do some actions that will update the buffer array: array // Resolve the task: } moduleexports = testTask
Release Notes
0.0.2
Helpers and demo for using SharedArrayBuffer. Also define the helpers module to be package's main script.createBuffer
bufferToArray
0.0.1
Initial version:- Send data via
postMessage
. - Worker loads the given task script using StealJS module loader.
- Helpers for using
data.task
Task.
- Send data via