diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-22 15:40:13 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 93cae47925e4dd68b7d34a41927b2740b4fab1b4 (patch) | |
tree | f649ab49fab1886b434e164591990cc99b234466 /client/src/app/core/plugins/hooks.service.ts | |
parent | 587568e1cc0e33c023c1ac62dd28fef313285250 (diff) | |
download | PeerTube-93cae47925e4dd68b7d34a41927b2740b4fab1b4.tar.gz PeerTube-93cae47925e4dd68b7d34a41927b2740b4fab1b4.tar.zst PeerTube-93cae47925e4dd68b7d34a41927b2740b4fab1b4.zip |
Add client hooks
Diffstat (limited to 'client/src/app/core/plugins/hooks.service.ts')
-rw-r--r-- | client/src/app/core/plugins/hooks.service.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/client/src/app/core/plugins/hooks.service.ts b/client/src/app/core/plugins/hooks.service.ts new file mode 100644 index 000000000..80c57869c --- /dev/null +++ b/client/src/app/core/plugins/hooks.service.ts | |||
@@ -0,0 +1,44 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { PluginService } from '@app/core/plugins/plugin.service' | ||
3 | import { ClientActionHookName, ClientFilterHookName } from '@shared/models/plugins/client-hook.model' | ||
4 | import { from, Observable } from 'rxjs' | ||
5 | import { mergeMap, switchMap } from 'rxjs/operators' | ||
6 | import { ServerService } from '@app/core/server' | ||
7 | import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' | ||
8 | |||
9 | type RawFunction<U, T> = (params: U) => T | ||
10 | type ObservableFunction<U, T> = RawFunction<U, Observable<T>> | ||
11 | |||
12 | @Injectable() | ||
13 | export class HooksService { | ||
14 | constructor ( | ||
15 | private server: ServerService, | ||
16 | private pluginService: PluginService | ||
17 | ) { } | ||
18 | |||
19 | wrapObject<T, U extends ClientFilterHookName> (result: T, hookName: U) { | ||
20 | return this.pluginService.runHook(hookName, result) | ||
21 | } | ||
22 | |||
23 | wrapObsFun | ||
24 | <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName> | ||
25 | (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { | ||
26 | return from(this.pluginService.ensurePluginsAreLoaded(scope)) | ||
27 | .pipe( | ||
28 | mergeMap(() => this.wrapObject(params, hookParamName)), | ||
29 | switchMap(params => fun(params)), | ||
30 | mergeMap(result => this.pluginService.runHook(hookResultName, result, params)) | ||
31 | ) | ||
32 | } | ||
33 | |||
34 | async wrapFun<U, T, V extends ClientFilterHookName> (fun: RawFunction<U, T>, params: U, hookName: V) { | ||
35 | const result = fun(params) | ||
36 | |||
37 | return this.pluginService.runHook(hookName, result, params) | ||
38 | } | ||
39 | |||
40 | runAction<T, U extends ClientActionHookName> (hookName: U, params?: T) { | ||
41 | this.pluginService.runHook(hookName, params) | ||
42 | .catch((err: any) => console.error('Fatal hook error.', { err })) | ||
43 | } | ||
44 | } | ||