New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

web-worker-helper

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web-worker-helper

Utilities for running tasks on worker threads

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
28K
decreased by-4.23%
Maintainers
1
Weekly downloads
 
Created
Source

web-worker-helper

Forked from @loaders.gl/worker-utils refactoring

耗时任务

// 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;
}

创建耗时任务的 worker

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

createWorker(async (data: number, options?: Record<string, any>) => {
  const result = fibonacci(data);
  return result;
});

打包 worker 文件为 umd

主线程调用 worker

  1. 解析 worker
import { getWorkerURL, WorkerFarm } from "17-worker";

export async function parseWorker(workerName: string, data: any, options?: Record<string, any> ) {
  const url = getWorkerURL (workerName, options);
  const workerFarm = WorkerFarm.getWorkerFarm({ maxConcurrency: 3, reuseWorkers: true });
  const workerPool = workerFarm.getWorkerPool({ name: workerName, url })
  // const workerPool = workerController.getWorkerPool({ name: workerName, url });
  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. 调用耗时任务
import { parseWorker } from '17-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;
}

Keywords

FAQs

Package last updated on 24 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc