-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·37 lines (31 loc) · 1.15 KB
/
index.js
File metadata and controls
executable file
·37 lines (31 loc) · 1.15 KB
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
module.exports = (originalFn, reporter, timer) => {
if(typeof reporter !== 'function') {
reporter = (duration) => console.log('Function call duration(' + originalFn.name + '): ' + duration);
}
if(typeof timer !== 'function') {
timer = Date.now;
}
return function () {
const hasCallback = (typeof arguments[arguments.length - 1] === 'function');
const context = this;
const startTime = timer();
const report = () => reporter(timer() - startTime);
if (hasCallback) {
const originalCallback = arguments[arguments.length - 1];
arguments[arguments.length - 1] = function () {
report();
originalCallback.apply(null, arguments);
}
}
const result = originalFn.apply(context, arguments);
if (!hasCallback) {
if (isThenable(result)) {
result.then(report, report);
} else {
report();
}
}
return result;
}
}
const isThenable = value => !!value && typeof value.then === 'function';