X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fplugins%2Fplugin.service.ts;h=1294edd7d2375987299f7a63de4e32548795abbc;hb=a1758df8a3c3f866460edd8f9bbc94e8dd41fd80;hp=14310f093670b053039a68d33ee3c68ae990d5f7;hpb=93cae47925e4dd68b7d34a41927b2740b4fab1b4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index 14310f093..1294edd7d 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts @@ -3,15 +3,16 @@ import { Router } from '@angular/router' import { ServerConfigPlugin } from '@shared/models' import { ServerService } from '@app/core/server/server.service' import { ClientScript } from '@shared/models/plugins/plugin-package-json.model' +import { ClientScript as ClientScriptModule } from '../../../types/client-script.model' import { environment } from '../../../environments/environment' -import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model' import { ReplaySubject } from 'rxjs' import { first, shareReplay } from 'rxjs/operators' import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' -import { ClientHook, ClientHookName } from '@shared/models/plugins/client-hook.model' +import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model' import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' +import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model' -interface HookStructValue extends RegisterHookOptions { +interface HookStructValue extends RegisterClientHookOptions { plugin: ServerConfigPlugin clientScript: ClientScript } @@ -28,6 +29,7 @@ export class PluginService implements ClientHook { pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject } = { common: new ReplaySubject(1), + search: new ReplaySubject(1), 'video-watch': new ReplaySubject(1) } @@ -35,6 +37,7 @@ export class PluginService implements ClientHook { private scopes: { [ scopeName: string ]: PluginInfo[] } = {} private loadedScripts: { [ script: string ]: boolean } = {} private loadedScopes: PluginClientScope[] = [] + private loadingScopes: { [id in PluginClientScope]?: boolean } = {} private hooks: { [ name: string ]: HookStructValue[] } = {} @@ -62,6 +65,8 @@ export class PluginService implements ClientHook { } ensurePluginsAreLoaded (scope: PluginClientScope) { + this.loadPluginsByScope(scope) + return this.pluginsLoaded[scope].asObservable() .pipe(first(), shareReplay()) .toPromise() @@ -103,13 +108,23 @@ export class PluginService implements ClientHook { } async loadPluginsByScope (scope: PluginClientScope, isReload = false) { + if (this.loadingScopes[scope]) return + if (!isReload && this.loadedScopes.includes(scope)) return + + this.loadingScopes[scope] = true + try { await this.ensurePluginsAreBuilt() if (!isReload) this.loadedScopes.push(scope) const toLoad = this.scopes[ scope ] - if (!Array.isArray(toLoad)) return + if (!Array.isArray(toLoad)) { + this.loadingScopes[scope] = false + this.pluginsLoaded[scope].next(true) + + return + } const promises: Promise[] = [] for (const pluginInfo of toLoad) { @@ -125,6 +140,7 @@ export class PluginService implements ClientHook { await Promise.all(promises) this.pluginsLoaded[scope].next(true) + this.loadingScopes[scope] = false } catch (err) { console.error('Cannot load plugins by scope %s.', scope, err) } @@ -149,7 +165,12 @@ export class PluginService implements ClientHook { private loadPlugin (pluginInfo: PluginInfo) { const { plugin, clientScript } = pluginInfo - const registerHook = (options: RegisterHookOptions) => { + const registerHook = (options: RegisterClientHookOptions) => { + if (clientHookObject[options.target] !== true) { + console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name) + return + } + if (!this.hooks[options.target]) this.hooks[options.target] = [] this.hooks[options.target].push({ @@ -166,7 +187,7 @@ export class PluginService implements ClientHook { console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) return import(/* webpackIgnore: true */ clientScript.script) - .then(script => script.register({ registerHook, peertubeHelpers })) + .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers })) .then(() => this.sortHooksByPriority()) }