fix: use ?? for autoReconnect default and add onClose/onError callbacks (closes #43)#44
Conversation
… 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
| if (this.onErrorCallback) { | ||
| this.onErrorCallback(err); | ||
| } | ||
| if (this.autoReconnect) { | ||
| this.connect(); | ||
| } |
There was a problem hiding this comment.
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);
}
}| 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
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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.
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
| if (this.onCloseCallback) { | ||
| try { | ||
| this.onCloseCallback(message.code, String(message.reason)); |
There was a problem hiding this comment.
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.
| 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
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Already fixed in 778a5c4 applied the same try-catch pattern to onCloseCallback as well.

Problem
Two bugs in
RealTimeDataClientArgsinitialization:1.
autoReconnect: falsesilently ignoredargs!.autoReconnect || truetreatsfalseas falsy, so the clientalways reconnects regardless of the user's intent.
2. No
onClose/onErrorcallbacksCallers 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
||with??forautoReconnectdefault assignmentonClose?: (code: number, reason: string) => voidto interfaceonError?: (error: ErrorEvent) => voidto interfaceonCloseandonErrorhandlersUsage after fix
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
RealTimeDataClientinitialization soautoReconnect: falseis respected by switching the defaulting logic to??.Extends
RealTimeDataClientArgswithonCloseandonErrorcallbacks 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.