summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/src/main/expectation.ts
blob: 4141b46ffb5365dd37387a30e601beebdb28c4c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export default class Expectation {
  private fulfilled = false;
  private timeout: NodeJS.Timeout;

  constructor(
    private handler: () => void,
    timeout = 2000,
  ) {
    this.timeout = global.setTimeout(() => {
      this.fulfill();
    }, timeout);
  }

  public fulfill() {
    if (this.fulfilled) {
      return;
    }

    this.fulfilled = true;
    global.clearTimeout(this.timeout);
    this.handler();
  }
}