MCPcopy Create free account
hub / github.com/uiwjs/react-codemirror / TimeoutLatch

Class TimeoutLatch

core/src/timeoutLatch.ts:4–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2// Inspired from https://github.com/iostreamer-X/timeout-latch
3
4export class TimeoutLatch {
5 private timeLeftMS: number;
6 private timeoutMS: number;
7 private isCancelled = false;
8 private isTimeExhausted = false;
9 private callbacks: Function[] = [];
10
11 constructor(callback: Function, timeoutMS: number) {
12 this.timeLeftMS = timeoutMS;
13 this.timeoutMS = timeoutMS;
14 this.callbacks.push(callback);
15 }
16
17 tick(): void {
18 if (!this.isCancelled && !this.isTimeExhausted) {
19 this.timeLeftMS--;
20 if (this.timeLeftMS <= 0) {
21 this.isTimeExhausted = true;
22 const callbacks = this.callbacks.slice();
23 this.callbacks.length = 0;
24 callbacks.forEach((callback) => {
25 try {
26 callback();
27 } catch (error) {
28 console.error('TimeoutLatch callback error:', error);
29 }
30 });
31 }
32 }
33 }
34
35 cancel(): void {
36 this.isCancelled = true;
37 this.callbacks.length = 0;
38 }
39
40 reset(): void {
41 this.timeLeftMS = this.timeoutMS;
42 this.isCancelled = false;
43 this.isTimeExhausted = false;
44 }
45
46 get isDone(): boolean {
47 return this.isCancelled || this.isTimeExhausted;
48 }
49}
50
51class Scheduler {
52 private interval: NodeJS.Timeout | null = null;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…