From 52969eebe6e6a73536e21638aa5eb4e3699ba1d6 Mon Sep 17 00:00:00 2001 From: hexxa Date: Sat, 12 Feb 2022 11:03:00 +0800 Subject: [PATCH] feat(fe/cron): add cron table --- src/client/web/src/common/cron.ts | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/client/web/src/common/cron.ts diff --git a/src/client/web/src/common/cron.ts b/src/client/web/src/common/cron.ts new file mode 100644 index 0000000..d8d0f9a --- /dev/null +++ b/src/client/web/src/common/cron.ts @@ -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; + constructor() { + this.tasks = Map(); + } + + 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 => { + return this.tasks; + }; +} + +const cronTable = new Cron(); +export const CronTable = (): Cron => { + return cronTable; +};