From b55e662f812a59589f9c5a91be999759aa87e5ea Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 2 Apr 2022 16:51:08 +0800 Subject: [PATCH] feat: add more event emit Signed-off-by: MiaoWoo --- packages/core/src/index.ts | 13 +++++++++++++ packages/plugin/src/manager.ts | 13 ++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 64a27529..2ed3b788 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -17,14 +17,18 @@ class MiaoScriptCore { private pluginManager: plugin.PluginManager enable() { + process.emit('core.before.enable') this.loadServerConsole() this.loadPlugins() + process.emit('core.after.enable') return () => this.disable() } loadServerConsole() { + process.emit('core.before.load.console') //@ts-ignore global.setGlobal('console', new this.Console(), { writable: false, configurable: false }) + process.emit('core.after.load.console') } loadPlugins() { @@ -81,6 +85,7 @@ function detectServer(): constants.ServerType { } function initialize() { + process.emit('core.before.initialize') global.ScriptSlowExecuteTime = 30 global.ScriptEngineVersion = require('../package.json').version try { @@ -97,21 +102,29 @@ function initialize() { container.bind(plugin.PluginInstance).toConstantValue(base.getInstance()) container.bind(plugin.PluginFolder).toConstantValue('plugins') let type = detectServer() + + process.emit('core.before.initialize.detect') console.i18n("ms.core.initialize.detect", { scope: global.scope, type }) container.bind(server.ServerType).toConstantValue(type) container.bind(server.ServerChecker).toSelf().inSingletonScope() container.bind(server.NativePluginManager).toSelf().inSingletonScope() + process.emit('core.after.initialize.detect') + + process.emit('core.before.package.initialize') console.i18n("ms.core.package.initialize", { scope: global.scope, type }) require(`${global.scope}/${type}`).default(container) require(`${global.scope}/plugin`) container.load(buildProviderModule()) console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 }) + process.emit('core.after.package.initialize') + let disable = container.get(MiaoScriptCore).enable() console.i18n("ms.core.engine.completed", { loader: base.version, version: 'v' + global.ScriptEngineVersion, time: (Date.now() - global.ScriptEngineStartTime) / 1000 }) + process.emit('core.after.initialize') return disable } catch (error: any) { console.i18n("ms.core.initialize.error", { error }) diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index 88294edb..79e24a47 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -64,6 +64,7 @@ export class PluginManagerImpl implements plugin.PluginManager { initialize() { if (this.pluginInstance === undefined) { throw new Error("Can't found Plugin Instance!") } if (this.initialized !== true) { + process.emit('plugin.manager.before.initialize') console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance, loader: Thread.currentThread().contextClassLoader }) console.i18n('ms.plugin.event.map', { count: this.eventManager.mapEventName(), type: this.serverType }) let pluginScanner = this.container.getAll(plugin.PluginScanner) @@ -77,13 +78,14 @@ export class PluginManagerImpl implements plugin.PluginManager { this.loaderMap.set(loader.type, loader) }) this.initialized = true - process.emit('plugin.initialize') + process.emit('plugin.manager.after.initialize') } } scan(folder: string): void { if (!folder) { throw new Error('plugin scan folder can\'t be empty!') } this.initialize() + process.emit('plugin.manager.before.scan', folder) for (const [, scanner] of this.sacnnerMap) { try { console.i18n('ms.plugin.manager.scan', { scanner: scanner.type, folder }) @@ -102,12 +104,13 @@ export class PluginManagerImpl implements plugin.PluginManager { console.ex(error) } } - process.emit('plugin.scan', folder) + process.emit('plugin.manager.after.scan', folder) } build(): void { + process.emit('plugin.manager.before.build') this.buildPlugins() - process.emit('plugin.build') + process.emit('plugin.manager.after.build') } private logStage(plugin: plugin.Plugin, stage: string) { @@ -132,6 +135,7 @@ export class PluginManagerImpl implements plugin.PluginManager { private loadPlugin(loadMetadata: plugin.PluginLoadMetadata) { if (!loadMetadata) { throw new Error('loadMetadata can\'t be undefiend when loadPlugin!') } if (loadMetadata.loaded) { throw new Error(`Plugin file ${loadMetadata.file} is already loaded by ${loadMetadata.loader?.type}!`) } + process.emit(`plugin.before.require`, loadMetadata) try { for (const [, loader] of this.loaderMap) { if (this.loaderRequirePlugin(loadMetadata, loader)?.loaded) return loadMetadata.metadata @@ -154,6 +158,7 @@ export class PluginManagerImpl implements plugin.PluginManager { } this.metadataMap.set(metadata.name, metadata) metadata.loadMetadata = loadMetadata + process.emit(`plugin.after.require`, loadMetadata) } return loadMetadata } catch (error: any) { @@ -269,6 +274,7 @@ export class PluginManagerImpl implements plugin.PluginManager { return loseDepends } private buildPlugin(metadata: plugin.PluginMetadata) { + process.emit(`plugin.before.build`, metadata) try { if (this.instanceMap.has(metadata.name)) { throw new Error(`Plugin ${metadata.name} is already load from ${metadata.source}...`) } if (!this.loaderMap.has(metadata.type)) { throw new Error(`§4无法加载插件 §b${metadata.name} §4请检查 §c${metadata.type} §4加载器是否正常启用!`) } @@ -280,6 +286,7 @@ export class PluginManagerImpl implements plugin.PluginManager { let pluginInstance = this.loaderMap.get(metadata.type).build(metadata) if (!pluginInstance) { throw new Error(`§4加载器 §c${metadata.type} §4加载插件 §c${metadata.name} §4失败!`) } this.instanceMap.set(metadata.name, pluginInstance) + process.emit(`plugin.after.build`, metadata, pluginInstance) return pluginInstance } catch (error: any) { console.console(`§4无法加载插件 §b${metadata.name} §4构建插件失败!`)