]>
Commit | Line | Data |
---|---|---|
18a6f04c C |
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.model' | |
9 | import { ReplaySubject } from 'rxjs' | |
ffb321be | 10 | import { first, shareReplay } from 'rxjs/operators' |
18a6f04c C |
11 | |
12 | interface HookStructValue extends RegisterHookOptions { | |
13 | plugin: ServerConfigPlugin | |
14 | clientScript: ClientScript | |
15 | } | |
16 | ||
17 | @Injectable() | |
18 | export class PluginService { | |
19 | pluginsLoaded = new ReplaySubject<boolean>(1) | |
20 | ||
21 | private plugins: ServerConfigPlugin[] = [] | |
22 | private scopes: { [ scopeName: string ]: { plugin: ServerConfigPlugin, clientScript: ClientScript }[] } = {} | |
23 | private loadedScripts: { [ script: string ]: boolean } = {} | |
ffb321be | 24 | private loadedScopes: PluginScope[] = [] |
18a6f04c C |
25 | |
26 | private hooks: { [ name: string ]: HookStructValue[] } = {} | |
27 | ||
28 | constructor ( | |
29 | private router: Router, | |
30 | private server: ServerService | |
31 | ) { | |
32 | } | |
33 | ||
34 | initializePlugins () { | |
35 | this.server.configLoaded | |
36 | .subscribe(() => { | |
7cd4d2ba | 37 | this.plugins = this.server.getConfig().plugin.registered |
18a6f04c C |
38 | |
39 | this.buildScopeStruct() | |
40 | ||
41 | this.pluginsLoaded.next(true) | |
42 | }) | |
43 | } | |
44 | ||
45 | ensurePluginsAreLoaded () { | |
46 | return this.pluginsLoaded.asObservable() | |
ffb321be | 47 | .pipe(first(), shareReplay()) |
18a6f04c C |
48 | .toPromise() |
49 | } | |
50 | ||
ffb321be C |
51 | addPlugin (plugin: ServerConfigPlugin) { |
52 | for (const key of Object.keys(plugin.clientScripts)) { | |
53 | const clientScript = plugin.clientScripts[key] | |
54 | ||
55 | for (const scope of clientScript.scopes) { | |
56 | if (!this.scopes[scope]) this.scopes[scope] = [] | |
57 | ||
58 | this.scopes[scope].push({ | |
59 | plugin, | |
60 | clientScript: { | |
61 | script: environment.apiUrl + `/plugins/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, | |
62 | scopes: clientScript.scopes | |
63 | } | |
64 | }) | |
65 | ||
66 | this.loadedScripts[clientScript.script] = false | |
67 | } | |
68 | } | |
69 | } | |
70 | ||
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) | |
74 | } | |
75 | } | |
76 | ||
77 | async reloadLoadedScopes () { | |
78 | for (const scope of this.loadedScopes) { | |
79 | await this.loadPluginsByScope(scope) | |
80 | } | |
81 | } | |
82 | ||
18a6f04c C |
83 | async loadPluginsByScope (scope: PluginScope) { |
84 | try { | |
85 | await this.ensurePluginsAreLoaded() | |
86 | ||
ffb321be C |
87 | this.loadedScopes.push(scope) |
88 | ||
18a6f04c C |
89 | const toLoad = this.scopes[ scope ] |
90 | if (!Array.isArray(toLoad)) return | |
91 | ||
92 | const promises: Promise<any>[] = [] | |
93 | for (const { plugin, clientScript } of toLoad) { | |
94 | if (this.loadedScripts[ clientScript.script ]) continue | |
95 | ||
96 | promises.push(this.loadPlugin(plugin, clientScript)) | |
97 | ||
98 | this.loadedScripts[ clientScript.script ] = true | |
99 | } | |
100 | ||
ffb321be | 101 | await Promise.all(promises) |
18a6f04c C |
102 | } catch (err) { |
103 | console.error('Cannot load plugins by scope %s.', scope, err) | |
104 | } | |
105 | } | |
106 | ||
107 | async runHook (hookName: string, param?: any) { | |
108 | let result = param | |
109 | ||
110 | const wait = hookName.startsWith('static:') | |
111 | ||
112 | for (const hook of this.hooks[hookName]) { | |
113 | try { | |
114 | if (wait) result = await hook.handler(param) | |
115 | else result = hook.handler() | |
116 | } catch (err) { | |
117 | console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err) | |
118 | } | |
119 | } | |
120 | ||
121 | return result | |
122 | } | |
123 | ||
124 | private loadPlugin (plugin: ServerConfigPlugin, clientScript: ClientScript) { | |
125 | const registerHook = (options: RegisterHookOptions) => { | |
126 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | |
127 | ||
128 | this.hooks[options.target].push({ | |
129 | plugin, | |
130 | clientScript, | |
131 | target: options.target, | |
132 | handler: options.handler, | |
133 | priority: options.priority || 0 | |
134 | }) | |
135 | } | |
136 | ||
137 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | |
138 | ||
ffb321be | 139 | return import(/* webpackIgnore: true */ clientScript.script) |
18a6f04c C |
140 | .then(script => script.register({ registerHook })) |
141 | .then(() => this.sortHooksByPriority()) | |
142 | } | |
143 | ||
144 | private buildScopeStruct () { | |
145 | for (const plugin of this.plugins) { | |
ffb321be | 146 | this.addPlugin(plugin) |
18a6f04c C |
147 | } |
148 | } | |
149 | ||
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 | |
154 | }) | |
155 | } | |
156 | } | |
157 | } |