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

@little-island/workers

v0.1.8

Published

Stuff to help with workers

Downloads

2

Readme

Workers

These are some classes that will help easily use and manage WebWorkers in the projects you are working on. It's loosely based on SimonDev's Threading Technique with JavaScript WebWorkers that he used for his procedural world generation.

How it works

The most likely reason you would need to use many workers in projects is to complete complicated and performance-crippling tasks on different threads. I have created two classes that make doing this a walk in the park; WorkerThreadPool and WorkerThread.

WorkerThreadPool (options?: object)

This class essentially manages all the workers you create within it. It allows to run as many workers as you want at once as long as it is less than or equal to the amount if threads on your CPU. If you run more workers than the maxUsableThreads you have allowed it to use, it will queue those workers to run as soon as there is an empty thread available for it (like when one of the previous workers finishes its task). This allows for very easy management without haing to hastle to much with the performance.

  • constructor (options? object) Sets the max amount of threads that this pool is allowed to use (default is your CPU thread count).
  • add (workerThread? WorkerThread): void Stores a worker in this pool under its UUID.
  • enqueue (workerThread? WorkerThread): void This adds the worker to the queue if all the threads are busy and if it hasn't already been placed inside the queue.
  • createWorker (fileName: string, options?: object): WorkerThread This creates and returns a new worker and stores it in this pool.
  • isBusy (workerThread: WorkerThread): boolean Checks if the worker is busy.
  • isQueued (workerThread: WorkerThread): boolean Checks if the worker is in the queue.
  • checkMessage (workerThread: WorkerThread): void This is called when a worker has completed its task. It removes the worker from the "busy" list and runs the next worker in the queue. If it isn't a permanent worker, it remove it this pool's storage and terminate it.
  • run (workerThread: WorkerThread): void This decides if the worker should be executed and become "busy", or be put in the queue to be executed when a thread is available.
  • test (): void This creates a bunch of sample workers to test if this pool is queueing, executing, and terminating workers properly.

WorkerThread

This class contains the actual WebWorker. You can use tell it what data to send and what to when its task is complete. Unless the permanent option is set to "true", the worker will automatically terminate itself when its task is complete.

  • constructor (pool: WorkerThreadPool, fileName: string, options? object) Sets the thread pool that this worker will be assigned to and builds the WebWorker plus its default actions. Finally it stores this worker in the thread pool for later use.
  • postDirectMessage (data?: object): void Directly posts a message to the WebWorker without the help of the thread pool. This might interfere with the performance since it could possible execute when the threads are overloaded.
  • postMessage (data?: object, resolve?: void): void Tells this worker what data to send and what to do when the its task is completed. Once called, it will send this worker to the thread pool to either be queued or executed based on how many threads are currently busy.
  • terminate (): void Gets rid of this worker from memory.

Usage

There are a couple ways to use this, but below is the way it was intended

import { WorkerThreadPool } from '@little-island/ecs'

// let's create the thread pool we are tell it to only queue a max of 2 workers at once.
// This usually defaults to the number of threads your CPU has available.

const ThreadPool = new WorkerThreadPool( { maxUsableThreads: 2 } )

//-------------------------
/** Define some workers */
//-----------------------

// this worker takes two numbers and multiplies them together and squares them (it's a module by default).
const Worker1 = ThreadPool.createWorker( './workers/mathThing.js' )

//this worker takes an array and reverses the order of its contents and adds "animal:" to the beginning of each one.
const Worker2 = ThreadPool.createWorker( './workers/animals.js', { type: 'classic' } )

//this worker just posts "Hello wide world!".
const Worker3 = ThreadPool.createWorker( './workers/hello-world.js', { type: 'module' } )

//this worker just posts "I will stay here..." and is permanent.
const Worker4 = ThreadPool.createWorker( './workers/mr-steady.js', { permanent: true, type: 'module' } )

//-----------------------------------------------
/** Execute and send workers to do some tasks */
//---------------------------------------------

 // e.result is 784
Worker1.postMessage( { num1: 7, num2: 4 }, ( e ) => { console.log( e.result ) } )

 // e.newArray is [ "animal:fish", "animal:cat", "animal:dog" ]
Worker2.postMessage( [ 'dog', 'cat', 'fish' ], ( e ) => { console.log( e.newArray ) } )

// this worker will be queued since all 2 threads allowed have are being occupied by the previous workers.
// Once one of the first 2 finishes its task, this worker will be removed from the queue and will be executed.

// e.message is "Hello wide world!"
Worker3.postMessage( {}, ( e ) => { console.log( e.message ) } )

// this worker runs the same way as Worker3.
// Unlike all the other workers we made though, this one will stay busy until we decide to manually terminate it.

// e.message is "I will stay here..."
Worker4.postMessage( {}, ( e ) => { console.log( e.message ) } )