web-worker-helper
Utilities for running tasks on worker threads
Last updated 9 months ago by lvisei .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install web-worker-helper 
SYNC missed versions from official npm registry.

web-worker-helper

Forked from @loaders.gl/worker-utils implementation

Time consuming task

// fibonacci.js
export function fibonacci(n: number) {
  let n1 = 1,
    n2 = 1,
    sum = 1;
  for (let i = 3; i <= n; i += 1) {
    sum = n1 + n2;
    n1 = n2;
    n2 = sum;
  }
  return sum;
}

Creating workers for time-consuming task

// fibonacci.worker.js
import { createWorker } from 'web-worker-helper';
import { fibonacci } from './fibonacci';

createWorker(async (data: number) => {
  const result = fibonacci(data);
  return result;
});

Bundle worker

Main thread call worker

  1. parse worker
import { WorkerFarm } from 'web-worker-helper';

export async function parseWorker(
  workerName: string,
  data: any,
  options?: Record<string, any> = { maxConcurrency: 3, reuseWorkers: true }
) {
  const url = getWorkerURL({ name: workerName });
  // const source = `fibonacci codeString`
  const workerFarm = WorkerFarm.getWorkerFarm({
    maxConcurrency: options.maxConcurrency,
    reuseWorkers: options.reuseWorkers,
  });
  const workerPool = workerFarm.getWorkerPool({ name: workerName, url });
  // const workerPool = workerFarm.getWorkerPool({ name: workerName, source });
  const job = await workerPool.startJob(workerName, (job, type, data) => job.done(data));

  job.postMessage('process', { input: data });

  const result = await job.result;
  return result.result;
}
  1. Call time-consuming task
import { parseWorker } from './parse-worker';
import { fibonacci } from './fibonacci';

export async function starFibonacci(iskorker, data) {
  if (iskorker) {
    const result = await parseWorker('fibonacci-worker', data, {});
    return result;
  }

  const result = await fibonacci(data);
  return result;
}

Current Tags

  • 0.0.4-beta.4                                ...           beta (9 months ago)
  • 0.0.4                                ...           latest (9 months ago)

9 Versions

  • 0.0.4                                ...           9 months ago
  • 0.0.4-beta.4                                ...           9 months ago
  • 0.0.4-beta.3                                ...           9 months ago
  • 0.0.4-beta.2                                ...           9 months ago
  • 0.0.4-beta.1                                ...           9 months ago
  • 0.0.4-beta.0                                ...           9 months ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Total 1
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (19)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE