mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
most simple relay pool.
This commit is contained in:
27
pool.ts
Normal file
27
pool.ts
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user