async-deco
Advanced tools
Comparing version 7.2.1 to 7.3.0
var _log = require('../src/log'); | ||
var wrapper = require('../src/noop'); | ||
function log() { | ||
return _log(wrapper); | ||
function log(prefix) { | ||
return _log(wrapper, prefix); | ||
} | ||
module.exports = log; |
{ | ||
"name": "async-deco", | ||
"version": "7.2.1", | ||
"version": "7.3.0", | ||
"description": "A collection of decorators for adding features to asynchronous functions (callback or promise based).", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var _log = require('../src/log'); | ||
var wrapper = require('../src/promise-translator'); | ||
function log() { | ||
return _log(wrapper); | ||
function log(prefix) { | ||
return _log(wrapper, prefix); | ||
} | ||
module.exports = log; |
@@ -164,3 +164,19 @@ async-deco | ||
``` | ||
When using multiple decorator, it can be useful to attach this decorator multiple times, to give an insight about when the original function starts/ends and when the decorated function is called. To tell what log is called you can add a prefix to the logs. For example: | ||
```js | ||
var logDecorator = require('async-deco/callback/log'); | ||
var addLogsToInnerFunction = logDecorator('inner-'); | ||
var addLogsToOuterFunction = logDecorator('outer-'); | ||
var cached = cacheDecorator(cache); // caching decorator | ||
var myfunc = | ||
addLogsToOuterFunction( | ||
cached( | ||
addLogsToInnerFunction( | ||
function (..., cb) { .... })); | ||
``` | ||
In this example outer-log-start outer-log-end (or outer-log-error) will be always called. The inner logs only in case of cache miss. | ||
Memoize | ||
@@ -167,0 +183,0 @@ ------- |
var defaultLogger = require('../utils/default-logger'); | ||
function logDecorator(wrapper) { | ||
function logDecorator(wrapper, prefix) { | ||
prefix = prefix || ''; | ||
return wrapper(function (func) { | ||
@@ -13,3 +14,3 @@ return function () { | ||
if (err) { | ||
logger('log-error', { | ||
logger(prefix + 'log-error', { | ||
err: err | ||
@@ -19,3 +20,3 @@ }); | ||
else { | ||
logger('log-end', { | ||
logger(prefix + 'log-end', { | ||
result: dep | ||
@@ -26,3 +27,3 @@ }); | ||
}; | ||
logger('log-start', {args: args.slice(0, -1), context: context}); | ||
logger(prefix + 'log-start', {args: args.slice(0, -1), context: context}); | ||
func.apply(context, args); | ||
@@ -29,0 +30,0 @@ }; |
@@ -24,2 +24,17 @@ var assert = require('chai').assert; | ||
it('must log success (use prefix)', function (done) { | ||
wrapped = logDecorator('prefix-'); | ||
var f = addLog(wrapped(function (a, b, c, next) { | ||
next(undefined, a + b + c); | ||
})); | ||
f(1, 2, 3, function (err, dep) { | ||
assert.equal(dep, 6); | ||
assert.deepEqual(log, [ | ||
{type: 'prefix-log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}, key: 'key'}, | ||
{type: 'prefix-log-end', obj: {result: 6}, key: 'key'} | ||
]); | ||
done(); | ||
}); | ||
}); | ||
it('must log success', function (done) { | ||
@@ -26,0 +41,0 @@ var f = addLog(wrapped(function (a, b, c, next) { |
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
139277
3719
706