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

@thblt-thlgn/call-me-thread

v0.0.6

Published

Typescript thread like implementation

Downloads

3

Readme

CallMeThread

npm version

A simple thread implementation using node workers

Contributions are more than welcome

Install

# Using npm
$ npm install @thblt-thlgn/call-me-thread

# Or using yarn
$ yarn add @thblt-thlgn/call-me-thread

How does it work ?

For each thread or pool you create, a JavaScript worker file is generated, and binded to the API. It means that this library has the same limitations than NodeJS workers.

Have a look to the worker API for more details

How to use ?

Processor function

The processor function is the one passed to your threads or pools (it's basically the code you wanna run in a thread). As this function is copied into a generated javascript file, you cannot use variables from outside the processor function. Hopefully, some options allow you to use external libraries, or worker scoped variables.

/**
 * @data Object - Data passed from thread.pushData or pool.pushData function
 * @worker Object - Worker scoped data passed on thread or pool creation
 * @libraries Object - Object containing the libraries passed on the thread or pool creation
 */
const processor = (data, workerData, libraries) => {
  // use the workerData
  workerData.calls += 1;

  // use a librarie
  libraries.myLib.doSomething();

  // do some stuff;
  return data.value * 2;
};

Threads

Run a processor function in a NodeJS worker

import { threadManager, Thread } from '@thblt-thlgn/call-me-thread';

const threadOptions = {
  workerData: {
    calls: 0,
  },
  libraries: {
    path: 'fs',
    name: 'myLib';
  }
};

// Threads can be created from the thread-manager
const thread =  threadManager.createThread(processor, threadOptions);

// Or directly using the class
const thread = new Thread(processor, threadOptions);

// You can subscribe to the processed data
thread.subscribe(console.log);

// Or catch the errors if any
thread.catch(console.error);

// You can push data to the thread as many time as you need
// But please note that only primitive data can be pushed (no functions)
thread.pushData({ value: Math.random() });

// And the thread can be stopped when you are done with it
thread.stop(() => {
  console.log('I am printed when all the pushed data is processed by the worker')
})

// Or force the thread stop
thread.stop(() => {
  console.log('I am called before all the pushed data is processed by the worker')
}, true)

// Calls can be chained
thread
  .subscribe(console.log)
  .catch(console.error)
  .pushData({ value: Math.random() })
  .pushData({ value: Math.random() })
  .pushData({ value: Math.random() })
  .stop();

Behind the hood, a message queue is implemented (using stream for better memory efficiency) for each thread

Pools

Run a processor function in a pool of thread.

import { threadManager, Pool } from '@thblt-thlgn/call-me-thread';

const poolOptions = {
  workerData: {
    calls: 0,
  },
  libraries: {
    path: 'fs',
    name: 'myLib';
  }
};

// Pools can be created from the thread-manager
// Here a pool of 4 threads is gonna be created
const pool =  poolManager.createPool(processor, 4, poolOptions);

// Or directly using the class
// Here a pool of 2 threads is gonna be created
const pool = new Pool(processor, 2, poolOptions);

// You can subscribe to the processed data
pool.subscribe(console.log);

// Or catch the errors if any
pool.catch(console.error);

// You can push data to the pool as many time as you need
// But please note that only primitive data can be pushed (no functions)
pool.pushData({ value: Math.random() });

// And the pool can be stopped when you are done with it
pool.stop(() => {
  console.log('I am printed when all my threads are stopped')
})

// Or force the pool stop
pool.stop(() => {
  console.log('Force stop is called on all my threads')
}, true)

// Calls can be chained
pool
  .subscribe(console.log)
  .catch(console.error)
  .pushData({ value: Math.random() })
  .pushData({ value: Math.random() })
  .pushData({ value: Math.random() })
  .stop();

Behind the hood, the data will be sent to the threads with the smallest queue.

Single usage threads / promisified

Sometimes, you just need to run a process in thread, but only once

import threadManager from '@thblt-thlgn/call-me-thread';

threadManager
  .runInThread(processor, { value: Math.random }, threadOptions)
  .then(console.log)
  .catch(console.error);

Thread-manager

This class is a singleton. It allows thread and pools creation, as well as storing them (useful for debugging purpose)

import threadManager from '@thblt-thlgn/call-me-thread';

// Get all the active threads (as a Map)
const threads = threadManager.threads;

// Get all the active pools (as a Map)
const pools = threadManager.pools;

// Create a thread
const thread = threadManager.createThread(processor, threadOptions);

// Create a pool
const pool = threadManager.createPool(processor, 4, threadOptions);

// Create a single usage thread
threadManager
  .runInThread(processor, { value: Math.random }, threadOptions)
  .then(console.log)
  .catch(console.error);

Example

In this example, a pool is used to create some arrays with random values, and another one to sort those arrays.

const threadManager = require('@thbl-thlgn/call-me-thread');
const ARRAY_SIZE = 10000000;
const THREADS = 4;

const createArrays = ({ size, maxValue }) =>
  Array.from(new Array(size)).map(() => Math.round(Math.random() * maxValue));

const sorter = ({ array }) => array.sort((a, b) => a - b);

const sortingPool = threadManager
  .createPool(sorter, THREADS)
  .subscribe(console.log)
  .catch(console.error);

const creationPool = threadManager
  .createPool(createArrays, THREADS)
  .subscribe((array) => {
    sortingPool.pushData({ array });
  })
  .catch(console.error);

console.time('Time');

for (let i = 0; i < 8; i++) {
  creationPool.pushData({ size: ARRAY_SIZE, maxValue: ARRAY_SIZE });
}

creationPool.stop(() => {
  sortingPool.stop(() => console.timeEnd('Time'));
});