Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

buffered-queue

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

buffered-queue

A simple to use buffering queue (no dependencies) which flexibly buffers objects, strings, integers etc. until either a maximum size is reached, or an interval has come to an end.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
299
increased by39.07%
Maintainers
1
Weekly downloads
 
Created
Source

buffered-queue

A simple to use buffering queue as Node.js module (no dependencies), which flexibly buffers objects, strings, integers etc. until either a maximum size is reached, or an interval has come to an end.

Installation

You can simply install this module by running

npm install buffered-queue --save

from the command line withing your project's folder.

Usage

The module can be required by

var Queue = require('buffered-queue');
var q = new Queue(name, options);

There are two optional parameters, the name of the queue and the configuration options object.

Basic configuration

var q = new Queue('pageviews', {
    size: 3,               // The maximum size before the queue is flushed (default: 10)
    flushTimeout: 1000,    // The timeout in milliseconds after the queue is flushed
                           // (default: null, so there will be no recurring queue flush configured)
    verbose: true          // Whether debug info should be logged to console.log or not (default: false)
});

Add items to the queue

Basically you can add every type except functions to the queue. It's advisable that you use the same type though, or, if not, use a custom result function which handles the streamlining process (see below).

Objects
q.add({foo: "bar"});
Strings
q.add("This is a test!");

Queue flush

If the queue flushes, a flush event is triggered. If can be received as follows:

q.on("flush", function(data, name){
    console.log(data);
});

The "Custom result function"

You can also pass a function within the options object, which is then used to control the result once the flush is triggered. If there's no such function configured, the queue will just return the array in which it stored the items.

Example

The result function below will uppercase all items (considered they're strings):

var q = new Queue('test', {
    customResultFunction: function(items) {
        var temp = [];
        items.forEach(function(item, index, array) {
            temp.push(item.toUpperCase());
        });
        return temp;
    }
});

Complete example

Think about a use case where you receive input from a process, and want to log the results only after 3 seconds have passed, or 5 items have been added.

var Queue = require('buffered-queue');

var q = new Queue('example', {
    size: 5,
    flushTimeout: 3000,
    verbose: true,
    customResultFunction: function(items) {
        var temp = [];
        items.forEach(function(item, index, array) {
            temp.push(item.toUpperCase());
        });
        return temp.join('\n');
    }
});

q.on("flush", function(data, name){
    console.log(data);
});

q.add("Message 1");
q.add("Message 2");
q.add("Message 3");
q.add("Message 4");

setTimeout(function() {
    q.add("Message 5");
    q.add("Message 6");
}, 4000);

The output will be the following:

Queue (example): Added item:  Message 1
Queue (example): Maximum Queue size not reached. Currently 1 of 5 in queue!
Queue (example): Added item:  Message 2
Queue (example): Maximum Queue size not reached. Currently 2 of 5 in queue!
Queue (example): Added item:  Message 3
Queue (example): Maximum Queue size not reached. Currently 3 of 5 in queue!
Queue (example): Added item:  Message 4
Queue (example): Maximum Queue size not reached. Currently 4 of 5 in queue!
Queue (example): The timeout triggered a Queue flush! 4 items are in the Queue.
MESSAGE 1
MESSAGE 2
MESSAGE 3
MESSAGE 4
Queue (example): Added item:  Message 5
Queue (example): Maximum Queue size not reached. Currently 1 of 5 in queue!
Queue (example): Added item:  Message 6
Queue (example): Maximum Queue size not reached. Currently 2 of 5 in queue!
Queue (example): The timeout triggered a Queue flush! 2 items are in the Queue.
MESSAGE 5
MESSAGE 6

Testing

Jest is used for testing. Just run npm test.

Keywords

FAQs

Package last updated on 01 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc