X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fplugins%2Fhooks.service.ts;h=ec47aa48c562fa621857c3b608e07edea5481dd1;hb=7294aab0c879ef96c0fde15c389a2c4c1463d3c7;hp=80c57869c0008a507e69aaf861b5095e371b31b8;hpb=93cae47925e4dd68b7d34a41927b2740b4fab1b4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/plugins/hooks.service.ts b/client/src/app/core/plugins/hooks.service.ts index 80c57869c..ec47aa48c 100644 --- a/client/src/app/core/plugins/hooks.service.ts +++ b/client/src/app/core/plugins/hooks.service.ts @@ -1,44 +1,51 @@ -import { Injectable } from '@angular/core' -import { PluginService } from '@app/core/plugins/plugin.service' -import { ClientActionHookName, ClientFilterHookName } from '@shared/models/plugins/client-hook.model' import { from, Observable } from 'rxjs' import { mergeMap, switchMap } from 'rxjs/operators' -import { ServerService } from '@app/core/server' -import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' +import { Injectable } from '@angular/core' +import { PluginService } from '@app/core/plugins/plugin.service' +import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models' type RawFunction = (params: U) => T type ObservableFunction = RawFunction> @Injectable() export class HooksService { - constructor ( - private server: ServerService, - private pluginService: PluginService - ) { } - - wrapObject (result: T, hookName: U) { - return this.pluginService.runHook(hookName, result) - } + constructor (private pluginService: PluginService) { } wrapObsFun (fun: ObservableFunction, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { return from(this.pluginService.ensurePluginsAreLoaded(scope)) .pipe( - mergeMap(() => this.wrapObject(params, hookParamName)), + mergeMap(() => this.wrapObjectWithoutScopeLoad(params, hookParamName)), switchMap(params => fun(params)), mergeMap(result => this.pluginService.runHook(hookResultName, result, params)) ) } - async wrapFun (fun: RawFunction, params: U, hookName: V) { - const result = fun(params) + async wrapFun + + (fun: RawFunction, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { + await this.pluginService.ensurePluginsAreLoaded(scope) - return this.pluginService.runHook(hookName, result, params) + const newParams = await this.wrapObjectWithoutScopeLoad(params, hookParamName) + const result = fun(newParams) + + return this.pluginService.runHook(hookResultName, result, params) + } + + runAction (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 })) } - runAction (hookName: U, params?: T) { - this.pluginService.runHook(hookName, params) - .catch((err: any) => console.error('Fatal hook error.', { err })) + async wrapObject (result: T, scope: PluginClientScope, hookName: U) { + await this.pluginService.ensurePluginsAreLoaded(scope) + + return this.wrapObjectWithoutScopeLoad(result, hookName) + } + + private wrapObjectWithoutScopeLoad (result: T, hookName: U) { + return this.pluginService.runHook(hookName, result) } }