UNPKG

1.19 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/ace
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.Hooks = void 0;
12/**
13 * Exposes the API to register and execute async hooks
14 */
15class Hooks {
16 constructor() {
17 this.hooks = {
18 before: new Map(),
19 after: new Map(),
20 };
21 }
22 /**
23 * Register hook for a given action and lifecycle
24 */
25 add(lifecycle, action, handler) {
26 const handlers = this.hooks[lifecycle].get(action);
27 if (handlers) {
28 handlers.push(handler);
29 }
30 else {
31 this.hooks[lifecycle].set(action, [handler]);
32 }
33 return this;
34 }
35 /**
36 * Execute hooks for a given action and lifecycle
37 */
38 async execute(lifecycle, action, data) {
39 const handlers = this.hooks[lifecycle].get(action);
40 if (!handlers) {
41 return;
42 }
43 for (let handler of handlers) {
44 await handler(data);
45 }
46 }
47}
48exports.Hooks = Hooks;