Wait for disconnect before removing listeners on connection

This commit is contained in:
Jon Staab
2024-10-30 13:26:04 -07:00
parent da3379bca3
commit c93d102c08
3 changed files with 10 additions and 6 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@welshman/net", "name": "@welshman/net",
"version": "0.0.30", "version": "0.0.31",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
"description": "Utilities for connecting with nostr relays.", "description": "Utilities for connecting with nostr relays.",
+6 -4
View File
@@ -117,15 +117,17 @@ export class Connection extends Emitter {
} }
} }
disconnect() { async disconnect() {
this.socket.disconnect() await this.socket.disconnect()
this.sender.clear() this.sender.clear()
this.receiver.clear() this.receiver.clear()
this.meta.clearPending() this.meta.clearPending()
} }
destroy() { async destroy() {
this.disconnect() await this.disconnect()
this.removeAllListeners() this.removeAllListeners()
this.sender.stop() this.sender.stop()
this.receiver.stop() this.receiver.stop()
+3 -1
View File
@@ -115,10 +115,12 @@ export class ConnectionAuth {
while (Date.now() < deadline) { while (Date.now() < deadline) {
await sleep(100) await sleep(100)
// State got reset while we were waiting
if ([None, Requested].includes(this.status)) { if ([None, Requested].includes(this.status)) {
throw new Error("Auth flow reset while waiting for auth") break
} }
// We've completed the auth flow
if ([DeniedSignature, Forbidden, Ok].includes(this.status)) { if ([DeniedSignature, Forbidden, Ok].includes(this.status)) {
break break
} }