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 = (params: U) => T type ObservableFunction = RawFunction> @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
(fun: ObservableFunction
, 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
(fun: RawFunction
, 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