-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patheditInterval.ts
More file actions
37 lines (37 loc) · 919 Bytes
/
editInterval.ts
File metadata and controls
37 lines (37 loc) · 919 Bytes
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
export function setEditInterval(
cb: () => Promise<void>,
ms = 3000,
manual = false,
): {end: () => void, trigger: () => void} {
let running = false;
let shouldQueue = false;
const makeTimeout = () => () => (
(async () => {
nextTimeout = undefined;
running = true;
await cb();
running = false;
if (shouldQueue || (!nextTimeout && !manual)) {
shouldQueue = false;
nextTimeout = setTimeout(makeTimeout(), ms);
}
})().catch(e => {
console.log(
"edit message perr (this will stop message editing, even manually triggered)",
);
nextTimeout = undefined;
}),
undefined
);
let nextTimeout: NodeJS.Timeout | undefined = setTimeout(makeTimeout(), 0);
return {
end: () => {
if (nextTimeout) clearTimeout(nextTimeout);
nextTimeout = undefined;
},
trigger: () => {
if (running) shouldQueue = true;
else nextTimeout = setTimeout(makeTimeout(), 0);
},
};
}