# extended-worker

> A wrapper for a web worker to simplify workers usage

Latest version **1.0.0** (published 2021-11-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install extended-worker
pnpm add extended-worker
yarn add extended-worker
bun add extended-worker
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-11-18 |
| First published | 2021-11-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | evoltic |
| Maintainers | evoltic |
| Keywords | worker, web worker, thread |

## Links

- npm: https://www.npmjs.com/package/extended-worker
- Repository: https://github.com/Evoltic/extended-worker
- Homepage: https://github.com/Evoltic/extended-worker#readme
- Issues: https://github.com/Evoltic/extended-worker/issues
- npm.io page: https://npm.io/package/extended-worker

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2021-11-18

## 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.

---
_Source: https://npm.io/package/extended-worker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
