get rid of RelayTrackingPool, merge it into SimplePool.

This commit is contained in:
fiatjaf 2023-12-17 19:15:27 -03:00
parent 2a7fd83be8
commit a0cb2eecae
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
1 changed files with 13 additions and 17 deletions

30
pool.ts
View File

@ -11,6 +11,8 @@ export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
export class SimplePool {
private relays = new Map<string, Relay>()
public seenOn = new Map<string, Set<Relay>>()
public trackRelays: boolean = false
async ensureRelay(url: string): Promise<Relay> {
url = normalizeURL(url)
@ -29,6 +31,17 @@ export class SimplePool {
filters: Filter[],
params: SubscribeManyParams,
): Promise<{ close: () => void }> {
if (this.trackRelays) {
params.receivedEvent = (relay: Relay, id: string) => {
let set = this.seenOn.get(id)
if (!set) {
set = new Set()
this.seenOn.set(id, set)
}
set.add(relay)
}
}
const _knownIds = new Set<string>()
params.alreadyHaveEvent = (id: string) => {
if (params.alreadyHaveEvent?.(id)) {
@ -148,20 +161,3 @@ export class SimplePool {
})
}
}
export class RelayTrackingPool extends SimplePool {
public seenOn = new Map<string, Set<Relay>>()
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): Promise<{ close: () => void }> {
params.receivedEvent = (relay: Relay, id: string) => {
let set = this.seenOn.get(id)
if (!set) {
set = new Set()
this.seenOn.set(id, set)
}
set.add(relay)
}
return super.subscribeMany(relays, filters, params)
}
}