Throttle depending on function arguments.
npm install --save @ambassify/throttle
const throttle = require('@ambassify/throttle');
const throttledFunction = throttle(<function-to-throttle>, <timeout>, [<cache-key-resolver> | <options>]);
throttledFunction.clear(<...args>);
- function-to-throttle: The function to which access should be throttled, will be called at most once during
timeout
period for the samecache-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 samecache-key
(in milliseconds). - cache-key-resolver: This function generates the
cache-key
used as index into the cache. The resolver receives all of the same arguments asfunction-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 ofclear
,delete
,get
,has
, andset
. - 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.
- resolver: See
- 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.
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.
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
This library currently provides one custom caching implementation you can use for the cache
option.
This cache implementation lets you specifiy a maximum size and will evict the least recently used items in cache when the cache overflows.
- options:
- maxSize: The maximum amount of items to keep in cache
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 })
});
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.
We are releasing this under a MIT License.
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.