From 89cd12756035a146bbcc4db78cd3cd64f2f3d88d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Jul 2019 17:30:41 +0200 Subject: Add hook filters tests --- shared/core-utils/plugins/hooks.ts | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'shared/core-utils/plugins/hooks.ts') 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) { return HookType.STATIC } -async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { - let result = param - +async function internalRunHook (handler: Function, hookType: HookType, result: T, params: any, onError: (err: Error) => void) { try { - const p = handler(result) + if (hookType === HookType.FILTER) { + const p = handler(result, params) + + if (isPromise(p)) result = await p + else result = p + + return result + } - switch (hookType) { - case HookType.FILTER: - if (isPromise(p)) result = await p - else result = p - break + // Action/static hooks do not have result value + const p = handler(params) + + if (hookType === HookType.STATIC) { + if (isPromise(p)) await p + + return undefined + } - case HookType.STATIC: - if (isPromise(p)) await p - break + if (hookType === HookType.ACTION) { + if (isCatchable(p)) p.catch(err => onError(err)) - case HookType.ACTION: - if (isCatchable(p)) p.catch(err => onError(err)) - break + return undefined } } catch (err) { onError(err) -- cgit v1.2.3