Lazy load client script scopes

This commit is contained in:
Chocobozzz 2019-07-23 12:16:34 +02:00 committed by Chocobozzz
parent 5b77537ce5
commit c9e3eeedad
5 changed files with 18 additions and 13 deletions

View File

@ -206,9 +206,7 @@ export class AppComponent implements OnInit {
private async loadPlugins () { private async loadPlugins () {
this.pluginService.initializePlugins() this.pluginService.initializePlugins()
await this.pluginService.loadPluginsByScope('common') this.hooks.runAction('action:application.init', 'common')
this.hooks.runAction('action:application.init')
} }
private initHotkeys () { private initHotkeys () {

View File

@ -37,8 +37,9 @@ export class HooksService {
return this.pluginService.runHook(hookName, result, params) return this.pluginService.runHook(hookName, result, params)
} }
runAction<T, U extends ClientActionHookName> (hookName: U, params?: T) { runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) {
this.pluginService.runHook(hookName, params) this.pluginService.ensurePluginsAreLoaded(scope)
.then(() => this.pluginService.runHook(hookName, params))
.catch((err: any) => console.error('Fatal hook error.', { err })) .catch((err: any) => console.error('Fatal hook error.', { err }))
} }
} }

View File

@ -36,6 +36,7 @@ export class PluginService implements ClientHook {
private scopes: { [ scopeName: string ]: PluginInfo[] } = {} private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
private loadedScripts: { [ script: string ]: boolean } = {} private loadedScripts: { [ script: string ]: boolean } = {}
private loadedScopes: PluginClientScope[] = [] private loadedScopes: PluginClientScope[] = []
private loadingScopes: { [id in PluginClientScope]?: boolean } = {}
private hooks: { [ name: string ]: HookStructValue[] } = {} private hooks: { [ name: string ]: HookStructValue[] } = {}
@ -63,6 +64,8 @@ export class PluginService implements ClientHook {
} }
ensurePluginsAreLoaded (scope: PluginClientScope) { ensurePluginsAreLoaded (scope: PluginClientScope) {
this.loadPluginsByScope(scope)
return this.pluginsLoaded[scope].asObservable() return this.pluginsLoaded[scope].asObservable()
.pipe(first(), shareReplay()) .pipe(first(), shareReplay())
.toPromise() .toPromise()
@ -104,6 +107,11 @@ export class PluginService implements ClientHook {
} }
async loadPluginsByScope (scope: PluginClientScope, isReload = false) { async loadPluginsByScope (scope: PluginClientScope, isReload = false) {
if (this.loadingScopes[scope]) return
if (!isReload && this.loadedScopes.includes(scope)) return
this.loadingScopes[scope] = true
try { try {
await this.ensurePluginsAreBuilt() await this.ensurePluginsAreBuilt()
@ -111,6 +119,7 @@ export class PluginService implements ClientHook {
const toLoad = this.scopes[ scope ] const toLoad = this.scopes[ scope ]
if (!Array.isArray(toLoad)) { if (!Array.isArray(toLoad)) {
this.loadingScopes[scope] = false
this.pluginsLoaded[scope].next(true) this.pluginsLoaded[scope].next(true)
return return
@ -130,6 +139,7 @@ export class PluginService implements ClientHook {
await Promise.all(promises) await Promise.all(promises)
this.pluginsLoaded[scope].next(true) this.pluginsLoaded[scope].next(true)
this.loadingScopes[scope] = false
} catch (err) { } catch (err) {
console.error('Cannot load plugins by scope %s.', scope, err) console.error('Cannot load plugins by scope %s.', scope, err)
} }

View File

@ -53,8 +53,6 @@ export class SearchComponent implements OnInit, OnDestroy {
} }
ngOnInit () { ngOnInit () {
this.pluginService.loadPluginsByScope('search')
this.subActivatedRoute = this.route.queryParams.subscribe( this.subActivatedRoute = this.route.queryParams.subscribe(
queryParams => { queryParams => {
const querySearch = queryParams['search'] const querySearch = queryParams['search']
@ -80,7 +78,7 @@ export class SearchComponent implements OnInit, OnDestroy {
err => this.notifier.error(err.text) err => this.notifier.error(err.text)
) )
this.hooks.runAction('action:search.init') this.hooks.runAction('action:search.init', 'search')
} }
ngOnDestroy () { ngOnDestroy () {

View File

@ -103,8 +103,6 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
} }
async ngOnInit () { async ngOnInit () {
this.pluginService.loadPluginsByScope('video-watch')
this.configSub = this.serverService.configLoaded this.configSub = this.serverService.configLoaded
.subscribe(() => { .subscribe(() => {
if ( if (
@ -133,7 +131,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
this.theaterEnabled = getStoredTheater() this.theaterEnabled = getStoredTheater()
this.hooks.runAction('action:video-watch.init') this.hooks.runAction('action:video-watch.init', 'video-watch')
} }
ngOnDestroy () { ngOnDestroy () {
@ -497,7 +495,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
this.setOpenGraphTags() this.setOpenGraphTags()
this.checkUserRating() this.checkUserRating()
this.hooks.runAction('action:video-watch.video.loaded') this.hooks.runAction('action:video-watch.video.loaded', 'video-watch')
} }
private setRating (nextRating: UserVideoRateType) { private setRating (nextRating: UserVideoRateType) {