aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/plugins/hooks.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/core-utils/plugins/hooks.ts')
-rw-r--r--shared/core-utils/plugins/hooks.ts35
1 files changed, 20 insertions, 15 deletions
diff --git a/shared/core-utils/plugins/hooks.ts b/shared/core-utils/plugins/hooks.ts
index 047c04f7b..60f4130f5 100644
--- a/shared/core-utils/plugins/hooks.ts
+++ b/shared/core-utils/plugins/hooks.ts
@@ -8,25 +8,30 @@ function getHookType (hookName: string) {
8 return HookType.STATIC 8 return HookType.STATIC
9} 9}
10 10
11async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { 11async function internalRunHook <T>(handler: Function, hookType: HookType, result: T, params: any, onError: (err: Error) => void) {
12 let result = param
13
14 try { 12 try {
15 const p = handler(result) 13 if (hookType === HookType.FILTER) {
14 const p = handler(result, params)
15
16 if (isPromise(p)) result = await p
17 else result = p
18
19 return result
20 }
16 21
17 switch (hookType) { 22 // Action/static hooks do not have result value
18 case HookType.FILTER: 23 const p = handler(params)
19 if (isPromise(p)) result = await p 24
20 else result = p 25 if (hookType === HookType.STATIC) {
21 break 26 if (isPromise(p)) await p
27
28 return undefined
29 }
22 30
23 case HookType.STATIC: 31 if (hookType === HookType.ACTION) {
24 if (isPromise(p)) await p 32 if (isCatchable(p)) p.catch(err => onError(err))
25 break
26 33
27 case HookType.ACTION: 34 return undefined
28 if (isCatchable(p)) p.catch(err => onError(err))
29 break
30 } 35 }
31 } catch (err) { 36 } catch (err) {
32 onError(err) 37 onError(err)