Comparing version 2.1.0 to 2.2.0
@@ -6,3 +6,4 @@ 'use strict'; | ||
}); | ||
exports.middleware = exports.callback = exports.promise = exports.sync = exports.isWrapped = undefined; | ||
exports.middleware = exports.timeCallback = exports.callback = exports.timePromise = exports.promise = exports.timeSync = exports.sync = exports.isWrapped = undefined; | ||
exports.createDecorator = createDecorator; | ||
@@ -53,2 +54,24 @@ var _debug = require('debug'); | ||
function createDecorator(type) { | ||
var printer = arguments.length <= 1 || arguments[1] === undefined ? defaultPrinter : arguments[1]; | ||
return function decorate(namespace) { | ||
return function decorator(target, key, descriptor) { | ||
if (typeof target === 'function') { | ||
debug('cannot wrap class, skipping'); | ||
return target; | ||
} | ||
if (typeof target.value !== 'function') { | ||
debug('cannot wrap non-function, skipping'); | ||
return descriptor; | ||
} | ||
descriptor.value = type(namespace, descriptor.value, printer); | ||
return descriptor; | ||
}; | ||
}; | ||
} | ||
// wraps all our handler function to take care of some common patterns | ||
@@ -136,2 +159,4 @@ function wrap(handler) { | ||
var timeSync = exports.timeSync = createDecorator(sync); | ||
/** | ||
@@ -171,2 +196,4 @@ * Wrap a promise returning function | ||
var timePromise = exports.timePromise = createDecorator(sync); | ||
/** | ||
@@ -213,2 +240,4 @@ * Wrap a node-style callback function | ||
var timeCallback = exports.timeCallback = createDecorator(sync); | ||
/** | ||
@@ -215,0 +244,0 @@ * Wrap an express middleware function |
{ | ||
"name": "derf", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A javascript performance debugger.", | ||
@@ -31,3 +31,4 @@ "main": "lib/index.js", | ||
"nanoseconds", | ||
"log" | ||
"log", | ||
"decorator" | ||
], | ||
@@ -52,2 +53,3 @@ "author": { | ||
"babel-eslint": "^6.0.4", | ||
"babel-plugin-transform-decorators-legacy": "^1.3.4", | ||
"babel-preset-es2015": "^6.3.13", | ||
@@ -54,0 +56,0 @@ "babel-preset-stage-1": "^6.3.13", |
@@ -100,3 +100,3 @@ # derf | ||
* `debug` - _function_. the debug instance. | ||
* `time` - _number_. the time in nanoseconds _array_. the function to to run. | ||
* `time` - _number_. the time in nanoseconds it took the function to to run. | ||
* `args` - _array_. the arguments the function was called with. | ||
@@ -119,2 +119,45 @@ * `retArgs` - _array_. the error/value the function was resolved with. | ||
### Decorators | ||
In addition to exporting the standard wrapping functions, derf also provides | ||
functions that work with the experimental decorator syntax. | ||
```js | ||
import { timeSync, timePromise, timeCallback } from 'derf'; | ||
import createDebug from 'debug'; | ||
const debug = createDebug('test'); | ||
export default class TimedClass { | ||
@timeSync('test') | ||
sync(val) { | ||
return val; | ||
} | ||
@timePromise(debug) | ||
promise(val) { | ||
return Promise.resolve(val); | ||
} | ||
@timeCallback('test') | ||
callback(val, cb) { | ||
setTimeout(cb, 0, val); | ||
} | ||
} | ||
``` | ||
You can create decorators with custom logging logic by importing the `createDecorator` function. | ||
```js | ||
import { createDecorator, callback as callbackWrapper } from 'derf'; | ||
const myDecorator = createDecorator( | ||
callbackWrapper, | ||
function simplePrinter(debug, time, callArgs, retArgs) { | ||
debug('it\'s done'); | ||
} | ||
); | ||
``` | ||
### Caveats | ||
@@ -121,0 +164,0 @@ |
16135
233
171
15