feat: optimize task impl

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-09-22 18:42:39 +08:00
parent e7077b1315
commit b87fb08d1c
9 changed files with 167 additions and 90 deletions

View File

@ -1,34 +1,62 @@
import { plugin } from './index'
import { injectable } from '@ccms/container'
export namespace task {
export const TaskManager = Symbol('TaskManager')
export interface TaskManager {
@injectable()
export abstract class TaskManager {
protected cacheTasks = new Map<string, task.Task[]>()
protected pluginCreate(plugin: plugin.Plugin, task: task.Task) {
if (!this.cacheTasks.has(plugin.description.name)) {
this.cacheTasks.set(plugin.description.name, [])
}
this.cacheTasks.get(plugin.description.name).push(task)
return task
}
protected pluginDisable(plugin: plugin.Plugin) {
if (this.cacheTasks.has(plugin.description.name)) {
this.cacheTasks.get(plugin.description.name).forEach(task => task.cancel())
this.cacheTasks.delete(plugin.description.name)
}
}
/**
* 创建任务
* @param func 任务内容
*/
create(func: Function): task.Task;
create(func: Function, plugin?: plugin.Plugin): task.Task {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !') };
let task = this.create0(func)
if (plugin) { return this.pluginCreate(plugin, task) }
return task
}
abstract create0(func: Function): task.Task
/**
* 在主线程执行代码
* @param func 执行内容
*/
callSyncMethod(func: Function): any;
abstract callSyncMethod(func: Function): any
/**
* 关闭任务管理器
*/
disable();
disable(plugin?: plugin.Plugin) {
if (plugin) { return this.pluginDisable(plugin) }
this.disable0()
}
abstract disable0()
}
/**
* 任务抽象
*/
export abstract class Task {
protected plugin: any;
protected func: Function;
protected plugin: any
protected func: Function
protected isAsync: boolean = false;
protected laterTime: number = 0;
protected interval: number = 0;
constructor(plugin: any, func: Function) {
this.plugin = plugin;
this.func = func;
this.plugin = plugin
this.func = func
}
/**
@ -36,8 +64,8 @@ export namespace task {
* @param isAsync 是否异步
*/
async(isAsync: boolean = true): task.Task {
this.isAsync = isAsync;
return this;
this.isAsync = isAsync
return this
}
/**
@ -45,8 +73,8 @@ export namespace task {
* @param tick 延时 Tick
*/
later(tick: number): task.Task {
this.laterTime = tick;
return this;
this.laterTime = tick
return this
}
/**
@ -54,16 +82,23 @@ export namespace task {
* @param tick 循环时间 Tick
*/
timer(tick: number): task.Task {
this.interval = tick;
return this;
this.interval = tick
return this
}
/**
* 取消任务
*/
cancel(): boolean {
throw new Error("Method not implemented.")
}
protected run(...args: any[]): void {
try {
this.func(...args);
this.func(...args)
} catch (ex) {
console.console('§4插件执行任务时发生错误', ex)
console.ex(ex);
console.ex(ex)
}
}
@ -71,12 +106,22 @@ export namespace task {
* 提交任务
* @param args 任务参数
*/
abstract submit(...args: any[]): Cancelable;
submit(...args: any[]): Cancelable {
let cancelable = this.submit0(...args)
this.cancel = cancelable.cancel
return cancelable
}
/**
* 提交任务
* @param args 任务参数
*/
abstract submit0(...args: any[]): Cancelable
}
/**
* 返可取消的对象
*/
export interface Cancelable {
cancel(): boolean;
cancel(): boolean
}
}