From 4ea75d20cae6cb55e013e58678b1a8ef890fb22d Mon Sep 17 00:00:00 2001 From: vinerima Date: Mon, 23 Mar 2026 14:46:30 +0100 Subject: [PATCH] add setParams function --- package.json | 2 +- src/WebSocketClient.ts | 19 +++++++++++++++++++ src/connection/WebSocketConnection.ts | 10 ++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 51304f6..f3f5e48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vinerima/wah", - "version": "2.0.0", + "version": "2.0.1", "description": "Generic WebSocket action handler with Zod-based schema validation and typed message dispatch", "author": "vinerima", "license": "MIT", diff --git a/src/WebSocketClient.ts b/src/WebSocketClient.ts index 44a1bd1..a57d337 100644 --- a/src/WebSocketClient.ts +++ b/src/WebSocketClient.ts @@ -130,6 +130,25 @@ export class WebSocketClient extends Emitter { this.connection.updateParams(params); } + /** + * Merges new query parameters without triggering a reconnection. + * The updated params take effect on the next connection attempt + * (e.g., when the built-in reconnection fires after a disconnect). + * + * @param params - Key-value pairs to merge into the current query parameters. + * + * @example + * ```typescript + * // Update cursor on disconnect so reconnection resumes from last position + * client.on("close", () => { + * client.setParams({ cursor: lastEventId }); + * }); + * ``` + */ + setParams(params: Record): void { + this.connection.setParams(params); + } + /** * Returns a read-only snapshot of the current connection state. */ diff --git a/src/connection/WebSocketConnection.ts b/src/connection/WebSocketConnection.ts index 61d0647..5f2c629 100644 --- a/src/connection/WebSocketConnection.ts +++ b/src/connection/WebSocketConnection.ts @@ -118,6 +118,16 @@ export class WebSocketConnection extends Emitter { this.run(); } + /** + * Merges new query parameters without triggering a reconnection. + * The updated params will take effect on the next connection attempt + * (e.g., after a disconnect triggers the built-in reconnection). + */ + setParams(params: Record): void { + this.queryParams = { ...this.queryParams, ...params }; + this.logger.debug("Query params set (no reconnect)", { params: this.queryParams }); + } + /** * Returns a read-only snapshot of the current connection state. */ -- 2.51.2