const common = require('./../common');
module.exports = class Hook {
constructor(ctx, next) {
this.hooks = {};
}
get(name) {
Iif (arguments.length === 0) {
return this.hooks;
} else {
for (let key in this.hooks) {
Eif (name === key) {
return this.hooks[name];
}
}
}
}
add(name, fn) {
Iif (this.hooks[name]) {
common.log(`The "${name}" hook already exists. repeat adding will cover before`, 'info');
}
Iif (typeof fn !== 'function') {
throw new Error(`The "${name}" hook only support function`);
return;
}
this.hooks[name] = fn;
}
run(name, ...args) {
const fn = this.hooks[name];
if (!fn) {
return;
}
return fn(...args);
}
}
|