forked from ipfs/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChecker.ts
44 lines (38 loc) · 1.25 KB
/
Checker.ts
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
import { GatewayNode } from './GatewayNode'
import { Results } from './Results'
import { Stats } from './Stats'
import { Log } from './Log'
const log = new Log('Checker')
class Checker {
public readonly element: HTMLElement
public readonly nodes: GatewayNode[] = []
private readonly stats: Stats
private readonly results: Results
constructor () {
const element = document.getElementById('checker')
if (element == null) {
throw new Error('Element with Id "checker" not found.')
}
this.element = element
this.stats = new Stats(this)
this.results = new Results(this)
this.updateStats = this.updateStats.bind(this)
}
private updateStats (): void {
this.stats.update()
}
async checkGateways (gateways: string[]): Promise<void> {
const allChecks: Array<Promise<void>> = []
for (const gateway of gateways) {
const node = new GatewayNode(this.results, gateway, this.nodes.length)
this.nodes.push(node)
this.results.append(node.tag)
// void node.check()
setTimeout(() => {
allChecks.push(node.check().catch((err) => log.error(err)).finally(this.updateStats))
}, 100 * this.nodes.length)
}
// await Promise.all(allChecks).finally(this.updateStats)
}
}
export { Checker }