more automatic cleanup of event listeners.

This commit is contained in:
fiatjaf 2023-02-15 20:36:22 -03:00
parent 6dbcc87d93
commit 24406b5679
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
1 changed files with 10 additions and 3 deletions

View File

@ -135,15 +135,22 @@ export function relayInit(url: string): Relay {
return
case 'EOSE': {
let id = data[1]
;(subListeners[id]?.eose || []).forEach(cb => cb())
if (id in subListeners) {
subListeners[id].eose.forEach(cb => cb())
subListeners[id].eose = [] // 'eose' only happens once per sub, so stop listeners here
}
return
}
case 'OK': {
let id: string = data[1]
let ok: boolean = data[2]
let reason: string = data[3] || ''
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
else pubListeners[id]?.failed.forEach(cb => cb(reason))
if (id in pubListeners) {
if (ok) pubListeners[id].ok.forEach(cb => cb())
else pubListeners[id].failed.forEach(cb => cb(reason))
pubListeners[id].ok = [] // 'ok' only happens once per pub, so stop listeners here
pubListeners[id].failed = []
}
return
}
case 'NOTICE':