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

debouncer

v1.1.2

Published

Checks if it was called too recently. Useful for not allowing the same action in too close succession. The time delay can be lengthened on each successful call.

Downloads

11

Readme

debouncer

Build Status

notes on stepping

Each time the action is allowed, the internal step number goes up. The step number can be used to determine how long to disallow the action. If the action is disallowed for 1 minute and is not attempted within 2 minutes, the internal step number goes down.

If delayTimeMs is set as a function, it will be passed the step number as it's only argument. It is the function that turns the step number into the disallowed length of time.

examples

Use debounce() with different keys:

var debounce = Debouncer(database, {
	delayTimeMs: function (step) { //ignores step
		return 5000 //always allow after 5 seconds
	}
})

var callback = function (err, allowed) {
	if (err) {
		console.warn(err)
	}
	console.log(allowed)
}

debounce('foo', callback) //true (first time)

setTimeout(function () {
	debounce('foo', callback) //false
	debounce('bar', callback) //true, (note 'bar')
}, 2500)

setTimeout(function () {
	debounce('foo', callback) //true, (been over 5 sec since last success)
	debounce('bar', callback) //false, (been under 5 sec since last success)
}, 5100)

setTimeout(function () {
	debounce('foo', callback) //true, (been over 5 sec since last success)
	debounce('bar', callback) //true, (been over 5 sec since last success)
}, 12000)

Scaling delay:

var debounce = Debouncer(database, {
	delayTimeMs: function (step) {
		return step*1000 //allow after `step` seconds (`step` is the number of successes)
	}
	//same as doing the following:
	//delayTimeMs: 1000
})

var callback = function (err, allowed) {
	if (err) {
		throw err
	}
	console.log(allowed)
}

debounce('foo', callback) //true (will be false until 1 sec after this)
debounce('foo', callback) //false

setTimeout(function () {
	debounce('foo', callback) //false
}, 900)
setTimeout(function () {
	debounce('foo', callback) //true (will be false until 2 sec after this)
}, 1100)

setTimeout(function () {
	debounce('foo', callback) //false
}, 2300)
setTimeout(function () {
	debounce('foo', callback) //false
}, 3000)
setTimeout(function () {
	debounce('foo', callback) //true (will be false until 3 sec after this)
}, 3200)

setTimeout(function () {
	debounce('foo', callback) //true (will be false until 4 sec after this)
}, 6300)

setTimeout(function () {
	debounce('foo', callback) //true (will be false until 5 sec after this)
}, 10400)

api

var Debouncer = require('debouncer')

Debouncer(db, opts)

Returns a debounce() function.

  • db takes a level db object.
  • opts takes an object with the following properties:
    • delayTimeMs can be a function, a number, or an array.
      • If it is a function, the step is passed as it's first parameter, and the return value is the delay time. return func(step)
      • If it is a number, the return value is the step multiplied by the number. return number * step
      • If it is an array, the return value is the array element at the index of step. If step is to large, it defaults to the last element. return array[step]

debounce(key, cb)

  • key takes a string. One key will not cause a different key to get debounced.
  • cb takes a function with the following arguments:
    • err is an error object, or null.
    • allowed is whether or not the action is allowed.
    • remaining is the remaining time (in ms) if the action was not allowed.

instantiation Examples

function

Always allow after 5 seconds since last allowance:

var debounce = Debouncer(database, {
	delayTimeMs: function (step) { //ignores `step`
		return 5000
	}
})

Allow after a random number of seconds between 0 and step: (I am pretty sure that this is not useful in any way.)

var debounce = Debouncer(database, {
	delayTimeMs: function (step) {
		return Math.floor(Math.random() * step) * 1000
	}
})

number

Add 2 seconds after each allowance:

var debounce = Debouncer(database, {
	delayTimeMs: 2000
})

array

  1. Allow right away.
  2. Allow after 1 second.
  3. Allow after 5 seconds.
  4. Allow after 20 seconds.
  5. Allow after 20 seconds.
  6. Allow after 20 seconds.
  7. You get it...
var debounce = Debouncer(database, {
	delayTimeMs: [0, 1000, 5000, 20000]
})

This will act like the one above. Element 0 is set to 0, but the original object is not modified.

var debounce = Debouncer(database, {
	delayTimeMs: [1000, 5000, 20000]
})

license

VOL