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

saturated

v1.0.0

Published

A tiny (203B) utility to enqueue items for batch processing and/or satisfying rate limits

Downloads

874

Readme

saturated codecov

A tiny (203B) utility to enqueue items for batch processing and/or satisfying rate limits.

Build a queue for your function – ideal for communicating with APIs that prefer batch/bulk processing or services that enforce rate limiting.

With saturated you provide a handler which will only be called every N items or after every X milliseconds... whichever comes first! This then allows you to push() your payload(s) into the queue, waiting for the next tick.

This module exposes three module definitions:

  • CommonJS: dist/saturated.js
  • ES Module: dist/saturated.mjs
  • UMD: dist/saturated.min.js

Install

$ npm install --save saturated

Usage

import saturated from 'saturated';

// Setup an instance
const rated = saturated(arr => {
  if (arr.length > 0) {
    console.log('~> Received', arr);
  } else {
    console.log('~> Empty...');
  }
}, {
  max: 5, // limit
  interval: 3e3 // 3s
});

// Now we have a `saturated` instance that will
//   call our function every 3 seconds or once it
//   has 5 items in its queue – whichever comes first.

// For demo purposes
const sleep = ms => new Promise(r => setTimeout(r, ms));

// Demo usage
async function demo() {
  rated.push('hello'); //=> 1
  rated.push('world'); //=> 2
  rated.push('how'); //=> 3
  rated.push('are'); //=> 4
  rated.push('you'); //=> 5

  // Queue received 5th item, immediately invoke!
  // ~> Received ['hello', 'world', 'how', 'are', 'you']
  rated.size(); //=> 0 (flushed)

  rated.push('hola'); //=> 1
  rated.push('mundo'); //=> 2
  rated.size(); //=> 2
  await sleep(3e3);

  // Interval waited 3 seconds
  // ~> Received ['hola', 'mundo']
  rated.size(); //=> 0 (flushed)

  // Wait another 3s ...
  await sleep(3e3);

  // ~> Empty...
  rated.size(); //=> 0 (flushed anyway)
}

// Init demo
demo().then(() => {
  rated.end(); // quit
});

API

saturated(handler, options)

Returns: ISaturated

handler

Type: Function Required: true

The function to invoke once a threshold has been met. It will always receive an Array of whatever item(s) you previously pushed.

Note: You may be passed an empty Array!

options.max

Type: Number Default: Infinity

The maximum size of the queue. For example, with max: 5, your handler will be invoked immediately after the 5th item was pushed to queue.

Important: Your function will be called if max is met before the next interval tick.

options.interval

Type: Number Default: 10000

The amount of time, in milliseconds, to wait before calling your handler function. Defaults to calling your handler every 10 seconds, even if the queue is empty.

ISaturated.size()

Returns: Number

Get the current size of the queue.

ISaturated.push(value)

Returns: Number

Add an item/value into the queue stack. Doing so will return the current size of the queue.

value

Type: Any

You may push any value into queue.

Important: Anything you push will be added to an Array! Pushing an Array will have your handler receive an Array of your Arrays.

ISaturated.end(toFlush)

Returns: undefined

Cancels the internal setInterval timer.

toFlush

Type: Boolean Default: false

When true, will also flush() the queue so that remaining items will be passed to your handler function.

ISaturated.flush()

Returns: undefined

Forcibly invoke your handler will all items currently in the queue.

Calling flush will restart the internal setInterval timer and empty the queue.

License

MIT © Luke Edwards