feat: opitmize plugin manager
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
20749d3381
commit
bc17e2f376
@ -129,6 +129,10 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
if (loader.require(loadMetadata).loaded) {
|
if (loader.require(loadMetadata).loaded) {
|
||||||
loadMetadata.loader = loader
|
loadMetadata.loader = loader
|
||||||
let metadata = loadMetadata.metadata
|
let metadata = loadMetadata.metadata
|
||||||
|
if (this.metadataMap.has(metadata.name)) {
|
||||||
|
let oldMetadata = this.metadataMap.get(metadata.name)
|
||||||
|
throw new Error(`Plugin ${oldMetadata.name} is already load from ${oldMetadata.source}...`)
|
||||||
|
}
|
||||||
this.metadataMap.set(metadata.name, metadata)
|
this.metadataMap.set(metadata.name, metadata)
|
||||||
metadata.loadMetadata = loadMetadata
|
metadata.loadMetadata = loadMetadata
|
||||||
}
|
}
|
||||||
@ -151,7 +155,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
if (!file) { throw new Error('plugin file can\'t be undefiend!') }
|
if (!file) { throw new Error('plugin file can\'t be undefiend!') }
|
||||||
if (!scanner) { throw new Error('plugin scanner can\'t be undefiend!') }
|
if (!scanner) { throw new Error('plugin scanner can\'t be undefiend!') }
|
||||||
let metadata = this.loadPlugin(scanner.load(scanner.read(file)))
|
let metadata = this.loadPlugin(scanner.load(scanner.read(file)))
|
||||||
let plugin = metadata.loadMetadata.loader.build(metadata)
|
let plugin = this.buildPlugin(metadata)
|
||||||
this.load(plugin)
|
this.load(plugin)
|
||||||
this.enable(plugin)
|
this.enable(plugin)
|
||||||
return plugin
|
return plugin
|
||||||
@ -166,7 +170,11 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
disable(...args: any[]): void {
|
disable(...args: any[]): void {
|
||||||
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => this.runPluginStage(plugin, 'disable'))
|
this.checkAndGet(args[0]).forEach((plugin: plugin.Plugin) => {
|
||||||
|
this.runPluginStage(plugin, 'disable')
|
||||||
|
this.metadataMap.delete(plugin.description.name)
|
||||||
|
this.instanceMap.delete(plugin.description.name)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
reload(...args: any[]): void {
|
reload(...args: any[]): void {
|
||||||
@ -203,15 +211,21 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private buildPlugins() {
|
private buildPlugins() {
|
||||||
for (const [, metadata] of this.metadataMap) {
|
this.metadataMap.forEach((metadata) => {
|
||||||
let pluginInstance: plugin.Plugin
|
try {
|
||||||
if (!this.loaderMap.has(metadata.type)) {
|
this.buildPlugin(metadata)
|
||||||
console.error(`§4无法加载插件 §c${metadata.name} §4请检查 §c${metadata.type} §4加载器是否正常启用!`)
|
} catch (error) {
|
||||||
continue
|
console.error(error)
|
||||||
}
|
}
|
||||||
pluginInstance = this.loaderMap.get(metadata.type).build(metadata)
|
})
|
||||||
if (!pluginInstance) { console.error(`§4加载器 §c${metadata.type} §4加载插件 §c${metadata.name} §4失败!`); continue }
|
}
|
||||||
|
|
||||||
|
private buildPlugin(metadata: plugin.PluginMetadata) {
|
||||||
|
if (!this.loaderMap.has(metadata.type)) { throw new Error(`§4无法加载插件 §c${metadata.name} §4请检查 §c${metadata.type} §4加载器是否正常启用!`) }
|
||||||
|
let pluginInstance = this.loaderMap.get(metadata.type).build(metadata)
|
||||||
|
if (!pluginInstance) { throw new Error(`§4加载器 §c${metadata.type} §4加载插件 §c${metadata.name} §4失败!`) }
|
||||||
|
if (this.instanceMap.has(metadata.name)) { throw new Error(`Plugin ${metadata.name} is already load from ${metadata.source}...`) }
|
||||||
this.instanceMap.set(metadata.name, pluginInstance)
|
this.instanceMap.set(metadata.name, pluginInstance)
|
||||||
}
|
return pluginInstance
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user