Creating web worker files is difficult, when you are using build tools and frameworks. Making sure your web worker scripts end up in the right path after build is a hassle.
Duty Free will allow you to create your web workers with just a callback; no extra files needed.
- No Dependencies - no dependencies
- TypeScript - Everything is TypeScript based
- Lint - Pre-configured ESlint with Airbnb config
- Small - It almost doesn't exist
This package is constrained by limitations of Web Workers, and Function.prototype.toString
. Please be aware of the following, before using it:
-
Please note that you do have to pass in a function defined by a Function Declaration, not a Function Expression.
-
You cannot use imports in your callback. Scoping rules do not apply here. Visit here for more info.
-
If you use a module bundler like Webpack, ensure you are writing your callback is compatible with the target ES version (likely ES5), otherwise the bundler will modify the code, where it will not run in the isolation, while it needs to.
This module is distributed via npm which should be installed as one of your project's dependencies
:
npm i --save dutyfree
- Install
dutyfree
- Import
createWorker
from the package - Pass your callback to
dutyfree
. - Your instance is ready
import { createWorker } from "dutyfree";
function source() {
onmessage = function (e) {
postMessage(`Worker says ${e.data}`);
};
}
const myWorker = createWorker(source);
myWorker.onmessage = function (e) {
console.log("Received: " + e.data);
};
worker.postMessage("Dooti");
worker.terminate();
This package is a beginner-friendly package. If you don't know where to start, visit Make a Pull Request to learn how to make pull requests.
Please visit Contributing for more info.
Please visit Code of Conduct.
MIT