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

extended-worker

v1.0.0

Published

A wrapper for a web worker to simplify workers usage

Downloads

5

Readme

Extended worker library

A wrapper for a web worker

Basic usage

Use makeWorker function inside a worker file to attach the library interface to methods.

// /someDir/calculator.worker.js

import { makeWorker } from 'extended-worker/makeWorker'

function multiply(a, b) {
    return a*b
}

function divide(a, b) {
    return a/b
}

makeWorker({ multiply, divide })

Use useWorker function in the main thread to get access to the worker methods from the main thread.

// /someDir/calculator.js

import { useWorker } from 'extended-worker/useWorker'

async function calculate() {
    const { 
        multiply, 
        divide, 
        destroyContext 
    } = await useWorker('/someDir/calculator.worker.js')
    
    const a = await divide(999, 333)
    const b = await multiply(333, 3)
    
    await destroyContext()
    
    return [a, b]
}

So, you only need makeWorker and useWorker to start working with workers.

How it works

On makeWorker call, the function just wraps passed methods to provide the necessary interface for useWorker.

On useWorker call, the function does few things:

  • It checks is a worker already spawned:
    • if no, then spawns and creates a unique context for the spawned,
    • if yes, then creates a unique context for the spawned;
  • Returns the worker methods and additional methods (e.g. destroyContext).

For the same worker useWorker call will create a new context.

It is important to call destroyContext when functions (worker methods) returned by useWorker won't be used anymore, so the context could be destroyed to free memory and if no contexts left, then even destroy a worker itself.

React, Vue etc

It's not quite simple to use useWorker in a component, because of keeping in mind syncing a component lifecycle with a worker lifecycle and maintaining the order (a worker is ready -> a method call).

So, forget about using useWorker function and welcome AutoWorker

AutoWorker

AutoWorker just wraps all useWorker functionality to provide more simple usage.

// /calculorUI.js

import React from 'react'
import { AutoWorker } from 'extended-worker/autoWorker'

class CalculatorUI extends React.Component {
    constructor(props) {
        super(props)
        this.autoWorker = new AutoWorker('/someDir/calculator.worker.js')
    }

    componentDidMount() {
        this.autoWorker.create()
    }

    componentWillUnmount() {
        this.autoWorker.destroy()
    }
    
    multiply(a, b) {
        this.autoWorker.do('multiply', a, b).then(result => alert(result))
    }

    render() {
        return (
            <button onClick={(event) => this.multiply(2, 3)}>
                multiply 2 by 3
            </button>
        )
    }
  }

So, the library could be easily integrated with any UI framework.
Just call autoWorker.create on a component mount, autoWorker.destroy on unmount, and autoWorker.do to call a worker method.