Add Executor and RelaySet

This commit is contained in:
Jonathan Staab
2023-03-27 13:39:48 -05:00
parent 4b362e3061
commit 853b42c1c9
9 changed files with 121 additions and 72 deletions
+12 -15
View File
@@ -6,6 +6,11 @@ export class Pool {
relays: Map<string, Relay>
constructor() {
this.relays = new Map()
this.interval = setInterval(() => {
for (const relay of this.relays) {
relay.reconnect()
}
}, 30_000)
}
add(url) {
url = normalizeUrl(url)
@@ -22,6 +27,13 @@ export class Pool {
this.relays.get(url)?.disconnect()
this.relays.delete(url)
}
cleanup() {
this.interval = clearInterval(this.interval)
for (const url of this.relays.keys()) {
this.remove(url)
}
}
async waitFor(url) {
const relay = this.add(url)
@@ -29,19 +41,4 @@ export class Pool {
return relay.status === Relay.STATUS.READY ? relay : null
}
async execute(urls, callback) {
const results = await Promise.all([
urls.map(async url => {
const relay = await this.waitFor(url)
if (!relay) {
return null
}
return [relay, callback(relay)]
}),
])
return results.filter(Boolean)
}
}