async-deco
Advanced tools
Comparing version 6.0.0 to 6.1.0
@@ -1,2 +0,2 @@ | ||
var _race = require('../src/parallel'); | ||
var _race = require('../src/race'); | ||
module.exports = _race; |
{ | ||
"name": "async-deco", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "A collection of decorators for adding features to asynchronous functions (callback or promise based).", | ||
@@ -33,4 +33,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"es6-promisify": "^3.0.0", | ||
"little-ds-toolkit": "0.0.2", | ||
"lodash": "^4.13.1", | ||
"memoize-cache-utils": "^0.0.2", | ||
"es6-promisify": "^3.0.0", | ||
"require-all": "^2.0.0", | ||
@@ -37,0 +39,0 @@ "setimmediate": "^1.0.4", |
@@ -25,2 +25,4 @@ async-deco | ||
* [balance](#balance) | ||
* [debounce](#debounce) | ||
* [throttle](#throttle) | ||
@@ -464,6 +466,7 @@ Callback and promises | ||
var balanceDecorator = balance(function (counter, loads) { | ||
// "counter2 is the number of times I have called the function | ||
var balanceDecorator = balance(function (counter, loads, args) { | ||
// "counter" is the number of times I have called the function | ||
// "loads" is an array with length equal to the number of functions | ||
// it contains how many calls are currently running for that function | ||
// it contains how many calls are currently running for that function | ||
// "args" is an array containing the arguments of the current function call | ||
// this function should return the index of the function I want to run | ||
@@ -474,2 +477,63 @@ }); | ||
Debounce | ||
-------- | ||
This decorator is a pretty sophisticated version of debounce. In a few words, when a debounced function is called many times within a time interval, it gets executed only once. | ||
It uses the same options of [lodash debounce](https://lodash.com/docs#debounce) (that is used internally), but also allows to have multiple "debounce" contexts. | ||
The decorators takes these arguments: | ||
* wait (mandatory): it is the time interval to debounce | ||
* debounceOpts (optional): the debounce options used by lodash debounce: | ||
* leading: Specify invoking on the leading edge of the timeout. (default false) | ||
* trailing: Specify invoking on the trailing edge of the timeout. (default true) | ||
* maxWait: The maximum time the decorated function is allowed to be delayed before it’s invoked | ||
* getKey (optional): it runs against the original arguments and returns the key used for creating different "debounce" context. If is undefined there will be a single debouncing context. If it returns null there won't be debouncing. Two functions in different contexts aren't influenced each other and are executed independently. | ||
* cacheOpts (optional): the contexts are cached, in this object you can define a maxLen (maximum number of context) and a defaultTTL (contexts last only for this amount of ms). | ||
Example: | ||
```js | ||
var debounce = require('async-deco/callback/debounce'); | ||
var debounceDecorator = debounce(1000, { maxWait: 500 }, function (key) { return key; }, { maxLen: 100 }); | ||
var func = debounceDecorator(function (key, cb) { | ||
// this is the function I want to debounce | ||
}); | ||
func('r', function (err, res) { | ||
// the callback is not guaranteed to be called | ||
// for every execution, being debounced. | ||
}); | ||
``` | ||
Throttle | ||
-------- | ||
This decorator is a pretty sophisticated version of throttle. In a few words, a throttled function can be called only a certain amount of times within a time interval. | ||
It uses the same options of [lodash throttle](https://lodash.com/docs#throttle) (that is used internally), but also allows to have multiple "throttle" contexts. | ||
The decorators takes these arguments: | ||
* wait (mandatory): it is the time interval to throttle | ||
* throttleOpts (optional): the throttle options used by lodash throttle: | ||
* leading: Specify invoking on the leading edge of the timeout. (default true) | ||
* trailing: Specify invoking on the trailing edge of the timeout. (default true) | ||
* getKey (optional): it runs against the original arguments and returns the key used for creating different "throttle" context. If is undefined there will be a single debouncing context. If it returns null there won't be throttling. Two functions in different contexts aren't influenced each other and are executed independently. | ||
* cacheOpts (optional): the contexts are cached, in this object you can define a maxLen (maximum number of context) and a defaultTTL (contexts last only for this amount of ms). | ||
Example: | ||
```js | ||
var throttle = require('async-deco/callback/throttle'); | ||
var throttleDecorator = throttle(1000, { maxWait: 500 }, function (key) { return key; }, { maxLen: 100 }); | ||
var func = throttleDecorator(function (key, cb) { | ||
// this is the function I want to throttle | ||
}); | ||
func('r', function (err, res) { | ||
// the callback is not guaranteed to be called | ||
// for every execution, being throttled. | ||
}); | ||
``` | ||
A note about throttle/debounce. A function that uses these decorators is not guaranteed to be executed every time is called. And the same is true for their callback and promise (yes, there is a promise version!). If you don't need to return a promise, I advice to use the simple callback version. | ||
Utilities | ||
@@ -476,0 +540,0 @@ ========= |
@@ -15,3 +15,3 @@ var defaultLogger = require('../utils/default-logger'); | ||
var cb = args[args.length - 1]; | ||
var selected = policy(executionNumber++, loads); | ||
var selected = policy.call(context, executionNumber++, loads, args); | ||
loads[selected]++; | ||
@@ -18,0 +18,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
126119
112
3330
686
7
+ Addedlittle-ds-toolkit@0.0.2
+ Addedlodash@^4.13.1
+ Addedlittle-ds-toolkit@0.0.2(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedsizeof@1.0.0(transitive)