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

concurrent-worker

v1.2.1

Published

Multithreading for javascript

Downloads

6

Readme

npm version Build Status Coverage Status Greenkeeper badge

Concurrent worker

This library allowes you to create web workers inline, focussed on concurrency and control flow. It is Promise based, this allowes control flow to be regulated via Promise chains as well as async / await. You can also pass in and use functions and objects / primitives from the main thread via the context. All exceptions and promise rejections are propagated to the main thread and can be handled in a promise catch or try / catch block in an async function.

There are two worker creation methods, serial and concurrent. A serial worker will create a single Worker that all calls to it will be executed on. A concurrent worker will create a new Worker for each call to it, this allowes you to run multiple calls in parallel. The API's of both are identical.

npm install concurrent-worker --save
import { serial } from "concurrent-worker";

const worker = serial((x, y) => x + y);

worker.run([1, 2]).then(result => {
  console.log(result); // 3
});

Config

The second, optional, argument to the creation method is a configuration object, this has the following properties:

| Key | Type | Description | Default | | ---------------- | -------- | ----------------------------------------------------------------------------------------------------------------------- | -------- | | context | object | All functions, objects and primitives on this object are available in the Worker, they can be accessed via this.{key} | {} | | inTransferrable | Function | A function that returns an array of Transferrable objects from the input arguments | () => [] | | outTransferrable | Function | A function that returns an array of Transferrable objects from the result object. | () => [] | | rootUrl | string | Scripts loaded from a relative path need to be prepended with the sites root URL. | '' | | scripts | string[] | Array of script URL's, these scripts are loaded in the worker when it's instantiated. | [] |

Examples

Concurrent worker with async / await and Promise.all

import { concurrent } from "concurrent-worker";

const sum = (x, y) => x + y;
const concurrentWorker = concurrent(sum);

const runAsyncTasks = async () => {
  // These three calls will run concurrently on 3 seperate workers
  const processes = Promise.all([
    concurrentWorker.run([1, 2]),
    concurrentWorker.run([2, 3]),
    concurrentWorker.run([3, 4])
  ]);

  const results = await processes; // [3, 5, 7]
  const summed = results.reduce((acc, val) => (acc += val), 0);

  console.log(summed); // 15
};

Using context with TypeScript

import { serial } from "concurrent-worker";

const constNumber = 5;

function add(this: typeof ctx, x: number, y: number) {
  return x + y + this.constNumber;
}

function run(this: typeof ctx, x: number, y: number) {
  return this.add(x, y);
}

const context = { add, constNumber };
const worker = serial(run, { context });

Using in and out Transferrables

import { serial } from "concurrent-worker";

const arrayAdd = (n: number, arr: Float32Array) => arr.map(x => x + n);

const worker = serial(arrayAdd, {
  inTransferrable: ([n, arr]) => [arr.buffer],
  outTransferrable: arr => [arr.buffer]
});

const arr = new Float32Array([1, 2, 3, 4]);

worker.run([5, arr]).then(console.log); // Float32Array([6, 7, 8, 9])

Using a combination of self hosted and external scripts

import { concurrent } from "concurrent-worker";

// Function using lodash map from cdn and sum function from self hosted script import.
const arrayAdd = (n: number, arr: Float32Array) => _.map(arr, x => sum(x, n));

const worker = concurrent(arrayAdd, {
  inTransferrable: ([n, arr]) => [arr.buffer],
  outTransferrable: arr => [arr.buffer],
  rootUrl: "http://www.mydomain.com",
  scripts: [
    "/js/sum.js",
    "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"
  ]
});

const arr = new Float32Array([1, 2, 3, 4]);

worker.run([5, arr]).then(console.log); // Float32Array([6, 7, 8, 9])

Returning a Promise

If your wrapped function returns a Promise, it will be resolved in the worker before returning to the main thread.

import { serial } from "concurrent-worker";

const worker = serial((x, y) => Promise.resolve(x + y));

worker.run([1, 2]).then(result => {
  console.log(result); // 3
});