]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/plugins/plugin.service.ts
Change plugin models names
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
index 14310f093670b053039a68d33ee3c68ae990d5f7..e4a73de81d2de717f0823b3a60ff7c3a56025bc1 100644 (file)
@@ -4,14 +4,14 @@ import { ServerConfigPlugin } from '@shared/models'
 import { ServerService } from '@app/core/server/server.service'
 import { ClientScript } from '@shared/models/plugins/plugin-package-json.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 +28,7 @@ export class PluginService implements ClientHook {
 
   pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
     common: new ReplaySubject<boolean>(1),
+    search: new ReplaySubject<boolean>(1),
     'video-watch': new ReplaySubject<boolean>(1)
   }
 
@@ -35,6 +36,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 +64,8 @@ export class PluginService implements ClientHook {
   }
 
   ensurePluginsAreLoaded (scope: PluginClientScope) {
+    this.loadPluginsByScope(scope)
+
     return this.pluginsLoaded[scope].asObservable()
                .pipe(first(), shareReplay())
                .toPromise()
@@ -103,13 +107,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<any>[] = []
       for (const pluginInfo of toLoad) {
@@ -125,6 +139,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 +164,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({