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

ricks-bricks

v1.1.0

Published

Run cb if called X times within X ms and hasn't been run in X ms

Downloads

3

Readme

ricks-bricks NPM version js-standard-style Dependency Status

Run if happened X times within X ms and hasn't been run in X ms

What is it this?

Simply put it's yet another throttling utility. Specifically, I made to restart a server process if ECONNREFUSED is encountered by the reverse proxy beyond the threshold. It has plenty of other potential uses, but more than anything I wanted the logic isolated and reusable. So here it is in module form.

Double you tea eff is that name 'bout brah?

Run if happened X times within X seconds(ms, actually) and hasn't been run in X seconds(ms)
-> rihxtwxsahbrixs
-> rix-twix-sabrixs
-> ricks-bricks

Install

$ npm install --save ricks-bricks

Usage

Only execute callback if called with same signature count >= threshold

const rb = require('ricks-bricks')
const someCb = () => console.log('oak yeah')

// set the threshold
const rbOpts = { threshold: 3 }

// call with sig 2 times, 1 less than threshold
rb('sig', someCb, rbOpts)
rb('sig', someCb, rbOpts)
// call with differentSig once, doesn't trigger callback
rb('differentSig', someCb, rbOpts)
// call with sig a third time, now meets threshold, executes callback
rb('sig', someCb, rbOpts)

Only execute callback if (...) within specified timeframe

const rb = require('ricks-bricks')
const ms = require('pico-ms')
const someCb = () => console.log('oak yeah')
const delay = async (d) => {
  await new Promise((resolve, reject) => setTimeout(resolve, d))
}

// this time also we also set resetAfter
const rbOpts = { threshold: 3, resetAfter: ms('5sec') }

async function main () {
// call with sig 2 times, 1 less than threshold
  rb('sig', someCb, rbOpts)
  rb('sig', someCb, rbOpts)
  // wait 7 seconds
  await delay(ms('7sec'))
  // call with sig 3rd time, outside resetAfter, doesn't trigger callback
  rb('sig', someCb, rbOpts)
  // now call again once, shouldn't meet threshold
  rb('sig', someCb, rbOpts)
  // now call again, should meet threshold and call
  rb('sig', someCb, rbOpts)
}
main()

Only execute callback if (...) throttling calls for X ms

const rb = require('ricks-bricks')
const ms = require('pico-ms')
const someCb = () => console.log('oak yeah')
const delay = async (d) => {
  await new Promise((resolve, reject) => setTimeout(resolve, d))
}

// this time also we also set throttle
const rbOpts = { threshold: 3, throttle: ms('5sec') }

async function main () {
  // call with sig 3 times, should execute callback
  rb('sig', someCb, rbOpts)
  rb('sig', someCb, rbOpts)
  rb('sig', someCb, rbOpts)
  // call a 4th time and 5th time, it should not execute, end count should be 1
  rb('sig', someCb, rbOpts)
  rb('sig', someCb, rbOpts)
  // wait 7 seconds
  await delay(ms('7sec'))
  // call again, should effectively be the 2nd call as far as count is concerned
  rb('sig', someCb, rbOpts)
  // and we call one last time, this should be 3 and trigger cb
  rb('sig', someCb, rbOpts)
}
main()

API

rb(signature, callback, options)

  • signature (String - required)
    • Identifier to track
  • callback (Function - required)
    • Function to execute when conditions met
  • options (Object - optional)
    • threshold (Number - optional)
      • How many times rb is called for signature before we execute callback
    • throttle (Number - milliseconds - optional)
      • Once callback is executed don't track until this many ms have passed
    • resetAfter (Number - milliseconds - optional)
      • Reset tracking once this many ms have passed since previous call

rb.cleanupInterval = ms

  • ms (Number - milliseconds - optional [defaults to 30000 (30secs)])
    • Clear stale objects from tracking object every ms millseconds

License

MIT © Andrew Carpenter