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

smart-queue

v0.9.3

Published

A generic purpose *"delayed"* FIFO queue with ticket system, inspired by real life.

Downloads

15

Readme

smart-queue

A generic purpose "delayed" FIFO queue with ticket system, inspired by real life.

The hows and whys

Most queue modules, at least those that I've been able to find, assume that you have already ordered your data and are ready to push it into a storage, which is not always the case, especially when asynchronous functions come into play.

The idea of a ticket system seems way easier to manage in many use cases. All you have to do is request a ticket and store the data using the ticket whenever it arrives (from other asynchronous sources), you don't have to worry about ordering your data beforehand. The queue and tickets do it for you.

Download

smart-queue is installable via:

  • GitHub git clone https://github.com/Pupix/smart-queue.git
  • npm: npm install smart-queue

Quick example

    var Queue = require('smart-queue'),
        bouncer = function (err, status) {
            console.log('The bouncer says: ' + (err || 'The queue has emptied'));
        },
        //We set a handler to be able to read errors and the queue status
        q = new Queue({handler: bouncer}),
        ticket,
        timedTicket;
    
    //Assume this is the queue of a club
    
    //A permanent ticket is sold.
    ticket = q.getTicket();
    
    //A temporary ticket is sold. It must be used within a second or it expires.
    timedTicket = q.getTicket(1000);
    
    //Jimmy arrives at the club.
    q.join('Jimmmy');
    
    //Bob arrives at the club with the ticket that was sold earlier.
    q.join(ticket, 'Bob');
    
    //After a minute the queue process starts.
    setTimeout(function () {
    
        console.log(q.next());
        => {id: 1, value: "Bob"}
        
        //As the queue is processing Megan tries to join with her ticket.
        q.join(timedTicket, 'Megan', function () {
        
            => "The bouncer says: Ticket No. 2 has expired."
            
            //Megan can still join the queue, but now she has to wait in line.
            q.join('Megan');
        });
        
        console.log(q.next());
        => {id: 3, value: "Jimmy"}
        
        console.log(q.next());
        => {id: 4, value: "Megan"}
        
        q.next();
        => "The bouncer says: The queue has emptied"
        
    }, 60000);
    
    //Even if `Jimmy` arrived at the club before anyone else, whoever reserved
    //a ticket earlier had the possibility to get in before him.

Getting started

Queue(opt)

Creates an instance of smart-queue.

Parameters

  1. opt {Object} Configuration object
    • [opt.limit = Infinity] {number} The maximum number of possible clients in the queue.
    • [opt.lifetime = Infinity] {number} The amount of time (in milliseconds) tickets are valid for, after they has been issued.
    • [opt.handler] {Function} Function that will be called when there's an error or the queue has emptied. Will be called with (error, queueStatus) as arguments.

Properties

queue

A simplified and easier to digest version of the actual queue.

It present the queue in an object with the ticket as key and the specified data as value of said ticket.

Methods

getTicket(lifetime, cb)

Creates a placeholder into the next available queue position and returns its ticket.

Parameters

  1. [lifetime] {number} The amount of time (in milliseconds) the ticket is valid for.
  2. [cb] {Function} An optional callback for client related errors. Will be called with (error, queueStatus) as arguments.

join(ticket, data, cb)

Parameters

  1. [ticket] {number | string | *} The ticket number to be used for data storage.
  2. [data] {*} The data to be stored.
  3. [cb] {Function} An optional callback for client related errors. Will be called with (error, queueStatus) as arguments.

current(cb)

Returns the current client in the queue.

Parameters

  1. [cb] {Function} An optional callback for client related errors. Will be called with (error, queueStatus) as arguments.

next(cb)

The very first time it's ran it will start the queue processing. Sequential call of next will remove the current client from the queue and advance the queue.

Parameters

  1. [cb] {Function} An optional callback for client related errors. Will be called with (error, queueStatus) as arguments.

reset()

Resets all the queue's data, except for the options passed through the Queue constructor.