Files
welshman/lib/Pool.ts
T
Jonathan Staab daca5adf11 Add Relay, Pool
2023-03-25 11:15:03 -05:00

47 lines
906 B
TypeScript

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)
}
}