1 import { Injectable } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { ServerConfigPlugin } from '@shared/models'
4 import { ServerService } from '@app/core/server/server.service'
5 import { ClientScript } from '@shared/models/plugins/plugin-package-json.model'
6 import { PluginScope } from '@shared/models/plugins/plugin-scope.type'
7 import { environment } from '../../../environments/environment'
8 import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model'
9 import { ReplaySubject } from 'rxjs'
10 import { first, shareReplay } from 'rxjs/operators'
12 interface HookStructValue extends RegisterHookOptions {
13 plugin: ServerConfigPlugin
14 clientScript: ClientScript
18 export class PluginService {
19 pluginsLoaded = new ReplaySubject<boolean>(1)
21 private plugins: ServerConfigPlugin[] = []
22 private scopes: { [ scopeName: string ]: { plugin: ServerConfigPlugin, clientScript: ClientScript }[] } = {}
23 private loadedScripts: { [ script: string ]: boolean } = {}
24 private loadedScopes: PluginScope[] = []
26 private hooks: { [ name: string ]: HookStructValue[] } = {}
29 private router: Router,
30 private server: ServerService
34 initializePlugins () {
35 this.server.configLoaded
37 this.plugins = this.server.getConfig().plugin.registered
39 this.buildScopeStruct()
41 this.pluginsLoaded.next(true)
45 ensurePluginsAreLoaded () {
46 return this.pluginsLoaded.asObservable()
47 .pipe(first(), shareReplay())
51 addPlugin (plugin: ServerConfigPlugin) {
52 for (const key of Object.keys(plugin.clientScripts)) {
53 const clientScript = plugin.clientScripts[key]
55 for (const scope of clientScript.scopes) {
56 if (!this.scopes[scope]) this.scopes[scope] = []
58 this.scopes[scope].push({
61 script: environment.apiUrl + `/plugins/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
62 scopes: clientScript.scopes
66 this.loadedScripts[clientScript.script] = false
71 removePlugin (plugin: ServerConfigPlugin) {
72 for (const key of Object.keys(this.scopes)) {
73 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
77 async reloadLoadedScopes () {
78 for (const scope of this.loadedScopes) {
79 await this.loadPluginsByScope(scope)
83 async loadPluginsByScope (scope: PluginScope) {
85 await this.ensurePluginsAreLoaded()
87 this.loadedScopes.push(scope)
89 const toLoad = this.scopes[ scope ]
90 if (!Array.isArray(toLoad)) return
92 const promises: Promise<any>[] = []
93 for (const { plugin, clientScript } of toLoad) {
94 if (this.loadedScripts[ clientScript.script ]) continue
96 promises.push(this.loadPlugin(plugin, clientScript))
98 this.loadedScripts[ clientScript.script ] = true
101 await Promise.all(promises)
103 console.error('Cannot load plugins by scope %s.', scope, err)
107 async runHook (hookName: string, param?: any) {
110 const wait = hookName.startsWith('static:')
112 for (const hook of this.hooks[hookName]) {
114 if (wait) result = await hook.handler(param)
115 else result = hook.handler()
117 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err)
124 private loadPlugin (plugin: ServerConfigPlugin, clientScript: ClientScript) {
125 const registerHook = (options: RegisterHookOptions) => {
126 if (!this.hooks[options.target]) this.hooks[options.target] = []
128 this.hooks[options.target].push({
131 target: options.target,
132 handler: options.handler,
133 priority: options.priority || 0
137 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
139 return import(/* webpackIgnore: true */ clientScript.script)
140 .then(script => script.register({ registerHook }))
141 .then(() => this.sortHooksByPriority())
144 private buildScopeStruct () {
145 for (const plugin of this.plugins) {
146 this.addPlugin(plugin)
150 private sortHooksByPriority () {
151 for (const hookName of Object.keys(this.hooks)) {
152 this.hooks[hookName].sort((a, b) => {
153 return b.priority - a.priority