]>
Commit | Line | Data |
---|---|---|
1 | import { from, Observable } from 'rxjs' | |
2 | import { mergeMap, switchMap } from 'rxjs/operators' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { PluginService } from '@app/core/plugins/plugin.service' | |
5 | import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models' | |
6 | import { AuthService, AuthStatus } from '../auth' | |
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 { | |
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 | } | |
29 | ||
30 | wrapObsFun | |
31 | <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName> | |
32 | (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { | |
33 | return from(this.pluginService.ensurePluginsAreLoaded(scope)) | |
34 | .pipe( | |
35 | mergeMap(() => this.wrapObjectWithoutScopeLoad(params, hookParamName)), | |
36 | switchMap(params => fun(params)), | |
37 | mergeMap(result => this.pluginService.runHook(hookResultName, result, params)) | |
38 | ) | |
39 | } | |
40 | ||
41 | async wrapFun | |
42 | <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName> | |
43 | (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { | |
44 | await this.pluginService.ensurePluginsAreLoaded(scope) | |
45 | ||
46 | const newParams = await this.wrapObjectWithoutScopeLoad(params, hookParamName) | |
47 | const result = fun(newParams) | |
48 | ||
49 | return this.pluginService.runHook(hookResultName, result, params) | |
50 | } | |
51 | ||
52 | runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) { | |
53 | this.pluginService.ensurePluginsAreLoaded(scope) | |
54 | .then(() => this.pluginService.runHook(hookName, undefined, params)) | |
55 | .catch((err: any) => console.error('Fatal hook error.', { err })) | |
56 | } | |
57 | ||
58 | async wrapObject<T, U extends ClientFilterHookName> (result: T, scope: PluginClientScope, hookName: U) { | |
59 | await this.pluginService.ensurePluginsAreLoaded(scope) | |
60 | ||
61 | return this.wrapObjectWithoutScopeLoad(result, hookName) | |
62 | } | |
63 | ||
64 | private wrapObjectWithoutScopeLoad<T, U extends ClientFilterHookName> (result: T, hookName: U) { | |
65 | return this.pluginService.runHook(hookName, result) | |
66 | } | |
67 | } |