Skip to content
A WebWorker extension that takes a function as argument
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE Add LICENSE Sep 12, 2017
README.md Update readme Sep 12, 2017
closure-worker.js
index.html Update demo Sep 12, 2017
package.json Initial commit Sep 12, 2017

README.md

ClosureWorker

ClosureWorker is a small extension of WebWorker that allows you to define a worker function inline instead of as a separate url.

It works by "compiling" the passed function, and injecting variables. This get's turned into a Blob and then a Object URL which can be passed to a native WebWorker.

Tested on latest version of Google Chrome and Safari.

Live demo

Installation

yarn add closure-worker
npm install closure-worker

Example

const blocking = (n) => {
    let sum = 0
    for (let i = 0; i < n; i++) {
        sum += Math.pow(Math.random() * Math.PI, 2)
    }
    // return sum
    postMessage(sum)
}

const handler = (msg) => {
    console.log(`Worker completed, result: ${msg.data}`)
}

let worker = new ClosureWorker(blocking, handler, { n: 1000000 })
You can’t perform that action at this time.