Run hooks and register plugins outside angular

This commit is contained in:
Chocobozzz 2019-07-26 09:41:49 +02:00
parent ba211e7386
commit 16d5469629
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 20 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import { Injectable } from '@angular/core' import { Injectable, NgZone } from '@angular/core'
import { Router } from '@angular/router' import { Router } from '@angular/router'
import { ServerConfigPlugin } from '@shared/models' import { ServerConfigPlugin } from '@shared/models'
import { ServerService } from '@app/core/server/server.service' import { ServerService } from '@app/core/server/server.service'
@ -11,7 +11,6 @@ import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
import { ClientHook, ClientHookName, clientHookObject } 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 { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type'
import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model' import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model'
import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
import { HttpClient } from '@angular/common/http' import { HttpClient } from '@angular/common/http'
import { RestExtractor } from '@app/shared/rest' import { RestExtractor } from '@app/shared/rest'
import { PluginType } from '@shared/models/plugins/plugin.type' import { PluginType } from '@shared/models/plugins/plugin.type'
@ -52,6 +51,7 @@ export class PluginService implements ClientHook {
constructor ( constructor (
private router: Router, private router: Router,
private server: ServerService, private server: ServerService,
private zone: NgZone,
private authHttp: HttpClient, private authHttp: HttpClient,
private restExtractor: RestExtractor private restExtractor: RestExtractor
) { ) {
@ -157,12 +157,13 @@ export class PluginService implements ClientHook {
} }
} }
async runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> { runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
if (!this.hooks[hookName]) return Promise.resolve(result) return this.zone.runOutsideAngular(async () => {
if (!this.hooks[ hookName ]) return result
const hookType = getHookType(hookName) const hookType = getHookType(hookName)
for (const hook of this.hooks[hookName]) { for (const hook of this.hooks[ hookName ]) {
console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name) console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
result = await internalRunHook(hook.handler, hookType, result, params, err => { result = await internalRunHook(hook.handler, hookType, result, params, err => {
@ -171,6 +172,7 @@ export class PluginService implements ClientHook {
} }
return result return result
})
} }
nameToNpmName (name: string, type: PluginType) { nameToNpmName (name: string, type: PluginType) {
@ -211,10 +213,12 @@ export class PluginService implements ClientHook {
console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
return this.zone.runOutsideAngular(() => {
return import(/* webpackIgnore: true */ clientScript.script) return import(/* webpackIgnore: true */ clientScript.script)
.then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers })) .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers }))
.then(() => this.sortHooksByPriority()) .then(() => this.sortHooksByPriority())
.catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
})
} }
private buildScopeStruct () { private buildScopeStruct () {