]>
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' | |
10 | import { first } from 'rxjs/operators' | |
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 } = {} | |
24 | ||
25 | private hooks: { [ name: string ]: HookStructValue[] } = {} | |
26 | ||
27 | constructor ( | |
28 | private router: Router, | |
29 | private server: ServerService | |
30 | ) { | |
31 | } | |
32 | ||
33 | initializePlugins () { | |
34 | this.server.configLoaded | |
35 | .subscribe(() => { | |
36 | this.plugins = this.server.getConfig().plugins | |
37 | ||
38 | this.buildScopeStruct() | |
39 | ||
40 | this.pluginsLoaded.next(true) | |
41 | }) | |
42 | } | |
43 | ||
44 | ensurePluginsAreLoaded () { | |
45 | return this.pluginsLoaded.asObservable() | |
46 | .pipe(first()) | |
47 | .toPromise() | |
48 | } | |
49 | ||
50 | async loadPluginsByScope (scope: PluginScope) { | |
51 | try { | |
52 | await this.ensurePluginsAreLoaded() | |
53 | ||
54 | const toLoad = this.scopes[ scope ] | |
55 | if (!Array.isArray(toLoad)) return | |
56 | ||
57 | const promises: Promise<any>[] = [] | |
58 | for (const { plugin, clientScript } of toLoad) { | |
59 | if (this.loadedScripts[ clientScript.script ]) continue | |
60 | ||
61 | promises.push(this.loadPlugin(plugin, clientScript)) | |
62 | ||
63 | this.loadedScripts[ clientScript.script ] = true | |
64 | } | |
65 | ||
66 | return Promise.all(promises) | |
67 | } catch (err) { | |
68 | console.error('Cannot load plugins by scope %s.', scope, err) | |
69 | } | |
70 | } | |
71 | ||
72 | async runHook (hookName: string, param?: any) { | |
73 | let result = param | |
74 | ||
75 | const wait = hookName.startsWith('static:') | |
76 | ||
77 | for (const hook of this.hooks[hookName]) { | |
78 | try { | |
79 | if (wait) result = await hook.handler(param) | |
80 | else result = hook.handler() | |
81 | } catch (err) { | |
82 | console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err) | |
83 | } | |
84 | } | |
85 | ||
86 | return result | |
87 | } | |
88 | ||
89 | private loadPlugin (plugin: ServerConfigPlugin, clientScript: ClientScript) { | |
90 | const registerHook = (options: RegisterHookOptions) => { | |
91 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | |
92 | ||
93 | this.hooks[options.target].push({ | |
94 | plugin, | |
95 | clientScript, | |
96 | target: options.target, | |
97 | handler: options.handler, | |
98 | priority: options.priority || 0 | |
99 | }) | |
100 | } | |
101 | ||
102 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | |
103 | ||
104 | const url = environment.apiUrl + `/plugins/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}` | |
105 | ||
106 | return import(/* webpackIgnore: true */ url) | |
107 | .then(script => script.register({ registerHook })) | |
108 | .then(() => this.sortHooksByPriority()) | |
109 | } | |
110 | ||
111 | private buildScopeStruct () { | |
112 | for (const plugin of this.plugins) { | |
113 | for (const key of Object.keys(plugin.clientScripts)) { | |
114 | const clientScript = plugin.clientScripts[key] | |
115 | ||
116 | for (const scope of clientScript.scopes) { | |
117 | if (!this.scopes[scope]) this.scopes[scope] = [] | |
118 | ||
119 | this.scopes[scope].push({ | |
120 | plugin, | |
121 | clientScript | |
122 | }) | |
123 | ||
124 | this.loadedScripts[clientScript.script] = false | |
125 | } | |
126 | } | |
127 | } | |
128 | } | |
129 | ||
130 | private sortHooksByPriority () { | |
131 | for (const hookName of Object.keys(this.hooks)) { | |
132 | this.hooks[hookName].sort((a, b) => { | |
133 | return b.priority - a.priority | |
134 | }) | |
135 | } | |
136 | } | |
137 | } |