]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/plugins/hooks.service.ts
Add ability to update icons content
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / hooks.service.ts
CommitLineData
93cae479
C
1import { Injectable } from '@angular/core'
2import { PluginService } from '@app/core/plugins/plugin.service'
3import { ClientActionHookName, ClientFilterHookName } from '@shared/models/plugins/client-hook.model'
4import { from, Observable } from 'rxjs'
5import { mergeMap, switchMap } from 'rxjs/operators'
6import { ServerService } from '@app/core/server'
7import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type'
8
9type RawFunction<U, T> = (params: U) => T
10type ObservableFunction<U, T> = RawFunction<U, Observable<T>>
11
12@Injectable()
13export class HooksService {
14 constructor (
15 private server: ServerService,
16 private pluginService: PluginService
17 ) { }
18
93cae479
C
19 wrapObsFun
20 <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
c2023a9f
C
21 (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2)
22 {
93cae479
C
23 return from(this.pluginService.ensurePluginsAreLoaded(scope))
24 .pipe(
25 mergeMap(() => this.wrapObject(params, hookParamName)),
26 switchMap(params => fun(params)),
27 mergeMap(result => this.pluginService.runHook(hookResultName, result, params))
28 )
29 }
30
c2023a9f
C
31 async wrapFun
32 <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
33 (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2)
34 {
35 await this.pluginService.ensurePluginsAreLoaded(scope)
36
37 const newParams = await this.wrapObject(params, hookParamName)
38 const result = fun(newParams)
93cae479 39
c2023a9f 40 return this.pluginService.runHook(hookResultName, result, params)
93cae479
C
41 }
42
c9e3eeed
C
43 runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) {
44 this.pluginService.ensurePluginsAreLoaded(scope)
23bdacf8 45 .then(() => this.pluginService.runHook(hookName, undefined, params))
c9e3eeed 46 .catch((err: any) => console.error('Fatal hook error.', { err }))
93cae479 47 }
c2023a9f
C
48
49 private wrapObject<T, U extends ClientFilterHookName> (result: T, hookName: U) {
50 return this.pluginService.runHook(hookName, result)
51 }
93cae479 52}