From 046c86681b501fa8681ad6c6670845eddc140456 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 21 Sep 2019 15:05:37 +0800 Subject: [PATCH] feat: support multi stage load Signed-off-by: MiaoWoo --- packages/plugin/src/decorators.ts | 6 +++--- packages/plugin/src/interfaces.ts | 1 + packages/plugin/src/manager.ts | 28 +++++++++++++--------------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/plugin/src/decorators.ts b/packages/plugin/src/decorators.ts index 19806147..54700823 100644 --- a/packages/plugin/src/decorators.ts +++ b/packages/plugin/src/decorators.ts @@ -1,6 +1,6 @@ +import { injectable, decorate } from "@ms/container"; import { interfaces } from './interfaces' import { METADATA_KEY } from './constants' -import { injectable, decorate } from "inversify"; import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata } from './utils' /** @@ -40,7 +40,7 @@ export function cmd(metadata: interfaces.CommandMetadata = {}) { */ export function tab(metadata: interfaces.TabCompleterMetadata = {}) { return function(target: any, key: string, value: any) { - metadata.name = metadata.name || key; + metadata.name = metadata.name || key.startsWith('tab') ? key.split('tab', 2)[0] : key; metadata.executor = key; metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key) const previousMetadata: Map = getPluginTabCompleterMetadata(target) @@ -60,4 +60,4 @@ export function listener(metadata: interfaces.ListenerMetadata = {}) { const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target) Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor); }; -} \ No newline at end of file +} diff --git a/packages/plugin/src/interfaces.ts b/packages/plugin/src/interfaces.ts index cef81e3d..18c48f43 100644 --- a/packages/plugin/src/interfaces.ts +++ b/packages/plugin/src/interfaces.ts @@ -30,6 +30,7 @@ export namespace interfaces { export interface ListenerMetadata { name?: string; executor?: string; + servertype?: string; } export type PluginLike = Plugin | string; } diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index f1900a9f..1b96535a 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -45,25 +45,30 @@ export class PluginManagerImpl implements plugin.PluginManager { } load(...args: any[]): void { - this.checkAndGet(args[0]).forEach(pl => this.runCatch(pl, 'load')); + this.checkAndGet(args[0]).forEach(pl => { + this.runCatch(pl, 'load'); + this.runCatch(pl, `${this.serverType}load`); + }); } enable(...args: any[]): void { - this.checkAndGet(args[0]).forEach(pl => this.runCatch(pl, 'enable')); + this.checkAndGet(args[0]).forEach(pl => { + this.runCatch(pl, 'enable') + this.runCatch(pl, `${this.serverType}enable`); + }); } disable(...args: any[]): void { this.checkAndGet(args[0]).forEach(pl => { this.runCatch(pl, 'disable'); + this.runCatch(pl, `${this.serverType}disable`); this.EventManager.disable(pl); }); - } reload(...args: any[]): void { - this.checkAndGet(arguments[0]).forEach((pl: interfaces.Plugin) => { + this.checkAndGet(args[0]).forEach((pl: interfaces.Plugin) => { this.disable(pl); - this.EventManager.disable(pl); this.loadPlugin(pl.description.source); pl = this.buildPlugin(getPlugin(pl.description.name)); this.load(pl); @@ -73,7 +78,7 @@ export class PluginManagerImpl implements plugin.PluginManager { private runCatch(pl: any, func: string) { try { - pl[func].call(pl); + if (pl[func]) pl[func].call(pl); } catch (ex) { console.console(`§6插件 §b${pl.description.name} §6执行 §d${func} §6方法时发生错误 §4${ex}`); console.ex(ex); @@ -146,15 +151,6 @@ export class PluginManagerImpl implements plugin.PluginManager { }); } - private beforeLoadHook(origin) { - var result = origin; - // // 注入 console 对象 // 给插件注入单独的 console - // result += '\nvar console = new Console(); module.exports.console = console;'; - // // 插件注入 self 对象 - // result += '\nvar self = {}; module.exports.self = self;'; - return result; - } - private buildPlugins(container: Container) { let pluginMetadatas = getPluginMetadatas(); pluginMetadatas.forEach(metadata => { @@ -197,6 +193,8 @@ export class PluginManagerImpl implements plugin.PluginManager { private registryListener(pluginInstance: interfaces.Plugin) { let events = getPluginListenerMetadata(pluginInstance) for (const event of events) { + // ignore space listener + if (event.servertype && event.servertype != this.serverType) { continue; } // here must bind this to pluginInstance this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance)); }