more automatic cleanup of event listeners.

This commit is contained in:
fiatjaf
2023-02-15 20:36:22 -03:00
parent 6dbcc87d93
commit 24406b5679

View File

@@ -135,15 +135,22 @@ export function relayInit(url: string): Relay {
return return
case 'EOSE': { case 'EOSE': {
let id = data[1] 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 return
} }
case 'OK': { case 'OK': {
let id: string = data[1] let id: string = data[1]
let ok: boolean = data[2] let ok: boolean = data[2]
let reason: string = data[3] || '' let reason: string = data[3] || ''
if (ok) pubListeners[id]?.ok.forEach(cb => cb()) if (id in pubListeners) {
else pubListeners[id]?.failed.forEach(cb => cb(reason)) 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 return
} }
case 'NOTICE': case 'NOTICE':