diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-23 12:16:34 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | c9e3eeedad67649d9b7aec8897b738d0ad63ec1f (patch) | |
tree | 97590f0e9d4c16c0968ba6f6ff1b1d12672f999b /client/src/app/core | |
parent | 5b77537ce54832f47931ba47dc513be2a9197f92 (diff) | |
download | PeerTube-c9e3eeedad67649d9b7aec8897b738d0ad63ec1f.tar.gz PeerTube-c9e3eeedad67649d9b7aec8897b738d0ad63ec1f.tar.zst PeerTube-c9e3eeedad67649d9b7aec8897b738d0ad63ec1f.zip |
Lazy load client script scopes
Diffstat (limited to 'client/src/app/core')
-rw-r--r-- | client/src/app/core/plugins/hooks.service.ts | 7 | ||||
-rw-r--r-- | client/src/app/core/plugins/plugin.service.ts | 10 |
2 files changed, 14 insertions, 3 deletions
diff --git a/client/src/app/core/plugins/hooks.service.ts b/client/src/app/core/plugins/hooks.service.ts index 80c57869c..257e27e6b 100644 --- a/client/src/app/core/plugins/hooks.service.ts +++ b/client/src/app/core/plugins/hooks.service.ts | |||
@@ -37,8 +37,9 @@ export class HooksService { | |||
37 | return this.pluginService.runHook(hookName, result, params) | 37 | return this.pluginService.runHook(hookName, result, params) |
38 | } | 38 | } |
39 | 39 | ||
40 | runAction<T, U extends ClientActionHookName> (hookName: U, params?: T) { | 40 | runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) { |
41 | this.pluginService.runHook(hookName, params) | 41 | this.pluginService.ensurePluginsAreLoaded(scope) |
42 | .catch((err: any) => console.error('Fatal hook error.', { err })) | 42 | .then(() => this.pluginService.runHook(hookName, params)) |
43 | .catch((err: any) => console.error('Fatal hook error.', { err })) | ||
43 | } | 44 | } |
44 | } | 45 | } |
diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index 90ebe5669..5d180e5a0 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts | |||
@@ -36,6 +36,7 @@ export class PluginService implements ClientHook { | |||
36 | private scopes: { [ scopeName: string ]: PluginInfo[] } = {} | 36 | private scopes: { [ scopeName: string ]: PluginInfo[] } = {} |
37 | private loadedScripts: { [ script: string ]: boolean } = {} | 37 | private loadedScripts: { [ script: string ]: boolean } = {} |
38 | private loadedScopes: PluginClientScope[] = [] | 38 | private loadedScopes: PluginClientScope[] = [] |
39 | private loadingScopes: { [id in PluginClientScope]?: boolean } = {} | ||
39 | 40 | ||
40 | private hooks: { [ name: string ]: HookStructValue[] } = {} | 41 | private hooks: { [ name: string ]: HookStructValue[] } = {} |
41 | 42 | ||
@@ -63,6 +64,8 @@ export class PluginService implements ClientHook { | |||
63 | } | 64 | } |
64 | 65 | ||
65 | ensurePluginsAreLoaded (scope: PluginClientScope) { | 66 | ensurePluginsAreLoaded (scope: PluginClientScope) { |
67 | this.loadPluginsByScope(scope) | ||
68 | |||
66 | return this.pluginsLoaded[scope].asObservable() | 69 | return this.pluginsLoaded[scope].asObservable() |
67 | .pipe(first(), shareReplay()) | 70 | .pipe(first(), shareReplay()) |
68 | .toPromise() | 71 | .toPromise() |
@@ -104,6 +107,11 @@ export class PluginService implements ClientHook { | |||
104 | } | 107 | } |
105 | 108 | ||
106 | async loadPluginsByScope (scope: PluginClientScope, isReload = false) { | 109 | async loadPluginsByScope (scope: PluginClientScope, isReload = false) { |
110 | if (this.loadingScopes[scope]) return | ||
111 | if (!isReload && this.loadedScopes.includes(scope)) return | ||
112 | |||
113 | this.loadingScopes[scope] = true | ||
114 | |||
107 | try { | 115 | try { |
108 | await this.ensurePluginsAreBuilt() | 116 | await this.ensurePluginsAreBuilt() |
109 | 117 | ||
@@ -111,6 +119,7 @@ export class PluginService implements ClientHook { | |||
111 | 119 | ||
112 | const toLoad = this.scopes[ scope ] | 120 | const toLoad = this.scopes[ scope ] |
113 | if (!Array.isArray(toLoad)) { | 121 | if (!Array.isArray(toLoad)) { |
122 | this.loadingScopes[scope] = false | ||
114 | this.pluginsLoaded[scope].next(true) | 123 | this.pluginsLoaded[scope].next(true) |
115 | 124 | ||
116 | return | 125 | return |
@@ -130,6 +139,7 @@ export class PluginService implements ClientHook { | |||
130 | await Promise.all(promises) | 139 | await Promise.all(promises) |
131 | 140 | ||
132 | this.pluginsLoaded[scope].next(true) | 141 | this.pluginsLoaded[scope].next(true) |
142 | this.loadingScopes[scope] = false | ||
133 | } catch (err) { | 143 | } catch (err) { |
134 | console.error('Cannot load plugins by scope %s.', scope, err) | 144 | console.error('Cannot load plugins by scope %s.', scope, err) |
135 | } | 145 | } |