From de491bdd62f5a7c0f19ca8b9827906ac0db74859 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 17 Jan 2020 17:45:13 +0800 Subject: [PATCH] feat: optimiz plugin system Signed-off-by: MiaoWoo --- packages/plugin/src/decorators.ts | 3 +-- packages/plugin/src/interfaces.ts | 11 ++++++++++- packages/plugin/src/manager.ts | 24 +++++++++++------------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/plugin/src/decorators.ts b/packages/plugin/src/decorators.ts index 26f37166..0c7054d5 100644 --- a/packages/plugin/src/decorators.ts +++ b/packages/plugin/src/decorators.ts @@ -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 = getPluginMetadatas(); previousMetadata.set(metadata.name, metadata); Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect); diff --git a/packages/plugin/src/interfaces.ts b/packages/plugin/src/interfaces.ts index c6307668..93f1d176 100644 --- a/packages/plugin/src/interfaces.ts +++ b/packages/plugin/src/interfaces.ts @@ -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() { } diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index 30643b09..26f42c65 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -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)) }