@adonisjs/ace
Version:
Commandline apps framework used by AdonisJs
49 lines (48 loc) • 1.19 kB
JavaScript
;
/*
* @adonisjs/ace
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hooks = void 0;
/**
* Exposes the API to register and execute async hooks
*/
class Hooks {
constructor() {
this.hooks = {
before: new Map(),
after: new Map(),
};
}
/**
* Register hook for a given action and lifecycle
*/
add(lifecycle, action, handler) {
const handlers = this.hooks[lifecycle].get(action);
if (handlers) {
handlers.push(handler);
}
else {
this.hooks[lifecycle].set(action, [handler]);
}
return this;
}
/**
* Execute hooks for a given action and lifecycle
*/
async execute(lifecycle, action, data) {
const handlers = this.hooks[lifecycle].get(action);
if (!handlers) {
return;
}
for (let handler of handlers) {
await handler(data);
}
}
}
exports.Hooks = Hooks;