From 0ddcfdce683aa1cad40e2c87b67c9b9b4a49fe99 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 15 Feb 2023 20:21:27 -0300 Subject: [PATCH] remove "seen" event from Pub. too complicated. if anyone wants this they can do it themselves. --- README.md | 3 --- relay.ts | 41 ++++------------------------------------- 2 files changed, 4 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index dbe685f..77ff217 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,6 @@ let pub = relay.publish(event) pub.on('ok', () => { console.log(`${relay.url} has accepted our event`) }) -pub.on('seen', () => { - console.log(`we saw the event on ${relay.url}`) -}) pub.on('failed', reason => { console.log(`failed to publish to ${relay.url}: ${reason}`) }) diff --git a/relay.ts b/relay.ts index 0073276..78ec75b 100644 --- a/relay.ts +++ b/relay.ts @@ -19,8 +19,8 @@ export type Relay = { off: (type: RelayEvent, cb: any) => void } export type Pub = { - on: (type: 'ok' | 'seen' | 'failed', cb: any) => void - off: (type: 'ok' | 'seen' | 'failed', cb: any) => void + on: (type: 'ok' | 'failed', cb: any) => void + off: (type: 'ok' | 'failed', cb: any) => void } export type Sub = { sub: (filters: Filter[], opts: SubscriptionOptions) => Sub @@ -265,50 +265,17 @@ export function relayInit(url: string): Relay { if (!event.id) throw new Error(`event ${event} has no id`) let id = event.id - var sent = false - var mustMonitor = false - trySend(['EVENT', event]) - .then(() => { - sent = true - if (mustMonitor) { - startMonitoring() - mustMonitor = false - } - }) - .catch(() => {}) - - const startMonitoring = () => { - let monitor = sub([{ids: [id]}], { - id: `monitor-${id.slice(0, 5)}` - }) - let willUnsub = setTimeout(() => { - ;(pubListeners[id]?.failed || []).forEach(cb => - cb('event not seen after 5 seconds') - ) - monitor.unsub() - }, 5000) - monitor.on('event', () => { - clearTimeout(willUnsub) - ;(pubListeners[id]?.seen || []).forEach(cb => cb()) - }) - } return { - on: (type: 'ok' | 'seen' | 'failed', cb: any) => { + on: (type: 'ok' | 'failed', cb: any) => { pubListeners[id] = pubListeners[id] || { ok: [], - seen: [], failed: [] } pubListeners[id][type].push(cb) - - if (type === 'seen') { - if (sent) startMonitoring() - else mustMonitor = true - } }, - off: (type: 'ok' | 'seen' | 'failed', cb: any) => { + off: (type: 'ok' | 'failed', cb: any) => { let listeners = pubListeners[id] if (!listeners) return let idx = listeners[type].indexOf(cb)