]>
Commit | Line | Data |
---|---|---|
b4055e1c C |
1 | import { HookType } from '../../models/plugins/hook-type.enum' |
2 | import { isCatchable, isPromise } from '../miscs/miscs' | |
3 | ||
4 | function getHookType (hookName: string) { | |
5 | if (hookName.startsWith('filter:')) return HookType.FILTER | |
6 | if (hookName.startsWith('action:')) return HookType.ACTION | |
7 | ||
8 | return HookType.STATIC | |
9 | } | |
10 | ||
11 | async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { | |
12 | let result = param | |
13 | ||
14 | try { | |
15 | const p = handler(result) | |
16 | ||
17 | switch (hookType) { | |
18 | case HookType.FILTER: | |
19 | if (isPromise(p)) result = await p | |
20 | else result = p | |
21 | break | |
22 | ||
23 | case HookType.STATIC: | |
24 | if (isPromise(p)) await p | |
25 | break | |
26 | ||
27 | case HookType.ACTION: | |
28 | if (isCatchable(p)) p.catch(err => onError(err)) | |
29 | break | |
30 | } | |
31 | } catch (err) { | |
32 | onError(err) | |
33 | } | |
34 | ||
35 | return result | |
36 | } | |
37 | ||
38 | export { | |
39 | getHookType, | |
40 | internalRunHook | |
41 | } |