Skip to content

Commit 53c0a0e

Browse files
committed
prevent duplicate event emissions on flaky connections with multiple sequential reconnect attempts
1 parent 3442918 commit 53c0a0e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/xrpl/HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr
55
## Unreleased
66

77
### Fixed
8-
* Fix event listener accumulation bug where `'connected'` event handlers would fire multiple times after each reconnection. Changed `.on()` to `.once()` in the Client constructor's reconnect handler to prevent listener accumulation.
8+
* Fix event listener accumulation bug where `'connected'` event handlers would fire multiple times after each reconnection. The fix cleans up stale listeners from previous reconnect attempts to prevent duplicate event emissions on flaky connections with multiple sequential reconnect attempts.
99

1010
## 4.6.0 (2026-02-12)
1111

packages/xrpl/src/client/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,15 @@ class Client extends EventEmitter<EventTypes> {
262262
this.emit('error', errorCode, errorMessage, data)
263263
})
264264

265+
let connectedListener: (() => void) | undefined
265266
this.connection.on('reconnect', () => {
266-
this.connection.once('connected', () => this.emit('connected'))
267+
// Clean up any stale listener from previous reconnect attempt
268+
if (connectedListener !== undefined) {
269+
this.connection.off('connected', connectedListener)
270+
}
271+
272+
connectedListener = (): boolean => this.emit('connected')
273+
this.connection.once('connected', connectedListener)
267274
})
268275

269276
this.connection.on('disconnected', (code: number) => {

0 commit comments

Comments
 (0)