Skip to content

fix: use ?? for autoReconnect default and add onClose/onError callbacks (closes #43)#44

Open
Sertug17 wants to merge 3 commits into
Polymarket:mainfrom
Sertug17:fix/autoreconnect-false-ignored-and-error-callback
Open

fix: use ?? for autoReconnect default and add onClose/onError callbacks (closes #43)#44
Sertug17 wants to merge 3 commits into
Polymarket:mainfrom
Sertug17:fix/autoreconnect-false-ignored-and-error-callback

Conversation

@Sertug17

@Sertug17 Sertug17 commented Apr 14, 2026

Copy link
Copy Markdown

Problem

Two bugs in RealTimeDataClientArgs initialization:

1. autoReconnect: false silently ignored
args!.autoReconnect || true treats false as falsy, so the client
always reconnects regardless of the user's intent.

2. No onClose / onError callbacks
Callers have no way to react to connection drops or errors from
application code. This is the root cause of #26 where streams silently
die after ~20 minutes.

Fix

  • Replace || with ?? for autoReconnect default assignment
  • Add onClose?: (code: number, reason: string) => void to interface
  • Add onError?: (error: ErrorEvent) => void to interface
  • Wire both callbacks into the existing onClose and onError handlers

Usage after fix

new RealTimeDataClient({
  autoReconnect: false, // now actually respected
  onClose: (code, reason) => console.log('closed', code, reason),
  onError: (err) => console.error('error', err),
}).connect();

Closes #43
Relates to #26


Note

Low Risk
Low risk: small, localized changes to WebSocket lifecycle handling; main impact is altering reconnection behavior when users explicitly set autoReconnect: false.

Overview
Fixes RealTimeDataClient initialization so autoReconnect: false is respected by switching the defaulting logic to ??.

Extends RealTimeDataClientArgs with onClose and onError callbacks and wires them into the existing WebSocket handlers (with try/catch) so callers can react to disconnects and errors.

Reviewed by Cursor Bugbot for commit 778a5c4. Bugbot is set up for automated code reviews on this repo. Configure here.

… callbacks

- Replace || with ?? for autoReconnect default so that passing
  autoReconnect: false is respected instead of being overridden to true
- Add onClose and onError optional callbacks to RealTimeDataClientArgs
  interface so callers can handle disconnects and errors without
  relying solely on console.error

Fixes Polymarket#26
@Sertug17 Sertug17 requested a review from a team as a code owner April 14, 2026 20:28
Comment thread src/client.ts
Comment on lines +146 to 151
if (this.onErrorCallback) {
this.onErrorCallback(err);
}
if (this.autoReconnect) {
this.connect();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If onErrorCallback throws an exception, the autoReconnect logic on line 150 will never execute, breaking automatic reconnection. The callback should be wrapped in a try-catch block:

if (this.onErrorCallback) {
    try {
        this.onErrorCallback(err);
    } catch (e) {
        console.error('Error in onErrorCallback:', e);
    }
}
Suggested change
if (this.onErrorCallback) {
this.onErrorCallback(err);
}
if (this.autoReconnect) {
this.connect();
}
if (this.onErrorCallback) {
try {
this.onErrorCallback(err);
} catch (e) {
console.error('Error in onErrorCallback:', e);
}
}
if (this.autoReconnect) {
this.connect();
}

Spotted by Graphite

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Wrapped onErrorCallback in a try catch so autoReconnect
always executes regardless of callback behavior. Fixed in 8e8b339.

If onErrorCallback throws, the autoReconnect logic would never execute.
Wrapping in try-catch ensures reconnection always proceeds regardless of
callback behavior.

Suggested by Graphite review on Polymarket#44

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8e8b339. Configure here.

Comment thread src/client.ts
Same defensive pattern applied to onCloseCallback as onErrorCallback.
If the user-provided onClose callback throws, autoReconnect must still
execute to prevent silently dying streams.

Suggested by Cursor Bugbot review on Polymarket#44
Comment thread src/client.ts
Comment on lines +166 to +168
if (this.onCloseCallback) {
try {
this.onCloseCallback(message.code, String(message.reason));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unhandled Exception Breaking Auto-Reconnect: The onCloseCallback is invoked without try-catch protection. If the user's callback throws an error, it will prevent the auto-reconnect logic on line 169 from executing.

// Fix: Add try-catch like onError handler does:
if (this.onCloseCallback) {
    try {
        this.onCloseCallback(message.code, String(message.reason));
    } catch (e) {
        console.error('Error in onCloseCallback:', e);
    }
}

This is inconsistent with the onErrorCallback handling (lines 146-152) which correctly wraps the user callback in try-catch.

Suggested change
if (this.onCloseCallback) {
try {
this.onCloseCallback(message.code, String(message.reason));
if (this.onCloseCallback) {
try {
this.onCloseCallback(message.code, String(message.reason));
} catch (e) {
console.error('Error in onCloseCallback:', e);
}
}

Spotted by Graphite

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already fixed in 778a5c4 applied the same try-catch pattern to onCloseCallback as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: autoReconnect: false is ignored due to || operator; missing onClose/onError callbacks

1 participant