async-deco
Advanced tools
Comparing version 1.1.0 to 2.0.0
var _limit = require('../src/limit'); | ||
var wrapper = require('../src/noop'); | ||
function limit(max, logger) { | ||
return _limit(wrapper, max, logger); | ||
function limit(max, getKey, logger) { | ||
return _limit(wrapper, max, getKey, logger); | ||
} | ||
module.exports = limit; |
{ | ||
"name": "async-deco", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "A collection of decorators you can use to wrap functions (callback or promise based) to make them more robust", | ||
@@ -34,2 +34,3 @@ "main": "index.js", | ||
"es6-promisify": "^3.0.0", | ||
"lodash": "^4.5.1", | ||
"require-all": "^2.0.0", | ||
@@ -36,0 +37,0 @@ "setimmediate": "^1.0.4" |
var _limit = require('../src/limit'); | ||
var wrapper = require('../src/promise-translator'); | ||
function limit(max, logger) { | ||
return _limit(wrapper, max, logger); | ||
function limit(max, getKey, logger) { | ||
return _limit(wrapper, max, getKey, logger); | ||
} | ||
module.exports = limit; |
@@ -5,7 +5,7 @@ async-deco | ||
This is a collection of function decorators designed to work with functions using a callback or returning a promise. | ||
In case of callbacks, it must follow the [node convention](https://docs.nodejitsu.com/articles/errors/what-are-the-error-conventions): the callback should be the last argument and its arguments should be, an error instance and the output of the function. | ||
Most of them are designed to make an asynchronous function call more robust and reliable. | ||
This is a collection of function decorators. It allows to timeout, retry, throttle, limit and much more! | ||
They can be combined together using the "compose" function (included). | ||
All decorators are designed to work with functions using a callback or returning a promise. In case of callbacks, it must follow the [node convention](https://docs.nodejitsu.com/articles/errors/what-are-the-error-conventions): the callback should be the last argument and its arguments should be, an error instance and the output of the function. | ||
Callback and promises | ||
@@ -58,3 +58,3 @@ ===================== | ||
---------------- | ||
You can pass a logger to the decorators. It is a function with this signature: | ||
You can pass a logger to the decorators. This function is called with the same arguments of the original function and should return a function with this signature: | ||
```js | ||
@@ -66,2 +66,11 @@ function (type, obj) | ||
So for example, assuming that the first argument of the decorated function is a name: | ||
```js | ||
var logger = function (name) { | ||
return function (type, obj) { | ||
console.log(name + ': ' + type); | ||
}; | ||
}; | ||
``` | ||
Decorators | ||
@@ -194,2 +203,31 @@ ========== | ||
Throttle | ||
-------- | ||
It throttles or debounces the execution of a function. The callbacks returns normally with the result. Internally it uses the "getKey" function to group the callbacks into queues. It then executes the debounced (or throttled) function. When it returns a value it will run all the callbacks of the same queue. | ||
```js | ||
var debounceDecorator = require('async-deco/callback/debounce'); | ||
var debounce = debounceDecorator(100, 'debounce', undefined, undefined, getLogger); | ||
debounce(function (..., cb) { .... }); | ||
``` | ||
The arguments: | ||
* delay [optional, default 0] the delay before the execution of the function (for debounce) or the number of milliseconds to throttle invocations to. | ||
* timingFunctionName [optional, default "throttle"] it can be the string "throttle" or "debounce" | ||
* options [optional] see below | ||
* getKey | ||
* logger function (logs "deduping" whenever is calling more than one callback with the same results) | ||
The options change meaning if they are related to the "throttle" or the "debounce": | ||
For the debounce: | ||
* leading [default false] (boolean): Specify invoking on the leading edge of the timeout. | ||
* maxWait (number): The maximum time func is allowed to be delayed before it’s invoked. | ||
* trailing [default true] (boolean): Specify invoking on the trailing edge of the timeout. | ||
For the throttle: | ||
* leading [default true] (boolean): Specify invoking on the leading edge of the timeout. | ||
* trailing [default true] (boolean): Specify invoking on the trailing edge of the timeout. | ||
For better understanding of throttle/debounce I suggest to read the "lodash" documentation and this article: https://css-tricks.com/the-difference-between-throttling-and-debouncing/. | ||
Utilities | ||
@@ -196,0 +234,0 @@ ========= |
@@ -1,4 +0,6 @@ | ||
function cacheDecorator(wrapper, cache, logger) { | ||
logger = logger || function () {}; | ||
var noopLogger = require('./noop-logger'); | ||
function cacheDecorator(wrapper, cache, getlogger) { | ||
getlogger = getlogger || noopLogger; | ||
return wrapper(function (func) { | ||
@@ -8,2 +10,3 @@ return function () { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -10,0 +13,0 @@ |
@@ -1,5 +0,7 @@ | ||
function fallbackCacheDecorator(wrapper, cache, error, logger) { | ||
var noopLogger = require('./noop-logger'); | ||
function fallbackCacheDecorator(wrapper, cache, error, getlogger) { | ||
var condition; | ||
error = error || Error; | ||
logger = logger || function () {}; | ||
getlogger = getlogger || noopLogger; | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
@@ -16,2 +18,3 @@ condition = function (err, dep) { return err instanceof error; }; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -18,0 +21,0 @@ |
@@ -1,5 +0,7 @@ | ||
function fallbackValueDecorator(wrapper, fallbackValue, error, logger) { | ||
var noopLogger = require('./noop-logger'); | ||
function fallbackValueDecorator(wrapper, fallbackValue, error, getlogger) { | ||
var condition; | ||
error = error || Error; | ||
logger = logger || function () {}; | ||
getlogger = getlogger || noopLogger; | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
@@ -15,2 +17,3 @@ condition = function (err, dep) { return err instanceof error; }; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -17,0 +20,0 @@ |
@@ -1,5 +0,7 @@ | ||
function fallbackDecorator(wrapper, fallbackFunction, error, logger) { | ||
var noopLogger = require('./noop-logger'); | ||
function fallbackDecorator(wrapper, fallbackFunction, error, getlogger) { | ||
var condition; | ||
error = error || Error; | ||
logger = logger || function () {}; | ||
getlogger = getlogger || noopLogger; | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
@@ -15,2 +17,3 @@ condition = function (err, dep) { return err instanceof error; }; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -17,0 +20,0 @@ |
require('setimmediate'); | ||
var noopLogger = require('./noop-logger'); | ||
function limitDecorator(wrapper, max, logger) { | ||
var executionNumber = 0; | ||
var queue = []; | ||
logger = logger || function () {}; | ||
function limitDecorator(wrapper, max, getKey, getlogger) { | ||
getKey = getKey || function () { return '_default'; }; | ||
getlogger = getlogger || noopLogger; | ||
function runQueue() { | ||
if (executionNumber >= max) { | ||
logger('limit', { number: executionNumber }); | ||
return; | ||
} | ||
var f = queue.shift(); | ||
if (f) { | ||
executionNumber++; | ||
f(); | ||
} | ||
} | ||
return wrapper(function (func) { | ||
var executionNumbers = {}; | ||
var queues = {}; | ||
return wrapper(function (func) { | ||
return function () { | ||
var context = this; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
var cacheKey = getKey.apply(context, args).toString(); | ||
function runQueues() { | ||
// should run ALL the queues | ||
var cacheKey, f; | ||
for (cacheKey in executionNumbers) { | ||
if (executionNumbers[cacheKey] >= max) { | ||
logger('limit', { number: executionNumbers[cacheKey], key: cacheKey }); | ||
continue; | ||
} | ||
f = queues[cacheKey].shift(); | ||
if (f) { | ||
executionNumbers[cacheKey]++; | ||
setImmediate(f); | ||
} | ||
} | ||
} | ||
if (!(cacheKey in executionNumbers)) { | ||
executionNumbers[cacheKey] = 0; | ||
queues[cacheKey] = []; | ||
} | ||
args[args.length - 1] = function (err, dep) { | ||
executionNumber--; | ||
setImmediate(runQueue); | ||
executionNumbers[cacheKey]--; | ||
runQueues(); | ||
if (executionNumbers[cacheKey] === 0) { | ||
delete executionNumbers[cacheKey]; | ||
delete queues[cacheKey]; | ||
} | ||
cb(err, dep); | ||
}; | ||
queue.push(function () { | ||
queues[cacheKey].push(function () { | ||
func.apply(context, args); | ||
}); | ||
setImmediate(runQueue); | ||
runQueues(); | ||
}; | ||
@@ -36,0 +56,0 @@ }); |
@@ -1,2 +0,4 @@ | ||
function logDecorator(wrapper, logger) { | ||
var noopLogger = require('./noop-logger'); | ||
function logDecorator(wrapper, getlogger) { | ||
return wrapper(function (func) { | ||
@@ -6,2 +8,3 @@ return function () { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -18,3 +21,3 @@ | ||
result: dep | ||
}); | ||
}); | ||
} | ||
@@ -21,0 +24,0 @@ cb(err, dep); |
@@ -1,9 +0,12 @@ | ||
function memoizeDecorator(wrapper, getKey, logger) { | ||
var cache = {}; | ||
logger = logger || function () {}; | ||
var noopLogger = require('./noop-logger'); | ||
function memoizeDecorator(wrapper, getKey, getlogger) { | ||
getlogger = getlogger || noopLogger; | ||
getKey = getKey || function () { return '_default'; }; | ||
return wrapper(function (func) { | ||
var cache = {}; | ||
return function () { | ||
var context = this; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -10,0 +13,0 @@ var cacheKey = getKey.apply(context, args).toString(); |
@@ -0,4 +1,4 @@ | ||
var noopLogger = require('./noop-logger'); | ||
function retryDecorator(wrapper, times, interval, error, logger) { | ||
function retryDecorator(wrapper, times, interval, error, getlogger) { | ||
var condition; | ||
@@ -8,7 +8,7 @@ times = times || Infinity; | ||
interval = interval || 0; | ||
var intervalFunc = typeof interval === 'function' ? | ||
var intervalFunc = typeof interval === 'function' ? | ||
interval : | ||
function () { return interval; }; | ||
logger = logger || function () {}; | ||
getlogger = getlogger || noopLogger; | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
@@ -20,2 +20,3 @@ condition = function (err, dep) { return err instanceof error; }; | ||
} | ||
return wrapper(function (func) { | ||
@@ -26,2 +27,3 @@ return function () { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -28,0 +30,0 @@ |
var TimeoutError = require('../errors/timeout-error'); | ||
var noopLogger = require('./noop-logger'); | ||
function timeout(wrapper, ms, logger) { | ||
logger = logger || function () {}; | ||
function timeout(wrapper, ms, getlogger) { | ||
getlogger = getlogger || noopLogger; | ||
return wrapper(function (func) { | ||
@@ -9,2 +11,3 @@ return function () { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
var logger = getlogger.apply(context, args); | ||
var cb = args[args.length - 1]; | ||
@@ -11,0 +14,0 @@ |
@@ -11,4 +11,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -15,0 +17,0 @@ var cache = new Cache({key: function (a, b, c) { |
@@ -11,4 +11,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -15,0 +17,0 @@ var cache = new Cache(); |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -15,3 +15,3 @@ var assert = require('chai').assert; | ||
describe('limit (callback)', function () { | ||
var limiter; | ||
var limitToOne, limitToTwo, limitToThree; | ||
var log; | ||
@@ -21,9 +21,11 @@ | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
limitToOne = limitDecorator(1, logger); | ||
limitToTwo = limitDecorator(2, logger); | ||
limitToThree = limitDecorator(3, logger); | ||
limitToOne = limitDecorator(1, undefined, logger); | ||
limitToTwo = limitDecorator(2, undefined, logger); | ||
limitToThree = limitDecorator(3, undefined, logger); | ||
}); | ||
@@ -30,0 +32,0 @@ |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -13,4 +13,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -17,0 +19,0 @@ |
@@ -11,4 +11,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -15,0 +17,0 @@ |
@@ -11,5 +11,8 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
var cache = new Cache({key: function (a, b, c) { | ||
@@ -16,0 +19,0 @@ return a + b + c; |
@@ -11,5 +11,8 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
var cache = new Cache(); | ||
@@ -16,0 +19,0 @@ cached = fallbackCacheDecorator(cache, undefined, logger); |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -15,3 +15,3 @@ var assert = require('chai').assert; | ||
describe('limit (promise)', function () { | ||
var limiter; | ||
var limitToOne, limitToTwo, limitToThree; | ||
var log; | ||
@@ -21,9 +21,11 @@ | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
limitToOne = limitDecorator(1, logger); | ||
limitToTwo = limitDecorator(2, logger); | ||
limitToThree = limitDecorator(3, logger); | ||
limitToOne = limitDecorator(1, undefined, logger); | ||
limitToTwo = limitDecorator(2, undefined, logger); | ||
limitToThree = limitDecorator(3, undefined, logger); | ||
}); | ||
@@ -30,0 +32,0 @@ |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -10,4 +10,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -14,0 +16,0 @@ |
@@ -13,4 +13,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -17,0 +19,0 @@ |
@@ -5,3 +5,3 @@ var assert = require('chai').assert; | ||
describe('timeout (callback)', function () { | ||
describe('timeout (promise)', function () { | ||
var timeout20; | ||
@@ -12,4 +12,6 @@ var log; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -16,0 +18,0 @@ |
@@ -16,4 +16,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -20,0 +22,0 @@ |
@@ -39,4 +39,6 @@ var assert = require('chai').assert; | ||
log = []; | ||
var logger = function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
var logger = function () { | ||
return function (type, obj) { | ||
log.push({type: type, obj: obj}); | ||
}; | ||
}; | ||
@@ -43,0 +45,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
74656
68
2120
274
4
+ Addedlodash@^4.5.1
+ Addedlodash@4.17.21(transitive)