-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwrapper.js
More file actions
37 lines (34 loc) · 924 Bytes
/
wrapper.js
File metadata and controls
37 lines (34 loc) · 924 Bytes
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
'use strict';
const wrapFunction = (key, fn) => {
console.log(`Wrap function: ${key}`);
return (...args) => {
console.log(`Called wrapper for: ${key}`);
console.dir({ args });
if (args.length > 0) {
let callback = args[args.length - 1];
if (typeof callback === 'function') {
args[args.length - 1] = (...args) => {
console.log(`Callback: ${key}`);
callback(...args);
};
} else {
callback = null;
}
}
console.log(`Call: ${key}`);
console.dir(args);
const result = fn(...args);
console.log(`Ended wrapper for: ${key}`);
console.dir({ result });
return result;
};
};
const cloneInterface = (anInterface) => {
const clone = {};
for (const key in anInterface) {
const fn = anInterface[key];
clone[key] = wrapFunction(key, fn);
}
return clone;
};
module.exports = { cloneInterface, wrapFunction };