]>
Commit | Line | Data |
---|---|---|
f375bb3d C |
1 | import { from, Observable } from 'rxjs' |
2 | import { mergeMap, switchMap } from 'rxjs/operators' | |
93cae479 C |
3 | import { Injectable } from '@angular/core' |
4 | import { PluginService } from '@app/core/plugins/plugin.service' | |
67ed6552 | 5 | import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models' |
faeec106 | 6 | import { AuthService, AuthStatus } from '../auth' |
93cae479 C |
7 | |
8 | type RawFunction<U, T> = (params: U) => T | |
9 | type ObservableFunction<U, T> = RawFunction<U, Observable<T>> | |
10 | ||
11 | @Injectable() | |
12 | export class HooksService { | |
faeec106 C |
13 | constructor ( |
14 | private authService: AuthService, | |
15 | private pluginService: PluginService | |
16 | ) { | |
17 | // Run auth hooks | |
18 | this.authService.userInformationLoaded | |
19 | .subscribe(() => this.runAction('action:auth-user.information-loaded', 'common', { user: this.authService.getUser() })) | |
20 | ||
21 | this.authService.loginChangedSource.subscribe(obj => { | |
22 | if (obj === AuthStatus.LoggedIn) { | |
23 | this.runAction('action:auth-user.logged-in', 'common') | |
24 | } else if (obj === AuthStatus.LoggedOut) { | |
25 | this.runAction('action:auth-user.logged-out', 'common') | |
26 | } | |
27 | }) | |
28 | } | |
93cae479 | 29 | |
9df52d66 C |
30 | wrapObsFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName> |
31 | (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { | |
93cae479 C |
32 | return from(this.pluginService.ensurePluginsAreLoaded(scope)) |
33 | .pipe( | |
0912f1b4 | 34 | mergeMap(() => this.wrapObjectWithoutScopeLoad(params, hookParamName)), |
93cae479 C |
35 | switchMap(params => fun(params)), |
36 | mergeMap(result => this.pluginService.runHook(hookResultName, result, params)) | |
37 | ) | |
38 | } | |
39 | ||
9df52d66 C |
40 | async wrapFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName> |
41 | (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { | |
c2023a9f C |
42 | await this.pluginService.ensurePluginsAreLoaded(scope) |
43 | ||
0912f1b4 | 44 | const newParams = await this.wrapObjectWithoutScopeLoad(params, hookParamName) |
c2023a9f | 45 | const result = fun(newParams) |
93cae479 | 46 | |
c2023a9f | 47 | return this.pluginService.runHook(hookResultName, result, params) |
93cae479 C |
48 | } |
49 | ||
c9e3eeed C |
50 | runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) { |
51 | this.pluginService.ensurePluginsAreLoaded(scope) | |
23bdacf8 | 52 | .then(() => this.pluginService.runHook(hookName, undefined, params)) |
c9e3eeed | 53 | .catch((err: any) => console.error('Fatal hook error.', { err })) |
93cae479 | 54 | } |
c2023a9f | 55 | |
0912f1b4 C |
56 | async wrapObject<T, U extends ClientFilterHookName> (result: T, scope: PluginClientScope, hookName: U) { |
57 | await this.pluginService.ensurePluginsAreLoaded(scope) | |
58 | ||
59 | return this.wrapObjectWithoutScopeLoad(result, hookName) | |
60 | } | |
61 | ||
62 | private wrapObjectWithoutScopeLoad<T, U extends ClientFilterHookName> (result: T, hookName: U) { | |
c2023a9f C |
63 | return this.pluginService.runHook(hookName, result) |
64 | } | |
93cae479 | 65 | } |