-
Notifications
You must be signed in to change notification settings - Fork 4
/
Space.js
63 lines (53 loc) · 1.63 KB
/
Space.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module.exports = function Space(key, reporters, errback) {
function forEachReporter(func) {
reporters.forEach(function(reporter) {
try {
func(reporter);
}
catch (e) {
var errMsg = e && e.message ? e.message : 'Error in reporter';
(typeof errback === 'function' ? errback : console.log)(errMsg);
}
});
}
this.value = function (val) {
forEachReporter(reporter => reporter.value(key, val));
};
this.increment = function (val = 1) {
forEachReporter(reporter => reporter.increment(key, val));
};
this.meter = function(func) {
if(typeof func !== 'function') {
throw new Error('must pass a function as argument');
}
return function() {
var args = Array.prototype.slice.call(arguments);
var start = new Date();
if(isAsyncFunc(args)) {
var callback = args.pop();
args.push(function() {
var callbackArgs = Array.prototype.slice.call(arguments);
var finish = new Date();
report(key, start, finish);
callback.apply(this, callbackArgs);
});
func.apply(this, args);
} else {
var result = func.apply(this, args);
var finish = new Date();
report(key, start, finish);
return result;
}
};
};
this.space = function(nextKey) {
return new Space(`${key}.${nextKey}`, reporters, errback);
};
function report(key, start, finish) {
var duration = finish.getTime() - start.getTime();
forEachReporter(reporter => reporter.report(key, duration));
}
};
function isAsyncFunc(args) {
return typeof args[args.length - 1] === 'function';
}