ms/packages/api/src/task.ts

83 lines
2.0 KiB
TypeScript
Raw Normal View History

export namespace task {
export const TaskManager = Symbol('TaskManager')
export interface TaskManager {
/**
*
* @param func
*/
create(func: Function): task.Task;
/**
* 线
* @param func
*/
callSyncMethod(func: Function): any;
/**
*
*/
disable();
}
/**
*
*/
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;
}
/**
*
* @param isAsync
*/
async(isAsync: boolean = true): task.Task {
this.isAsync = isAsync;
return this;
}
/**
*
* @param tick Tick
*/
later(tick: number): task.Task {
this.laterTime = tick;
return this;
}
/**
*
* @param tick Tick
*/
timer(tick: number): task.Task {
this.interval = tick;
return this;
}
protected run(...args: any[]): void {
try {
this.func(...args);
} catch (ex) {
console.console('§4插件执行任务时发生错误', ex)
console.ex(ex);
}
}
/**
*
* @param args
*/
abstract submit(...args: any[]): Cancelable;
}
/**
*
*/
export interface Cancelable {
cancel(): boolean;
}
}