most simple relay pool.

This commit is contained in:
fiatjaf
2023-02-08 08:17:12 -03:00
parent 9082953ede
commit cc8e34163d
5 changed files with 60 additions and 2 deletions

27
pool.ts Normal file
View File

@@ -0,0 +1,27 @@
import {Relay, relayInit} from './relay'
import {normalizeURL} from './utils'
export function pool(defaultRelays: string[] = []) {
return new SimplePool(defaultRelays)
}
class SimplePool {
private _conn: {[url: string]: Relay}
private _knownIds: Set<string> = new Set()
constructor(defaultRelays: string[]) {
this._conn = {}
defaultRelays.forEach(this.ensureRelay)
}
ensureRelay(url: string): Relay {
const nm = normalizeURL(url)
const existing = this._conn[nm]
if (existing) return existing
const hasEventId = (id: string): boolean => this._knownIds.has(id)
const relay = relayInit(nm, hasEventId)
this._conn[nm] = relay
return relay
}
}