-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve/error handling #143
Changes from all commits
20ccee1
3597630
52c7368
26a6147
c4aaf66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,35 +226,49 @@ export const SockProvider = ({ children }: ChildrenProps) => { | |
useDebounce( | ||
() => { | ||
if (typeof window !== 'undefined') { | ||
try { | ||
sockPromise.current = new Promise<wsock.w3cwebsocket>((res, rej) => { | ||
let rejected = false; | ||
try { | ||
// eslint-disable-next-line new-cap | ||
const w = new wsock.w3cwebsocket(`${socketUrl}/ws`, '', '', {}); | ||
|
||
w.onmessage = onMessage; | ||
|
||
w.onopen = () => { | ||
res(w); | ||
}; | ||
|
||
w.onerror = (e) => { | ||
console.error('socket closed:', e); | ||
if (!rejected) { | ||
rejected = true; | ||
const connnect = (recon = () => {}) => { | ||
try { | ||
sockPromise.current = new Promise<wsock.w3cwebsocket>( | ||
(res, rej) => { | ||
try { | ||
// eslint-disable-next-line new-cap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code_refinement): Disabling ESLint rules can hide potential issues. If the 'new-cap' rule is frequently disabled, consider configuring this rule project-wide or ensuring that the naming conventions for constructors are followed. |
||
const w = new wsock.w3cwebsocket( | ||
`${socketUrl}/ws`, | ||
'', | ||
'', | ||
{} | ||
); | ||
|
||
w.onmessage = onMessage; | ||
|
||
w.onopen = () => { | ||
res(w); | ||
}; | ||
|
||
w.onerror = (e) => { | ||
console.error(e); | ||
recon(); | ||
Comment on lines
+248
to
+250
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code_refinement): Logging the error before calling the reconnect function could provide valuable debugging information. Consider logging the error using |
||
}; | ||
|
||
w.onclose = () => { | ||
recon(); | ||
}; | ||
} catch (e) { | ||
rej(e); | ||
} | ||
}; | ||
} | ||
); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
}; | ||
Comment on lines
+229
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick (typo): The function name 'connnect' seems to be a typo. Consider renaming it to 'connect' for clarity. |
||
|
||
w.onclose = () => {}; | ||
} catch (e) { | ||
rej(e); | ||
} | ||
}); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
connnect(() => { | ||
setTimeout(() => { | ||
console.log('reconnecting'); | ||
connnect(); | ||
}, 1000); | ||
}); | ||
Comment on lines
+266
to
+271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): Implementing an exponential backoff strategy for reconnection attempts could improve the robustness of the socket connection, especially in unstable network conditions. Constantly reconnecting every 1 second might not be the most efficient approach. |
||
} | ||
}, | ||
1000, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code_refinement): Formatting the errors with
JSON.stringify(errors, null, 2)
improves readability in the UI. This is a good practice for enhancing the debugging experience.