2019-09-21 06:58:00 +00:00
|
|
|
export namespace task {
|
|
|
|
export const TaskManager = Symbol('TaskManager')
|
|
|
|
export interface TaskManager {
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 创建任务
|
|
|
|
* @param func 任务内容
|
|
|
|
*/
|
2019-09-21 06:58:00 +00:00
|
|
|
create(func: Function): task.Task;
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 在主线程执行代码
|
|
|
|
* @param func 执行内容
|
|
|
|
*/
|
2020-01-17 03:08:51 +00:00
|
|
|
callSyncMethod(func: Function): any;
|
2019-09-21 06:58:00 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 任务抽象
|
|
|
|
*/
|
|
|
|
export abstract class Task {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 设置任务异步执行
|
|
|
|
* @param isAsync 是否异步
|
|
|
|
*/
|
2019-09-21 06:58:00 +00:00
|
|
|
async(isAsync: boolean = true): task.Task {
|
|
|
|
this.isAsync = isAsync;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 设置任务延时执行
|
|
|
|
* @param tick 延时 Tick
|
|
|
|
*/
|
2019-09-21 06:58:00 +00:00
|
|
|
later(tick: number): task.Task {
|
|
|
|
this.laterTime = tick;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 设置任务循环执行
|
|
|
|
* @param tick 循环时间 Tick
|
|
|
|
*/
|
2019-09-21 06:58:00 +00:00
|
|
|
timer(tick: number): task.Task {
|
|
|
|
this.interval = tick;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected run(): void {
|
|
|
|
try {
|
|
|
|
this.func();
|
|
|
|
} catch (ex) {
|
|
|
|
console.console('§4插件执行任务时发生错误', ex)
|
|
|
|
console.ex(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 提交任务
|
|
|
|
*/
|
2019-09-21 06:58:00 +00:00
|
|
|
abstract submit(): Cancelable;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 返可取消的对象
|
|
|
|
*/
|
|
|
|
export interface Cancelable {
|
|
|
|
cancel(): boolean;
|
|
|
|
}
|
|
|
|
}
|