
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
@tbela99/workerize
Advanced tools
Export functions or class into a worker context in nodejs and web browser. Tasks are cancellable.
$ npm i @tbela99/workerize
the
import {
workerize,
dispose
} from "@tbela99/workerize";
// do node stuff
import {
workerize,
dispose
} from "@tbela99/workerize/web";
// do browser stuff
Alternatively, you can use the 'browser.js' script which uses the UMD module format.
<script src="dist/browser.js"></script>
<script>
const func = workerize.workerize(function () {
// do something
})
</script>
The function is turned into an async proxy.
import {
workerize,
dispose
} from "@tbela99/workerize/web";
// async and arrow functions can be passed as well
const func = workerize(function (...args) {
return args.reduce((acc, curr) => acc + curr, 0);
});
response = await func(1, 2, 70); // 74
// terminate the service worker
dispose(func);
All class methods are turned into async proxies.
import {
workerize,
dispose
} from "@tbela99/workerize";
// pass a class definition
const Class = workerize(class {
type;
name;
age;
constructor(type, age, name = '') {
this.type = type;
this.age = age;
this.name = name;
if (name === '') {
let number = Math.floor(3 + 3 * Math.random());
while (number--) {
this.name += String.fromCharCode([65, 97][Math.floor(2 * Math.random())] + Math.floor(26 * Math.random()))
}
}
}
say() {
return `${this.name} says: I am a ${this.age} year(s) old ${this.type}`;
}
});
// create an instance
const dog = new Class('Dog', 2, 'Marvin');
console.log(await dog.say()); // 'Marin says: I am a 2 years(s) old Dog'
// terminate the service worker
dispose(dog);
You can inject javascript libraries into the worker context.
import {
workerize,
dispose
} from "@tbela99/workerize";
const func = workerize(function (...args) {
// sum function is defined in ./js/sum.js
return sum(...args);
}, {dependencies: ['./js/sum.js']});
const result = await func(5, 43, 10);
console.log(result); // 58
dispose(func);
import {
workerize,
dispose
} from "@tbela99/workerize";
const func = workerize(function (...args) {
// an array called 'modules' containing the declared modules is injected in the scope
// modules are in the same order they are declared
// add is exported by the module ./js/add.js
return modules[0].add(...args);
}, {dependencies: ['./js/add.js'], module: true});
const result = await func(5, 43, 10);
console.log(result); // 58
dispose(func);
parameters:
worker options:
Delete the worker instance.
dispose(worker1 [, worker2, ..., workern]);
All parameters you pass to the proxy must be transferable. Objects will be serialized and deserialized as JSON data. All methods, property settter and getter are removed.
MIT OR LGPL-3.0
Thanks to Jetbrains for providing a free WebStorm license
FAQs
Export functions and class into a web worker
The npm package @tbela99/workerize receives a total of 0 weekly downloads. As such, @tbela99/workerize popularity was classified as not popular.
We found that @tbela99/workerize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.