| 2 | // Inspired from https://github.com/iostreamer-X/timeout-latch |
| 3 | |
| 4 | export 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 | |
| 51 | class Scheduler { |
| 52 | private interval: NodeJS.Timeout | null = null; |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…