npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tbela99/workerize

v2.0.0

Published

Export functions and class into a web worker

Downloads

6

Readme

Workerize

Export functions or class into a worker context in nodejs and web browser. Tasks are cancellable.

Installation

$ npm i @tbela99/workerize

the

Node usage


import {
    workerize,
    dispose
} from "@tbela99/workerize";

// do node stuff

Web browser browser usage


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>

Using a function

The function is turned into an async proxy.

Web example


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);

Using a class

All class methods are turned into async proxies.

Node example


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);

Injecting dependencies

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);

Injecting modules


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);

Documentation

Workerize

parameters:

  • task: function or class to execute in the worker context.
  • options: optional, worker options.

worker options:

  • dependencies: array. an array of javascript files to inject as module or dependencies
  • module: boolean. If true, dependencies are injected as modules. A variable of type array called 'modules' will be injected in the scope. it contains the injected modules in the same order as they were injected.
  • signal: optional. AbortSignal instance used to abort the worker execution and delete it

Dispose

Delete the worker instance.

Usage


dispose(worker1 [, worker2, ..., workern]);

A note about the proxies parameters

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.

License

MIT OR LGPL-3.0


Thanks to Jetbrains for providing a free WebStorm license