feat: optimiz plugin system
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
268f5d2837
commit
93997cd695
@ -9,10 +9,9 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata
|
||||
*/
|
||||
export function plugin(metadata: interfaces.PluginMetadata) {
|
||||
return function(target: any) {
|
||||
target.description = metadata;
|
||||
metadata.target = target;
|
||||
decorate(injectable(), target);
|
||||
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target.constructor);
|
||||
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target);
|
||||
const previousMetadata: Map<string, interfaces.PluginMetadata> = getPluginMetadatas();
|
||||
previousMetadata.set(metadata.name, metadata);
|
||||
Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect);
|
||||
|
@ -1,4 +1,7 @@
|
||||
import { injectable } from "@ms/container";
|
||||
import { server, MiaoScriptConsole } from "@ms/api";
|
||||
import { METADATA_KEY } from './constants'
|
||||
import { injectable, inject, postConstruct } from "@ms/container";
|
||||
import { getPluginMetadata } from "./utils";
|
||||
|
||||
export namespace interfaces {
|
||||
@injectable()
|
||||
@ -6,6 +9,12 @@ export namespace interfaces {
|
||||
public description: PluginMetadata;
|
||||
public logger: Console;
|
||||
|
||||
constructor(@inject(server.Console) Console: MiaoScriptConsole) {
|
||||
this.description = getPluginMetadata(this)
|
||||
// @ts-ignore
|
||||
this.logger = new Console(this.description.prefix || this.description.name)
|
||||
}
|
||||
|
||||
public load() { }
|
||||
public enable() { }
|
||||
public disable() { }
|
||||
|
@ -13,8 +13,6 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
private pluginInstance: any
|
||||
@inject(server.ServerType)
|
||||
private serverType: string
|
||||
@inject(server.Console)
|
||||
private Console: MiaoScriptConsole
|
||||
@inject(command.Command)
|
||||
private CommandManager: command.Command
|
||||
@inject(event.Event)
|
||||
@ -149,19 +147,22 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
private checkServers(servers: string[]) {
|
||||
if (!servers) { return true }
|
||||
return servers?.indexOf(this.serverType) != -1 && servers?.indexOf(`!${this.serverType}`) == -1
|
||||
}
|
||||
|
||||
private createPlugin(file: string) {
|
||||
//@ts-ignore
|
||||
require(file, {
|
||||
cache: false
|
||||
})
|
||||
require(file, { cache: false })
|
||||
}
|
||||
|
||||
private buildPlugins() {
|
||||
let pluginMetadatas = getPluginMetadatas()
|
||||
pluginMetadatas.forEach(metadata => {
|
||||
if (metadata.servers?.indexOf(this.serverType) == -1) { return }
|
||||
for (const [_, metadata] of pluginMetadatas) {
|
||||
if (!this.checkServers(metadata.servers)) { continue }
|
||||
this.buildPlugin(metadata)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private buildPlugin(metadata: interfaces.PluginMetadata) {
|
||||
@ -172,9 +173,6 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
return
|
||||
}
|
||||
this.pluginMap.set(metadata.name, pluginInstance)
|
||||
pluginInstance.description = metadata
|
||||
// @ts-ignore
|
||||
pluginInstance.logger = new this.Console(metadata.prefix || metadata.name)
|
||||
return pluginInstance
|
||||
}
|
||||
|
||||
@ -195,7 +193,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
let tabs = getPluginTabCompleterMetadata(pluginInstance)
|
||||
for (const [_, cmd] of cmds) {
|
||||
let tab = tabs.get(cmd.name)
|
||||
if (cmd.servers?.indexOf(this.serverType) == -1) { continue }
|
||||
if (!this.checkServers(cmd.servers)) { continue }
|
||||
this.CommandManager.on(pluginInstance, cmd.name, {
|
||||
cmd: pluginInstance[cmd.executor].bind(pluginInstance),
|
||||
tab: tab ? pluginInstance[tab.executor].bind(pluginInstance) : undefined
|
||||
@ -207,7 +205,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
|
||||
let events = getPluginListenerMetadata(pluginInstance)
|
||||
for (const event of events) {
|
||||
// ignore space listener
|
||||
if (event.servers?.indexOf(this.serverType) == -1) { continue }
|
||||
if (!this.checkServers(event.servers)) { continue }
|
||||
// here must bind this to pluginInstance
|
||||
this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user