diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-19 17:30:41 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 89cd12756035a146bbcc4db78cd3cd64f2f3d88d (patch) | |
tree | 896cd9fca1e0baa969b1f7a5b398ec1602761661 /shared/core-utils | |
parent | 09071200c73f5358e1d0bfb61a274e4f2c4ec52b (diff) | |
download | PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.tar.gz PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.tar.zst PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.zip |
Add hook filters tests
Diffstat (limited to 'shared/core-utils')
-rw-r--r-- | shared/core-utils/plugins/hooks.ts | 35 |
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 | ||
11 | async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { | 11 | async 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) |