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

@supercat1337/queue

v1.0.4

Published

Queue is a class that manages the tasks in the queue. It emits events when tasks are added or removed.

Downloads

141

Readme

Queue

A JavaScript class for managing tasks in a queue.

Overview

Queue is a lightweight JavaScript class that provides a simple way to manage tasks in a queue. It emits events when tasks are added or removed, and provides methods for adding, removing, and waiting for tasks to complete.

Features

  • Task Queue: Queue allows you to add tasks to a queue and manage their execution.
  • Event Emission: Queue emits events when tasks are added or removed from the queue.
  • Task Management: Queue provides methods for adding, removing, and waiting for tasks to complete.

Usage

import { Queue } from "@supercat1337/queue"; // import Queue class

/**
 * Simulate a task that takes 1 second to complete.
 * @param {string} data - the task data
 */
async function task(data) { // define task function
    console.log(data); // log the task data
    await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second
}

async function init() { 

    const queue = new Queue(); // create Queue instance
    const tasks_limit = 3; // set the limit of tasks in the queue

    let counter = 0; // initialize counter

    while (counter < 10) { // loop until counter is 10
        counter++; // increment counter

        queue.addTaskAndRun(async () => { // add task to queue and run it
            return task("task " + counter); // run the task
        });

        console.log("queue.size is " + queue.size); // log the size of the queue
        await queue.waitForLessThan(tasks_limit); // wait until the number of tasks in the queue is less than tasks_limit
    }

    await queue.waitUntilEmpty(); // wait until all tasks are completed
}

await init(); // call init function

API Documentation

Queue Class

Methods

  • addTaskAndRun(task): Add a task to the queue and run it. If the task resolves, it is removed from the queue. If the task rejects, the error is logged to the console and the task is removed from the queue.
  • waitForLessThan(limit): Wait until the number of tasks in the queue is less than the specified limit.
  • waitUntilEmpty(): Wait until all tasks are completed.
  • add(...data): Add a task to the queue and return its task ID.
  • remove(taskId): Remove a task from the queue by its task ID.
  • on(event, listener): Subscribe to events on the Queue instance.

Properties

  • size: The number of tasks in the queue.

Events

  • add: Emitted when a task is added to the queue.
  • remove: Emitted when a task is removed from the queue.

License

Queue is licensed under the MIT License.

Contributing

Contributions are welcome! Please submit a pull request with your changes.