Skip to content

Send files directly between devices over the internet - no cloud storage, no size limits.

646 B
Raw Blame
export class Timeout {
  private readonly ms: number
  private readonly onExpire: () => void
  private timer: ReturnType<typeof setTimeout> | null = null

  constructor(ms: number, onExpire: () => void) {
    this.ms = ms
    this.onExpire = onExpire
  }

  start(): void {
    if (this.timer) return
    this.schedule()
  }

  restart(): void {
    this.stop()
    this.schedule()
  }

  stop(): void {
    if (!this.timer) return
    clearTimeout(this.timer)
    this.timer = null
  }

  private schedule(): void {
    this.timer = setTimeout(() => {
      this.timer = null
      this.onExpire()
    }, this.ms)
    this.timer.unref?.()
  }
}