UNPKG

984 BPlain TextView Raw
1import { IPluggableModule } from '@testring/types';
2import { Hook } from './hook';
3
4type HookDescriptor = string;
5
6export class PluggableModule implements IPluggableModule<Hook> {
7
8 private pluginHooks: Map<string, Hook> = new Map();
9
10 constructor(hooks: Array<HookDescriptor> = []) {
11 this.createHooks(hooks);
12 }
13
14 private createHooks(hooks: Array<HookDescriptor> = []) {
15 let hookName;
16
17 for (let index = 0; index < hooks.length; index++) {
18 hookName = hooks[index];
19
20 this.pluginHooks.set(hookName, new Hook());
21 }
22 }
23
24 protected async callHook<T = any>(name: string, ...args): Promise<T> {
25 const pluginHook = this.pluginHooks.get(name);
26
27 if (pluginHook === undefined) {
28 throw new ReferenceError(`There is no plugin called ${name}.`);
29 }
30
31 return pluginHook.callHooks(...args);
32 }
33
34 public getHook(name: string) {
35 return this.pluginHooks.get(name);
36 }
37}