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

assist-worker

v1.1.0

Published

providing minimal APIs to allow you to use Web Workers more flexibly in your code.

Downloads

13

Readme

AssistWorker

providing minimal APIs to allow you to use Web Workers more flexibly in your code.

Installation

npm i -S assist-worker

Usage

create(job)

create and initialize worker threads。

@param {function} job - the function that needs to be put into the worker thread to execute

@returns {object} the object used to control worker thread execution and thread shutdown

Example

import assistWorker from "assist-worker";

const job = (param, workerMethods) => {
  console.log(param); // PARAM TO WORKER
};

const worker = assistWorker.create(job);

// execute the job in the worker thread
await worker.run('PARAM TO WORKER');

// shutdown worker thread
await worker.terminate();

onMessage(callback)

receive and process the message sent by the worker thread。

@param {function} callback - the callback for the worker thread to send information to the main thread

@returns {object} assistWorker

Example

import assistWorker from "assist-worker";

const job = (workerMethods) => {
  // take out the method in the object containing the worker thread internal control method
  const { postMessage } = workerMethods;
  // send message to the main thread
  postMessage('MESSAGE TO MAIN')
};

const worker = assistWorker
  // the main thread receives and processes message
  .onMessage((message) => {
      console.log(message); // MESSAGE TO MAIN
  })
  .create(job);

await worker.run();

collect(data)

collect the main thread data that needs to be used in the worker thread.

the field name of the data must be consistent with the name used in the worker hread.

@param {object} data - the main thread data used in the worker thread

@returns {object} assistWorker

Example

import assistWorker from "assist-worker";

const numbers = [1, 2, 3, 4, 5];
const sum = (arr) => arr.reduce((total, i) => total + i, 0);

const worker = assistWorker
  .collect({ numbers, sum })
  .create(() => {
    console.log(sum(numbers)); // 15
  });

await worker.run();

❗Caveat

the data collected here only supports serializable data limited by web workers itself。

However, functions are supported here, but it is necessary to ensure that the main thread variables referenced in the function body are also collected, for example:

...
const id = 1;

const test = () => {
  console.log(id);
});

assistWorker.collect({ test })     // ✖️ ReferenceError: id is not defined
assistWorker.collect({ id, test }) // ✔️
...