Add Relay, Pool

This commit is contained in:
Jonathan Staab
2023-03-25 10:57:23 -05:00
parent 39d001280c
commit daca5adf11
8 changed files with 252 additions and 2171 deletions
+46
View File
@@ -0,0 +1,46 @@
import {Relay} from "./Relay"
const normalizeUrl = url => url.replace(/\/+$/, "").toLowerCase().trim()
export class Pool {
constructor() {
this.relays = new Map()
}
add(url) {
url = normalizeUrl(url)
if (!this.relays.has(url)) {
this.relays.set(url, new Relay(url))
}
return this.relays.get(url)
}
remove(url) {
url = normalizeUrl(url)
this.relays.get(url)?.disconnect()
this.relays.delete(url)
}
async waitFor(url) {
const relay = this.add(url)
await relay.connect()
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)
}
}