Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffa00d2

Browse files
ilyaliaoantfu
andauthored
fix(useWebSocket): clear retryTimer when connected (#4383)
Co-authored-by: Anthony Fu <[email protected]> Co-authored-by: Anthony Fu <[email protected]>
1 parent 398856a commit ffa00d2

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/core/useWebSocket/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export function useWebSocket<Data = any>(
181181

182182
let bufferedData: (string | ArrayBuffer | Blob)[] = []
183183

184+
let retryTimeout: ReturnType<typeof setTimeout> | undefined
184185
let pongTimeoutWait: ReturnType<typeof setTimeout> | undefined
185186

186187
const _sendBuffer = () => {
@@ -191,13 +192,21 @@ export function useWebSocket<Data = any>(
191192
}
192193
}
193194

195+
const resetRetry = () => {
196+
if (retryTimeout != null) {
197+
clearTimeout(retryTimeout)
198+
retryTimeout = undefined
199+
}
200+
}
201+
194202
const resetHeartbeat = () => {
195203
clearTimeout(pongTimeoutWait)
196204
pongTimeoutWait = undefined
197205
}
198206

199207
// Status code 1000 -> Normal Closure https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
200208
const close: WebSocket['close'] = (code = 1000, reason) => {
209+
resetRetry()
201210
if ((!isClient && !isWorker) || !wsRef.value)
202211
return
203212
explicitlyClosed = true
@@ -247,10 +256,10 @@ export function useWebSocket<Data = any>(
247256

248257
if (typeof retries === 'number' && (retries < 0 || retried < retries)) {
249258
retried += 1
250-
setTimeout(_init, delay)
259+
retryTimeout = setTimeout(_init, delay)
251260
}
252261
else if (typeof retries === 'function' && retries()) {
253-
setTimeout(_init, delay)
262+
retryTimeout = setTimeout(_init, delay)
254263
}
255264
else {
256265
onFailed?.()
@@ -313,6 +322,7 @@ export function useWebSocket<Data = any>(
313322
const open = () => {
314323
if (!isClient && !isWorker)
315324
return
325+
316326
close()
317327
explicitlyClosed = false
318328
retried = 0

0 commit comments

Comments
 (0)