From 1e270b52aaf16f1846db378777d0c524a182e531 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 1 Mar 2023 16:39:18 -0300 Subject: [PATCH] optimize deduplication. --- src/agent/pool.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/agent/pool.ts b/src/agent/pool.ts index 3995f91d..37cb718b 100644 --- a/src/agent/pool.ts +++ b/src/agent/pool.ts @@ -263,17 +263,20 @@ const subscribe = async ( return } - const sub = conn.nostr.sub(filter, {id}) + const sub = conn.nostr.sub(filter, { + id, + alreadyHaveEvent: (id) => { + conn.stats.eventsCount += 1 + let has = false + if (seen.has(id)) has = true + seen.add(id) + return has + } + }) sub.on('event', e => { - conn.stats.eventsCount += 1 - - if (!seen.has(e.id)) { - seen.add(e.id) - // Normalize events here, annotate with relay url onEvent({...e, seen_on: relay.url, content: e.content || ''}) - } }) sub.on('eose', () => {