# easy-worker-pool

> A webworker pool rpc management framework

Latest version **0.0.2** (published 2021-12-22) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install easy-worker-pool
pnpm add easy-worker-pool
yarn add easy-worker-pool
bun add easy-worker-pool
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2021-12-22 |
| First published | 2021-12-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 135 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tom You Zhou |
| Maintainers | bigtomm |
| Keywords | webworker, pool, thread, rpc |

## Links

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

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

- 0.0.2 (latest) — 2021-12-22

## README

## easy-worker-pool


A [RPC](https://www.runoob.com)-like framework for managing webworker pool, you can make full use of cpu resources to do some things, just write your method in a file like "worker.js", and then you can call these methods remotely from the main(UI) thread or other workers, you don't need to worry about the follow-up, the framework will evenly distribute a large number of calls to different webworkers for execution.

### How to use


#### Installation:
```
$ npm i easy-worker-pool
```

#### In main thread

```javascript
// App.tsx or some entry index.ts
import { WorkerCenter } from 'easy-worker-pool';
import WorkerUrl from './pool.worker.ts';
// import { othersWorker } from './othersWorker-creator.ts';

export let workerMethods: WorkerProxy;

async function initPoolWorker() {
  const center = new WorkerCenter();
  const proxy = center.init<WorkerProxy>(WorkerUrl, { maxWorkerNumbers: 6 });
  const workerMethods = await proxy;

  // If you need to use poolworker in other workers, you need to add the following line to establish a connection for them:
  await center.bindOtherWorkerAndPoolWorkers(othersWorker);
  // now the connection between worker pool and other workers is established
}

initPoolWorker().then(()=>{
  // now yopu can call remotely the poolworkers's methods in main thread
  workerMethods
   .fn1(123, 23132)
  //  Another usage, you can pass additional parameters:
  //  .fn1({ methodArgs: [123, 23132], proxyOptions: { timeout: 500 } })
   .then(rsp => console.log('pool >> fn1 >> main||rsp', rsp));
   .catch(err => console.log('pool >> fn1 >> main||err', err));

  // ...do something else
})

```


#### In pool-worker file
```javascript
// pool.worker.ts
import { registerPoolWorker } from 'easy-worker-pool';

export const fn1 = async (ff11: number, asd: number) => {
  return ff11 + asd;
};

export const fn3 = (ff333: string) => {
  return ff333;
};

export const fn2 = (ff222: boolean) => {
  return ff222;
};

const workerProxy = registerPoolWorker({
  fn1,
  fn2,
  fn3,
});

// Use the following types in other files and you will get typescript infer
export type WorkerProxy = typeof workerProxy;

// if you use webpack worker-loader, can export the worker file like:
export default {} as WorkerFile

```


#### In other worker file
```javascript
// others.worker.ts
import { registerOtherWorker } from 'easy-worker-pool';

export let workerMethods: WorkerProxy;

export const initPoolWorker = () => {
  registerOtherWorker<WorkerProxy>().then(methods => {
    workerMethods = methods;

  });
};

initPoolWorker()


// After the initialization(center.bindOtherWorkerAndPoolWorkers) is complete, you can directly use workerMethods to rpc a poolworker's method
// workerMethods
//   .fn1(123, 23132)
//   .fn1({ methodArgs: [123, 564], proxyOptions: { timeout: 100 } })
//   .catch(e => {}));
```

### TypeScript Support
easy-worker-pool is written in TypeScript, of course you can also use it in Javascript, just ignore the TS syntax in the example

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