-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook-manager.ts
50 lines (45 loc) · 1.49 KB
/
hook-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Logger } from './helpers/logger.ts';
import { HookData } from './interfaces/hook-data.interface.ts';
import { HookFilter } from './interfaces/hook-filter.interface.ts';
import { HookFunction } from './types.ts';
export class HookManager {
readonly #hooks: Array<{ filter: HookFilter; fn: HookFunction }>;
get hooks(): Array<{ filter: HookFilter; fn: HookFunction }> {
return this.#hooks;
}
constructor() {
this.#hooks = [];
}
subscribe(filter: HookFilter, fn: HookFunction): void {
this.#hooks.push({ filter, fn });
}
execute(
data: HookFilter & HookData & { kind?: 'provider' | 'consumer' },
): void {
Logger.debug('Hook call:', data);
this.#hooks
.filter((hook) =>
(hook.filter.application === '*' && data.application !== undefined) ||
hook.filter.application === data.application
)
.filter((hook) =>
(hook.filter.container === '*' && data.container !== undefined) ||
hook.filter.container === data.container
)
.filter((hook) =>
(hook.filter.scope === '*' && data.scope !== undefined) ||
hook.filter.scope === data.scope
)
.filter((hook) =>
(hook.filter.type === '*' && data.type !== undefined) ||
hook.filter.type === data.kind || hook.filter.type === data.type
)
.forEach((hook) => {
hook.fn({
application: data.application,
container: data.container,
typeData: data.typeData,
});
});
}
}