mirror of
https://github.com/SikongJueluo/cc-utils.git
synced 2025-11-05 03:37:50 +08:00
feature: global timer manager; reconstruct: accesscontrol; fix: chat manager
feature: - add global timer manager reconstruct: - use new cli framework for accesscontrol - use chat manager for accesscontrol fix: - chat manager only send one time
This commit is contained in:
36
src/lib/TimerManager.ts
Normal file
36
src/lib/TimerManager.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { pullEventAs, TimerEvent } from "./event";
|
||||
import { Result, Ok, Err, Option, Some, None } from "./thirdparty/ts-result-es";
|
||||
|
||||
class TimerManager {
|
||||
private isRunning = false;
|
||||
|
||||
private timerTaskMap = new Map<number, () => void>();
|
||||
|
||||
// Don't put heavy logic on callback function
|
||||
public setTimeOut(delay: number, callback: () => void): void {
|
||||
const timerId = os.startTimer(delay);
|
||||
this.timerTaskMap.set(timerId, callback);
|
||||
}
|
||||
|
||||
public run() {
|
||||
this.isRunning = true;
|
||||
while (this.isRunning) {
|
||||
const event = pullEventAs(TimerEvent, "timer");
|
||||
if (event === undefined) continue;
|
||||
|
||||
const task = this.timerTaskMap.get(event.id);
|
||||
if (task === undefined) continue;
|
||||
task();
|
||||
}
|
||||
}
|
||||
|
||||
public stop() {
|
||||
this.isRunning = false;
|
||||
}
|
||||
|
||||
public status(): boolean {
|
||||
return this.isRunning;
|
||||
}
|
||||
}
|
||||
|
||||
export const gTimerManager = new TimerManager();
|
||||
Reference in New Issue
Block a user