async-deco
Advanced tools
Comparing version 7.1.0 to 7.2.0
{ | ||
"name": "async-deco", | ||
"version": "7.1.0", | ||
"version": "7.2.0", | ||
"description": "A collection of decorators for adding features to asynchronous functions (callback or promise based).", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -192,2 +192,5 @@ async-deco | ||
The "options" object may contains: | ||
* an "error" attribute. This can be either an Error constructor function, or a function returning true if the result should be considered an error. The function takes as argument the output of the decorated function: error, result. If the result is an error the returned value is not cached. | ||
It logs: | ||
@@ -194,0 +197,0 @@ * "cache-hit" with {key: cache key, result: cache result} |
var defaultLogger = require('../utils/default-logger'); | ||
var getErrorCondition = require('./get-error-condition'); | ||
function cacheDecorator(wrapper, cache, opts) { | ||
opts = opts || {}; | ||
var condition = getErrorCondition(opts.error); | ||
@@ -29,3 +31,3 @@ return wrapper(function (func) { | ||
args[args.length - 1] = function (err, res) { | ||
if (!err) { | ||
if (!condition(err, res)) { | ||
key = cache.push(args, res); | ||
@@ -32,0 +34,0 @@ if (key) { |
var defaultLogger = require('../utils/default-logger'); | ||
var getErrorCondition = require('./get-error-condition'); | ||
function fallbackCacheDecorator(wrapper, cache, opts) { | ||
var condition; | ||
opts = opts || {}; | ||
@@ -9,10 +9,4 @@ var error = opts.error || Error; | ||
var noPush = opts.noPush; | ||
var condition = getErrorCondition(opts.error); | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
condition = function (err, res) { return err instanceof error; }; | ||
} | ||
else { | ||
condition = error; | ||
} | ||
return wrapper(function (func) { | ||
@@ -19,0 +13,0 @@ return function () { |
var defaultLogger = require('../utils/default-logger'); | ||
var getErrorCondition = require('./get-error-condition'); | ||
function fallbackValueDecorator(wrapper, fallbackValue, error) { | ||
var condition; | ||
error = error || Error; | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
condition = function (err, dep) { return err instanceof error; }; | ||
} | ||
else { | ||
condition = error; | ||
} | ||
var condition = getErrorCondition(error); | ||
return wrapper(function (func) { | ||
@@ -13,0 +8,0 @@ return function () { |
var defaultLogger = require('../utils/default-logger'); | ||
var getErrorCondition = require('./get-error-condition'); | ||
function fallbackDecorator(wrapper, fallbackFunction, error) { | ||
var condition; | ||
error = error || Error; | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
condition = function (err, dep) { return err instanceof error; }; | ||
} | ||
else { | ||
condition = error; | ||
} | ||
var condition = getErrorCondition(error); | ||
return wrapper(function (func) { | ||
@@ -13,0 +8,0 @@ return function () { |
var defaultLogger = require('../utils/default-logger'); | ||
var getErrorCondition = require('./get-error-condition'); | ||
function retryDecorator(wrapper, times, interval, error) { | ||
var condition; | ||
times = times || Infinity; | ||
error = error || Error; | ||
interval = interval || 0; | ||
@@ -12,8 +11,3 @@ var intervalFunc = typeof interval === 'function' ? | ||
if (error === Error || Error.isPrototypeOf(error)) { | ||
condition = function (err, dep) { return err instanceof error; }; | ||
} | ||
else { | ||
condition = error; | ||
} | ||
var condition = getErrorCondition(error); | ||
@@ -20,0 +14,0 @@ return wrapper(function (func) { |
@@ -30,2 +30,59 @@ var assert = require('chai').assert; | ||
it('must cache', function (done) { | ||
var f = cached(function (a, b, c, next) { | ||
next(undefined, Math.random()); | ||
}); | ||
f(1, 2, 3, function (err, res1) { | ||
f(3, 2, 1, function (err, res2) { | ||
assert.equal(res1, res2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('must not cache (error)', function (done) { | ||
var counter = 0; | ||
var f = cached(function (a, b, c, next) { | ||
counter++; | ||
if (counter % 2 !== 0) { | ||
return next(new Error(), 'error'); | ||
} | ||
return next(undefined, 'ok'); | ||
}); | ||
f(1, 2, 3, function (err, res1) { | ||
f(3, 2, 1, function (err, res2) { | ||
assert.equal(res1, 'error'); | ||
assert.equal(res2, 'ok'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('must cache (error)', function (done) { | ||
var cache = new Cache({key: function (a, b, c) { | ||
return a + b + c; | ||
}}); | ||
var cached = cacheDecorator(cache, { error: function (err, res) { | ||
return !(err instanceof Error); | ||
} }); | ||
var counter = 0; | ||
var f = cached(function (a, b, c, next) { | ||
counter++; | ||
if (counter % 2 !== 0) { | ||
return next(new Error(), 'error'); | ||
} | ||
return next(undefined, 'ok'); | ||
}); | ||
f(1, 2, 3, function (err, res1) { | ||
f(3, 2, 1, function (err, res2) { | ||
assert.equal(res1, res2); | ||
assert.equal(res1, 'error'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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
137956
118
3704
690