From 9a52a61589a535ca051423d009bea316510f4e1d Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Mon, 13 Jan 2025 11:27:14 -0600 Subject: [PATCH] add disconnectTimeout option to rx-nostr config --- package-lock.json | 4 ++-- packages/core/src/config/config.ts | 8 ++++++++ packages/core/src/connection/connection.ts | 20 ++++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 005b030..34dd3d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6334,7 +6334,7 @@ }, "packages/core": { "name": "rx-nostr", - "version": "3.4.1", + "version": "3.4.2", "license": "MIT", "dependencies": { "nostr-typedef": "^0.9.0", @@ -7113,7 +7113,7 @@ }, "packages/crypto": { "name": "rx-nostr-crypto", - "version": "3.1.2", + "version": "3.1.3", "license": "MIT", "dependencies": { "@noble/curves": "^1.0.0", diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index ed07c51..d0aea84 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -13,6 +13,7 @@ export const makeRxNostrConfig = (config: RxNostrConfig) => maxCount: 5, initialDelay: 1000, }, + disconnectTimeout: 10_000, eoseTimeout: 30 * 1000, okTimeout: 30 * 1000, authTimeout: 30 * 1000, @@ -48,6 +49,13 @@ export interface RxNostrConfig { * Auto reconnection strategy. */ retry?: RetryConfig; + + /** + * How long a relay connection should be held open when no longer used + * @default 5000 + */ + disconnectTimeout?: number, + /** * Specify how long rx-nostr waits for EOSE messages in `use()` following backward strategy (milliseconds). * diff --git a/packages/core/src/connection/connection.ts b/packages/core/src/connection/connection.ts index 1202b7d..d697da7 100644 --- a/packages/core/src/connection/connection.ts +++ b/packages/core/src/connection/connection.ts @@ -43,6 +43,8 @@ export class NostrConnection { private defaultSubscriptionIds: Set = new Set(); private communicating = false; private strategy: ConnectionStrategy = "lazy"; + private disconnectTimeout: number + private disconnectTimer?: ReturnType private isDefaultRelay = false; private disposed = false; private _url: string; @@ -65,6 +67,8 @@ export class NostrConnection { this.pubProxy = pubProxy; this.subProxy = subProxy; + this.disconnectTimeout = config.disconnectTimeout + // Idling cold sockets combineLatest([ this.pubProxy.getLogicalConnectionSizeObservable(), @@ -94,9 +98,18 @@ export class NostrConnection { switch (strategy) { case "lazy": { - if (!this.communicating) { - this.relay.disconnect(WebSocketCloseCode.RX_NOSTR_IDLE); + // clear existing timer + if(this.disconnectTimeout) { + clearTimeout(this.disconnectTimer) + this.disconnectTimer=undefined } + + // create a new timer + this.disconnectTimer = setTimeout(() => { + if (!this.communicating) { + this.relay.disconnect(WebSocketCloseCode.RX_NOSTR_IDLE); + } + }, this.disconnectTimeout); break; } case "lazy-keep": { @@ -254,6 +267,9 @@ export class NostrConnection { this.disposed = true; + if(this.disconnectTimer) clearTimeout(this.disconnectTimer) + this.disconnectTimer=undefined + this.relay.dispose(); this.pubProxy.dispose(); this.subProxy.dispose();