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

@ambassify/throttle

v2.0.0

Published

Throttle depending on function arguments.

Downloads

286

Readme

Throttle

CircleCI

Throttle depending on function arguments.

Installation

npm install --save @ambassify/throttle

Usage

const throttle = require('@ambassify/throttle');

const throttledFunction = throttle(<function-to-throttle>, <timeout>, [<cache-key-resolver> | <options>]);

throttledFunction.clear(<...args>);

throttle

  • function-to-throttle: The function to which access should be throttled, will be called at most once during timeout period for the same cache-key (by default the first argument to this function).
  • timeout: During this period only one call to function-to-throttle will be allowed with the same cache-key (in miliseconds).
  • cache-key-resolver: This function generates the cache-key used as index into the cache. The resolver receives all of the same arguments as function-to-throttle. Default: The value of the first argument.
  • options:
    • resolver: See cache-key-resolver argument
    • cache: A custom cache instance. It should implement the Map method interface of clear, delete, get, has, and set.
    • onCached: A callback that gets passed the cache item when a new item is cached.
    • maxSize: Shortcut to using LruCache with this maxSize value.
    • rejectFailedPromise: If true will not cache promises resulting in rejection.

.clear

  • When invoked without any arguments the entire result cache is cleared.
  • When arguments are supplied they are passed to cache-key-resolver to resolve the cache key to remove from cache.

CacheItem

CacheItems are exposed through the onCached callback that can be specified in the throttle options.

  • key: The cache key for this item
  • value: The cached value for this item
  • clear: A method to clear this item from cache.
  • ttl(timeout): A method to adjust the timeout of the cache item. Pass the new timeout in milliseconds.

Example

const throttle = require('@ambassify/throttle');

let example = 0;
function myFunction(input) {
    // Do some slow operation with a different result based on input
    example += input;
    return example;
}

// Allow `myFunction` to be called once every 2 seconds for each different `input`.
const myThrottledFunction = throttle(myFunction, 2000);


myThrottledFunction(1); // 1
myThrottledFunction(1); // 1
myThrottledFunction(1); // 1

// Wait for 2 seconds

myThrottledFunction(1); // 2
myThrottledFunction(1); // 2
myThrottledFunction(1); // 2

const conditionalThrottleFunction = throttle(myFunction, 2000, {
    onCached: function(item) {
        // Only cache results for large values of input
        if (item.key < 10)
            item.clear();
    }
}

conditionalThrottleFunction(1); // 1
conditionalThrottleFunction(1); // 2
conditionalThrottleFunction(1); // 3

conditionalThrottleFunction(20); // 23
conditionalThrottleFunction(20); // 23

Caches

This library currently provides one custom caching implementation you can use for the cache option.

LruCache

This cache implementation lets you specifiy a maximum size and will evict the least recently used items in cache when the cache overflows.

new LruCache(options)

  • options:
    • maxSize: The maximum amount of items to keep in cache

Example

const throttle = require('@ambassify/throttle');
const LruCache = require('@ambassify/throttle/cache/lru');

function myFunction(input) {}

/**
 * Allow `myFunction` to be called once every 2 seconds for each different `input`
 * unless it is called more than 20 times with different `input` values. In that
 * case, it drops the cache item for the least recently used `input`
 */
const myThrottledFunction = throttle(myFunction, 2000, {
    cache: new LruCache({ maxSize: 20 })
});

Contributing

If you have some issue or code you would like to add, feel free to open a Pull Request or Issue and we will look into it as soon as we can.

License

We are releasing this under a MIT License.

About us

If you would like to know more about us, be sure to have a look at our website, or our Twitter accounts @Ambassify, Sitebase, JorgenEvens