when in pool, automatically and efficiently deduplicate.

This commit is contained in:
fiatjaf
2023-02-08 09:46:05 -03:00
parent cc8e34163d
commit da59e3ce90
3 changed files with 96 additions and 1 deletions

11
pool.ts
View File

@@ -1,5 +1,8 @@
import {Relay, relayInit} from './relay'
import {normalizeURL} from './utils'
import {Filter} from './filter'
import {Event} from './event'
import {SubscriptionOptions, Sub} from './relay'
export function pool(defaultRelays: string[] = []) {
return new SimplePool(defaultRelays)
@@ -22,6 +25,14 @@ class SimplePool {
const hasEventId = (id: string): boolean => this._knownIds.has(id)
const relay = relayInit(nm, hasEventId)
this._conn[nm] = relay
let sub = relay.sub
relay.sub = (filters: Filter[], opts?: SubscriptionOptions): Sub => {
let s = sub(filters, opts)
s.on('event', (event: Event) => this._knownIds.add(event.id as string))
return s
}
return relay
}
}