Add Executor and RelaySet
This commit is contained in:
+12
-15
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user