blob: 8ae5a2cbf0c8e8538bd489ba8465cf1424ceae31 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import { useEffect, useMemo } from 'react';
export class Scheduler {
private timer?: NodeJS.Timeout;
private running = false;
public schedule(action: () => void, delay = 0) {
this.cancel();
this.running = true;
this.timer = global.setTimeout(() => {
this.running = false;
action();
}, delay);
}
public cancel() {
if (this.timer) {
clearTimeout(this.timer);
this.running = false;
}
}
public get isRunning() {
return this.running;
}
}
export function useScheduler() {
const closeScheduler = useMemo(() => new Scheduler(), []);
useEffect(() => {
return () => closeScheduler.cancel();
}, []);
return closeScheduler;
}
|