From c93d102c080c9aca6fba48a9ac19c328c0e347e3 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Wed, 30 Oct 2024 13:26:04 -0700 Subject: [PATCH] Wait for disconnect before removing listeners on connection --- packages/net/package.json | 2 +- packages/net/src/Connection.ts | 10 ++++++---- packages/net/src/ConnectionAuth.ts | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/net/package.json b/packages/net/package.json index 3e9c333..192b0d6 100644 --- a/packages/net/package.json +++ b/packages/net/package.json @@ -1,6 +1,6 @@ { "name": "@welshman/net", - "version": "0.0.30", + "version": "0.0.31", "author": "hodlbod", "license": "MIT", "description": "Utilities for connecting with nostr relays.", diff --git a/packages/net/src/Connection.ts b/packages/net/src/Connection.ts index af2f95d..8d7d97c 100644 --- a/packages/net/src/Connection.ts +++ b/packages/net/src/Connection.ts @@ -117,15 +117,17 @@ export class Connection extends Emitter { } } - disconnect() { - this.socket.disconnect() + async disconnect() { + await this.socket.disconnect() + this.sender.clear() this.receiver.clear() this.meta.clearPending() } - destroy() { - this.disconnect() + async destroy() { + await this.disconnect() + this.removeAllListeners() this.sender.stop() this.receiver.stop() diff --git a/packages/net/src/ConnectionAuth.ts b/packages/net/src/ConnectionAuth.ts index 3c7a137..3a7568d 100644 --- a/packages/net/src/ConnectionAuth.ts +++ b/packages/net/src/ConnectionAuth.ts @@ -115,10 +115,12 @@ export class ConnectionAuth { while (Date.now() < deadline) { await sleep(100) + // State got reset while we were waiting 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)) { break }