aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/plugins/hooks.service.ts
blob: 062083fd155db48a1749e4c4dafba77e521c2fee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { from, Observable } from 'rxjs'
import { mergeMap, switchMap } from 'rxjs/operators'
import { Injectable } from '@angular/core'
import { PluginService } from '@app/core/plugins/plugin.service'
import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models'
import { AuthService, AuthStatus } from '../auth'

type RawFunction<U, T> = (params: U) => T
type ObservableFunction<U, T> = RawFunction<U, Observable<T>>

@Injectable()
export class HooksService {
  constructor (
    private authService: AuthService,
    private pluginService: PluginService
  ) {
    // Run auth hooks
    this.authService.userInformationLoaded
      .subscribe(() => this.runAction('action:auth-user.information-loaded', 'common', { user: this.authService.getUser() }))

    this.authService.loginChangedSource.subscribe(obj => {
      if (obj === AuthStatus.LoggedIn) {
        this.runAction('action:auth-user.logged-in', 'common')
      } else if (obj === AuthStatus.LoggedOut) {
        this.runAction('action:auth-user.logged-out', 'common')
      }
    })
  }

  wrapObsFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
  (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) {
    return from(this.pluginService.ensurePluginsAreLoaded(scope))
      .pipe(
        mergeMap(() => this.wrapObjectWithoutScopeLoad(params, hookParamName)),
        switchMap(params => fun(params)),
        mergeMap(result => this.pluginService.runHook(hookResultName, result, params))
      )
  }

  async wrapFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
  (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) {
    await this.pluginService.ensurePluginsAreLoaded(scope)

    const newParams = await this.wrapObjectWithoutScopeLoad(params, hookParamName)
    const result = fun(newParams)

    return this.pluginService.runHook(hookResultName, result, params)
  }

  runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) {
    this.pluginService.ensurePluginsAreLoaded(scope)
        .then(() => this.pluginService.runHook(hookName, undefined, params))
        .catch((err: any) => console.error('Fatal hook error.', { err }))
  }

  async wrapObject<T, U extends ClientFilterHookName> (result: T, scope: PluginClientScope, hookName: U) {
    await this.pluginService.ensurePluginsAreLoaded(scope)

    return this.wrapObjectWithoutScopeLoad(result, hookName)
  }

  private wrapObjectWithoutScopeLoad<T, U extends ClientFilterHookName> (result: T, hookName: U) {
    return this.pluginService.runHook(hookName, result)
  }
}