1 | import { RealmType, Realm } from "./realm";
|
2 | import * as awsHooks from './hook-aws';
|
3 | import * as gcpHooks from './hook-gcp';
|
4 |
|
5 |
|
6 | type HookFn = (realm: Realm, ...args: any[]) => Promise<any>;
|
7 | type HookName = 'dpush_prep' | 'dpush_image_ex' | 'realm_init' | 'realm_set_begin' | 'realm_check';
|
8 |
|
9 | const hooksDic: Map<RealmType, Map<HookName, HookFn>> = new Map();
|
10 |
|
11 | type FnByHookName = { [name: string]: HookFn };
|
12 |
|
13 |
|
14 | const hookObjByType = {
|
15 | 'aws': awsHooks as FnByHookName,
|
16 | 'gcp': gcpHooks as FnByHookName
|
17 | };
|
18 |
|
19 | for (const k of Object.keys(hookObjByType)) {
|
20 | const type = k as 'aws' | 'gcp';
|
21 | const fnByName = hookObjByType[type];
|
22 | for (const key in fnByName) {
|
23 |
|
24 | const name = key as HookName;
|
25 | const fn = fnByName[name] as HookFn;
|
26 | registerHook(type, name, fn);
|
27 | }
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 | export async function callHook(realm: Realm, name: HookName, ...args: any[]) {
|
33 | const fn = getHook(realm.type, name);
|
34 | if (fn) {
|
35 | return fn(realm, ...args);
|
36 | }
|
37 | }
|
38 |
|
39 | function getHook(type: RealmType, name: HookName): HookFn | undefined {
|
40 | const fnDic = hooksDic.get(type);
|
41 | return (fnDic) ? fnDic.get(name) : undefined;
|
42 | }
|
43 |
|
44 | function registerHook(type: RealmType, name: HookName, fn: HookFn) {
|
45 | let fnDic = hooksDic.get(type);
|
46 | if (fnDic == null) {
|
47 | fnDic = new Map<HookName, HookFn>();
|
48 | hooksDic.set(type, fnDic);
|
49 | }
|
50 | fnDic.set(name, fn);
|
51 | }
|
52 |
|