UNPKG

505 BJavaScriptView Raw
1class Hooks {
2 constructor() {
3 this.hooks = new Map()
4 }
5
6 add(name, handler) {
7 if (!handler) return
8 if (!this.hooks.has(name)) {
9 this.hooks.set(name, new Set())
10 }
11 const hooks = this.hooks.get(name)
12 hooks.add(handler)
13 return this
14 }
15
16 async run(name, context) {
17 if (!this.hooks.has(name)) return
18 for (const hook of this.hooks.get(name)) {
19 // eslint-disable-next-line no-await-in-loop
20 await hook(context)
21 }
22 }
23}
24
25module.exports = new Hooks()