]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/core-utils/plugins/hooks.ts
Merge branch 'master' into release/3.3.0
[github/Chocobozzz/PeerTube.git] / shared / core-utils / plugins / hooks.ts
index 047c04f7b63a426b11c767c314399ad9c63cf683..5405e0529ad0eeab00bc08ed6cee44d84a8b5ef6 100644 (file)
@@ -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 <T> (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: any) => onError(err))
 
-      case HookType.ACTION:
-        if (isCatchable(p)) p.catch(err => onError(err))
-        break
+      return undefined
     }
   } catch (err) {
     onError(err)