File size: 3,471 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
export type AbortInstance = {
  signal: AbortSignal;
  timesOutAt?: Date;
  tier: "external" | "scrape" | "engine";
  throwable: () => any;
};

export class AbortManager {
  private aborts: AbortInstance[] = [];
  private mappedController: AbortController | null = null;
  private listeners: Array<{ signal: AbortSignal; handler: () => void }> = [];

  constructor(...instances: (AbortInstance | undefined | null)[]) {
    this.aborts = instances.filter(
      x => x !== undefined && x !== null,
    ) as AbortInstance[];
  }

  dispose() {
    for (const { signal, handler } of this.listeners) {
      signal.removeEventListener("abort", handler);
    }

    this.listeners = [];
    this.aborts = [];
    this.mappedController = null;
  }

  private resolveInner(abort: AbortInstance): any {
    try {
      return abort.throwable();
    } catch (err) {
      return err;
    }
  }

  private register(abort: AbortInstance) {
    const handler = () => {
      if (!this.mappedController) return;

      const inner = this.resolveInner(abort);
      const reason = new AbortManagerThrownError(abort.tier, inner);

      this.mappedController.abort(reason);
    };

    abort.signal.addEventListener("abort", handler);
    this.listeners.push({ signal: abort.signal, handler });
  }

  add(...instances: (AbortInstance | undefined | null)[]) {
    const pureInstances = instances.filter(
      x => x !== undefined && x !== null,
    ) as AbortInstance[];
    this.aborts.push(...pureInstances);

    if (this.mappedController !== null) {
      for (const abort of pureInstances) {
        this.register(abort);
      }
    }
  }

  child(...instances: (AbortInstance | undefined | null)[]): AbortManager {
    const manager = new AbortManager(
      ...this.aborts,
      ...(instances.filter(
        x => x !== undefined && x !== null,
      ) as AbortInstance[]),
    );
    return manager;
  }

  isAborted(): boolean {
    return this.aborts.some(x => x.signal.aborted);
  }

  throwIfAborted(): void {
    for (const abort of this.aborts) {
      if (abort.signal.aborted) {
        const inner = this.resolveInner(abort);
        throw new AbortManagerThrownError(abort.tier, inner);
      }
    }
  }

  private _mapController() {
    this.mappedController = new AbortController();

    for (const abort of this.aborts) {
      this.register(abort);
    }
  }

  asSignal(): AbortSignal {
    if (this.mappedController === null) {
      this._mapController();
    }

    return this.mappedController!.signal;
  }

  scrapeTimeout(): number | undefined {
    const timeouts = this.aborts
      .filter(x => x.tier === "scrape")
      .map(x => x.timesOutAt)
      .filter(x => x !== undefined);
    if (timeouts.length === 0) {
      return undefined;
    }
    return Math.min(...timeouts.map(x => x.getTime())) - Date.now();
  }

  engineNearestTimeout(): number | undefined {
    const timeouts = this.aborts
      .filter(x => x.tier === "engine")
      .map(x => x.timesOutAt)
      .filter(x => x !== undefined);
    if (timeouts.length === 0) {
      return undefined;
    }
    return Math.min(...timeouts.map(x => x.getTime())) - Date.now();
  }
}

export class AbortManagerThrownError extends Error {
  name = "AbortManagerThrownError";
  public tier: AbortInstance["tier"];
  public inner: any;
  constructor(tier: AbortInstance["tier"], inner: any) {
    super("AbortManagerThrownError: " + tier);
    this.tier = tier;
    this.inner = inner;
  }
}