feat(fe/cron): add cron table
This commit is contained in:
parent
43f6ff61dc
commit
52969eebe6
1 changed files with 40 additions and 0 deletions
40
src/client/web/src/common/cron.ts
Normal file
40
src/client/web/src/common/cron.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { Map } from "immutable";
|
||||
|
||||
export interface CronTask {
|
||||
func: (arg?: any, ...optionalArgs: any[]) => void;
|
||||
delay: number;
|
||||
args?: any[];
|
||||
handler?: number;
|
||||
}
|
||||
|
||||
export class Cron {
|
||||
private tasks: Map<string, CronTask>;
|
||||
constructor() {
|
||||
this.tasks = Map<string, CronTask>();
|
||||
}
|
||||
|
||||
add = (name: string, task: CronTask) => {
|
||||
if (this.tasks.has(name)) {
|
||||
this.delete(name);
|
||||
}
|
||||
|
||||
const handler = window.setInterval(task.func, task.delay, ...task.args);
|
||||
task.handler = handler;
|
||||
this.tasks = this.tasks.set(name, task);
|
||||
};
|
||||
|
||||
delete = (name: string) => {
|
||||
const preTask = this.tasks.get(name);
|
||||
window.clearInterval(preTask.handler);
|
||||
this.tasks = this.tasks.delete(name);
|
||||
};
|
||||
|
||||
getTasks = (): Map<string, CronTask> => {
|
||||
return this.tasks;
|
||||
};
|
||||
}
|
||||
|
||||
const cronTable = new Cron();
|
||||
export const CronTable = (): Cron => {
|
||||
return cronTable;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue