feat: support multi stage load

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-21 15:05:37 +08:00
parent 5ea4b6bdaf
commit 046c86681b
3 changed files with 17 additions and 18 deletions

View File

@ -1,6 +1,6 @@
import { injectable, decorate } from "@ms/container";
import { interfaces } from './interfaces' import { interfaces } from './interfaces'
import { METADATA_KEY } from './constants' import { METADATA_KEY } from './constants'
import { injectable, decorate } from "inversify";
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata } from './utils' import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata } from './utils'
/** /**
@ -40,7 +40,7 @@ export function cmd(metadata: interfaces.CommandMetadata = {}) {
*/ */
export function tab(metadata: interfaces.TabCompleterMetadata = {}) { export function tab(metadata: interfaces.TabCompleterMetadata = {}) {
return function(target: any, key: string, value: any) { 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.executor = key;
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key) metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
const previousMetadata: Map<string, interfaces.TabCompleterMetadata> = getPluginTabCompleterMetadata(target) const previousMetadata: Map<string, interfaces.TabCompleterMetadata> = getPluginTabCompleterMetadata(target)
@ -60,4 +60,4 @@ export function listener(metadata: interfaces.ListenerMetadata = {}) {
const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target) const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target)
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor); Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor);
}; };
} }

View File

@ -30,6 +30,7 @@ export namespace interfaces {
export interface ListenerMetadata { export interface ListenerMetadata {
name?: string; name?: string;
executor?: string; executor?: string;
servertype?: string;
} }
export type PluginLike = Plugin | string; export type PluginLike = Plugin | string;
} }

View File

@ -45,25 +45,30 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
load(...args: any[]): void { 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 { 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 { disable(...args: any[]): void {
this.checkAndGet(args[0]).forEach(pl => { this.checkAndGet(args[0]).forEach(pl => {
this.runCatch(pl, 'disable'); this.runCatch(pl, 'disable');
this.runCatch(pl, `${this.serverType}disable`);
this.EventManager.disable(pl); this.EventManager.disable(pl);
}); });
} }
reload(...args: any[]): void { reload(...args: any[]): void {
this.checkAndGet(arguments[0]).forEach((pl: interfaces.Plugin) => { this.checkAndGet(args[0]).forEach((pl: interfaces.Plugin) => {
this.disable(pl); this.disable(pl);
this.EventManager.disable(pl);
this.loadPlugin(pl.description.source); this.loadPlugin(pl.description.source);
pl = this.buildPlugin(getPlugin(pl.description.name)); pl = this.buildPlugin(getPlugin(pl.description.name));
this.load(pl); this.load(pl);
@ -73,7 +78,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
private runCatch(pl: any, func: string) { private runCatch(pl: any, func: string) {
try { try {
pl[func].call(pl); if (pl[func]) pl[func].call(pl);
} catch (ex) { } catch (ex) {
console.console(`§6插件 §b${pl.description.name} §6执行 §d${func} §6方法时发生错误 §4${ex}`); console.console(`§6插件 §b${pl.description.name} §6执行 §d${func} §6方法时发生错误 §4${ex}`);
console.ex(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) { private buildPlugins(container: Container) {
let pluginMetadatas = getPluginMetadatas(); let pluginMetadatas = getPluginMetadatas();
pluginMetadatas.forEach(metadata => { pluginMetadatas.forEach(metadata => {
@ -197,6 +193,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
private registryListener(pluginInstance: interfaces.Plugin) { private registryListener(pluginInstance: interfaces.Plugin) {
let events = getPluginListenerMetadata(pluginInstance) let events = getPluginListenerMetadata(pluginInstance)
for (const event of events) { for (const event of events) {
// ignore space listener
if (event.servertype && event.servertype != this.serverType) { continue; }
// here must bind this to pluginInstance // here must bind this to pluginInstance
this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance)); this.EventManager.listen(pluginInstance, event.name, pluginInstance[event.executor].bind(pluginInstance));
} }