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

ts-task-queue

v1.0.0

Published

ts-task-queue

Downloads

2

Readme

ts-task-queue

Create & queue tasks on a pool of workers.

Installation

npm i ts-task-queue

Usage

In this example, we'll create a queue that will add two numbers.

1. Declare the task interface

Create a file for the queue, src/add-queue.ts. In this file, we'll create an interface that will be used for our task.

Our add task will accept two inputs, a and b, which must both be numbers:

src/add-queue.ts

export interface AddTask {
	a: number;
	b: number;
}

2. Create a queue

Our task will take an AddTask as input, and output a number (the sum). So we'll create a queue that takes an AddTask as input and returns a number new Queue<AddTask, number>().

src/add-queue.ts

export interface AddTask {
	a: number;
	b: number;
}

export const addQueue = new Queue<AddTask, number>({

});

3. Setup the queue

At a minimum, our queue needs a name and a callback. The name must be a unique string to identify the queue, and the callback is a function that will perform the work that this queue does. The callback must be an async function!

src/add-queue.ts

export interface AddTask {
	a: number;
	b: number;
}

export const addQueue = new Queue<AddTask, number>({
	name: 'add-queue',
	callback: async (task: AddTask) => task.a + task.b,
});

4. Use the queue

We can use addQueue.await() to push tasks onto the queue and get a Promise back. You most likely will want to wrap your application startup in Queue.isMainThread(); so you're not running application code on the worker that should only be run on the main thread:

src/index.ts

import { AddTask, addQueue } from './add-queue';

if (addQueue.isMainThread()) {
	const sum = await addQueue.await({ a: 4, b: 8 });

	console.log('Sum is', sum);
	// Sum is 12
}

5. (Optional) Using with ts-node

If you plan to run this through ts-node, you will also need to create a javascript entry-point for the workers:

./index.js

/**
 * This file boots the worker in dev when the project is run through ts-node.
 *  - This file is not included in the build.
 */
if (!process.execArgv.includes('ts-node/register')) {
    require('ts-node').register();
}

const path = require('path');
require(path.resolve(__dirname, './src/index.ts'));

6. Using multiple queues

To run different tasks on multiple queues, you can repeat steps 1-4 to create additional queues. Just make sure the name is unique!

src/subtract-queue.ts


export interface SubtractTask {
	a: number;
	b: number;
};

export subtractQueue = new Queue<SubtractTask, number>({
	name: 'subtract-queue',
	callback: async (task: SubtractTask) => task.a - task.b,
});

src/index.ts

import { AddTask, addQueue } from './add-queue';
import { SubtractTask, subtractQueue } from './subtract-queue';

if (addQueue.isMainThread()) {
	const sum = await addQueue.await({ a: 4, b: 8 });
	console.log('Sum is', sum);
	// Sum is 12
}

if (subtractQueue.isMainThread()) {
	const diff = await subtractQueue.await({ a: 6, b: 4 });
	console.log('Difference is', diff);
	// Difference is 2
}

Customizing the Queue

Use the following options to customize the queue:

workerEntry

Specify a different file to load the workers. Default is index.js or the main file from package.json

nWorkers

Number of workers pooled for the queue. Default is 4

startup

Run a function on worker startup, e.g. establish database connection

error

Specify an error-handler function. Default logs to stderr

fatal

Specify a fatal error-handler function. Default logs to stderr and exits