UNPKG

467 BJavaScriptView Raw
1module.exports = class Hooks {
2 constructor() {
3 this.hooks = new Map()
4 }
5
6 add(name, fn) {
7 const hooks = this.get(name)
8 hooks.add(fn)
9 this.hooks.set(name, hooks)
10 }
11
12 get(name) {
13 return this.hooks.get(name) || new Set()
14 }
15
16 invoke(name, ...args) {
17 for (const hook of this.get(name)) {
18 hook(...args)
19 }
20 }
21
22 async invokePromise(name, ...args) {
23 for (const hook of this.get(name)) {
24 await hook(...args)
25 }
26 }
27}