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

async-task-dispatcher

v1.1.4

Published

TypeScript library that implements a generic queue for processing tasks asynchronously and obtaining their results

Downloads

14

Readme

Async Task Dispatcher

npm version CI

Async Task Dispatcher is a TypeScript library that implements a generic queue for processing tasks asynchronously and obtaining their results.

Features

  • Allows you to push tasks to the queue and receive individual results asynchronously.
  • Supports configurable work policies and intervals for processing tasks.
  • Allows you to set limits for the queue size and buffer size.
  • Simple and easy-to-use API.

Class: Queue<T, R>

Type Parameters

  • T: Task type
  • R: Result type

Constructor

The constructor takes a QueueConfig object as an argument.

QueueConfig<T, R>

  • worker (optional): Worker function that processes tasks of type T and returns a Promise of type R.
  • limit (optional): Limits the maximum allowed size of the tasks in queue.
  • workPolicy: Configures how tasks are processed in the queue.
    • after-add (default): The worker is started immediately after calling the push() method.
    • async-cycle-one: The worker is started in a separate asynchronous call with an optional interval.
    • async-cycle-many: Same as async-cycle-one, but with multiple asynchronous calls. The number of calls is determined by groupSize.
    • undefined: No specific work policy is applied.

Methods

  • get(): Promise<R>: Returns a single ready task result. If there are no results available, it will wait for them.
  • push(item: T | T[], worker?: WorkerFn<T, R>): Promise<void>: Adds tasks to the queue.
  • clear(): void: Stops the background worker.
  • get length(): number: Returns the size of the task queue.
  • get buff(): number: Returns the size of the results buffer.

Usage

import { Queue, QueueConfig } from 'async-task-dispatcher';

const config: QueueConfig<MyTask, MyResult> = {
  worker: async (task: MyTask): Promise<MyResult> => {
    // Process the task and return a result.
  },
  queueSizeLimit: 100,
  buffSizeLimit: 50,
  workPolicy: 'async-cycle-many',
  groupSize: 5,
  interval: 1000,
};

const queue = new Queue<MyTask, MyResult>(config);

// Add tasks to the queue.
await queue.push(task1);
await queue.push([task2, task3]);

// Get a single result of a processed task.
const result = await queue.get();

// Check the size of the task queue and the results buffer.
console.log(queue.length, queue.buff);

// Clear the background worker.
queue.clear();

Example


import { Queue, QueueConfig } from 'async-task-dispatcher';

const veryStrangeConverterNumberToString = async (x: number): Promise<string> => {
    return new Promise(done => {
        setTimeout(() => done(x.toString()), 100 * Math.random())
    })
}
const queue = new Queue<number, string>({
  worker: veryStrangeConverterNumberToString,
  queueSizeLimit: 3,
  buffSizeLimit: 3,
  workPolicy: 'async-cycle-one',
});

async function main() {
    setTimeout(async () => {
        const result0 = await queue.get() // 1
        const result1 = await queue.get() // 2
        const result2 = await queue.get() // 3
    }, 2000)

    await queue.push([1,2])
    await queue.push(3)

    // oops, sleep, according to queueSizeLimit, our queue cannot exceed 3
    // but timeout will be triggered soon
    await queue.push(4)
}

main()

Installation

npm install async-task-dispatcher

License

This project is licensed under the MIT License.