refactor: optimize plugin system

This commit is contained in:
2023-07-26 17:35:33 +08:00
parent 002f2c47c6
commit c07f1131c4
13 changed files with 95 additions and 48 deletions

View File

@@ -25,7 +25,7 @@ export namespace event {
public EventPriority = EventPriority;
private mapEvent = [];
private listenerMap = [];
private pluginEventMap = [];
private cacheSlowEventKey = {};
protected baseEventDir = '';
@@ -130,7 +130,7 @@ export namespace event {
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError(i18n.translate("ms.api.event.listen.plugin.name.empty"))
var name = plugin.description.name
var eventCls = this.name2Class(name, event)
if (!eventCls) { return }
if (!eventCls) { return () => { console.warn('event ' + event + ' not found ignore off listener.') } }
if (typeof priority === 'boolean') {
ignoreCancel = priority
priority = EventPriority.NORMAL
@@ -140,16 +140,15 @@ export namespace event {
// @ts-ignore
let executor = exec.name || exec.executor || '[anonymous]'
// noinspection JSUnusedGlobalSymbols
var listener = this.register(
let listener = this.register(
eventCls,
this.createExecute(name, exec, eventCls),
priority,
ignoreCancel
)
var listenerMap = this.listenerMap
// add to cache Be used for close plugin to close event
if (!listenerMap[name]) listenerMap[name] = []
var off = () => {
if (!this.pluginEventMap[name]) this.pluginEventMap[name] = []
let off = () => {
if (off['offed']) return
off['offed'] = true
this.unregister(eventCls, listener)
@@ -159,7 +158,7 @@ export namespace event {
exec: executor
}))
}
listenerMap[name].push(off)
this.pluginEventMap[name].push(off)
// noinspection JSUnresolvedVariable
console.debug(i18n.translate("ms.api.event.register", {
name,
@@ -176,10 +175,10 @@ export namespace event {
* @param plugin 插件
*/
disable(plugin: any) {
var eventCache = this.listenerMap[plugin.description.name]
var eventCache = this.pluginEventMap[plugin.description.name]
if (eventCache) {
eventCache.forEach((off: () => any) => off())
delete this.listenerMap[plugin.description.name]
delete this.pluginEventMap[plugin.description.name]
}
}

View File

@@ -11,6 +11,7 @@ export namespace particle {
abstract spawn(location: any, particle: Particle)
abstract spawnToPlayer(player: any, location: any, particle: Particle)
}
/**
* 表示一个特效对象
*

View File

@@ -1,6 +1,8 @@
import { plugin } from './index'
import { EventEmitter } from 'events'
import { injectable } from '@ccms/container'
import { plugin } from './index'
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
export namespace task {
@@ -79,7 +81,7 @@ export namespace task {
/**
* 任务抽象
*/
export abstract class Task implements Cancelable {
export abstract class Task extends EventEmitter implements Cancelable {
protected func: Function
protected isAsync: boolean = false;
protected laterTime: number = 0;
@@ -88,7 +90,10 @@ export namespace task {
protected taskId: number
protected innerTask: any
private cancelled: boolean = false
constructor(owner: plugin.Plugin, func: Function, id: number) {
super()
this.owner = owner
this.func = func
this.taskId = id
@@ -134,20 +139,35 @@ export namespace task {
*/
cancel(): boolean {
let result = this.cancel0()
process.emit('task.finish', this)
this.finish()
this.cancelled = true
return result
}
protected run(...args: any[]): void {
try {
this.emit('before', this)
if (this.cancelled) { return }
this.func(...args)
!this.interval && process.emit('task.finish', this)
} catch (ex: any) {
console.console('§4插件执行任务时发生错误', ex)
console.ex(ex)
this.emit('after', this)
} catch (error: any) {
this.emit('error', error)
if (!error.processed) {
console.console('§4插件执行任务时发生错误', error)
console.ex(error)
this.cancel()
}
} finally {
this.emit('finally', this)
if (!this.interval && !this.cancelled) { this.finish() }
}
}
protected finish() {
process.emit('task.finish', this)
this.emit('finish', this)
}
/**
* 提交任务
* @param args 任务参数