PeerTube/server/core/lib/plugins/hooks.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import Bluebird from 'bluebird'
import { ServerActionHookName, ServerFilterHookName } from '@peertube/peertube-models'
import { logger } from '../../helpers/logger.js'
import { PluginManager } from './plugin-manager.js'
2019-07-18 07:28:37 -05:00
2019-07-19 10:30:41 -05:00
type PromiseFunction <U, T> = (params: U) => Promise<T> | Bluebird<T>
type RawFunction <U, T> = (params: U) => T
2019-07-18 07:28:37 -05:00
// Helpers to run hooks
const Hooks = {
2022-08-02 08:29:00 -05:00
wrapObject: <T, U extends ServerFilterHookName>(result: T, hookName: U, context?: any) => {
return PluginManager.Instance.runHook(hookName, result, context)
2019-07-19 10:30:41 -05:00
},
wrapPromiseFun: async <U, T, V extends ServerFilterHookName>(fun: PromiseFunction<U, T>, params: U, hookName: V) => {
const result = await fun(params)
return PluginManager.Instance.runHook(hookName, result, params)
2019-07-18 07:28:37 -05:00
},
2019-07-19 10:30:41 -05:00
wrapFun: async <U, T, V extends ServerFilterHookName>(fun: RawFunction<U, T>, params: U, hookName: V) => {
const result = fun(params)
2019-07-18 07:28:37 -05:00
2019-07-19 10:30:41 -05:00
return PluginManager.Instance.runHook(hookName, result, params)
2019-07-18 07:28:37 -05:00
},
runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => {
PluginManager.Instance.runHook(hookName, undefined, params)
2019-07-18 07:28:37 -05:00
.catch(err => logger.error('Fatal hook error.', { err }))
}
}
export {
Hooks
}