Skip to content

Commit 73e6db2

Browse files
9romiseOrbisK
andauthored
feat(useWebSocket): pass the retried to the autoReconnect.retries (#4604)
Co-authored-by: Robin <robin.kehl@singular-it.de>
1 parent 4b7ab55 commit 73e6db2

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { useWebSocket } from '@vueuse/core'
2+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3+
4+
describe('useWebSocket', () => {
5+
beforeEach(() => {
6+
vi.useFakeTimers()
7+
})
8+
afterEach(() => {
9+
vi.restoreAllMocks()
10+
})
11+
12+
describe('autoReconnect', () => {
13+
const AUTO_RECONNECT_DELAY = 1000
14+
15+
it('should reconnect if autoReconnect is true', () => {
16+
const { ws, status } = useWebSocket('ws://localhost', {
17+
autoReconnect: true,
18+
})
19+
20+
ws.value?.onopen?.(new Event('open'))
21+
22+
ws.value?.onclose?.(new CloseEvent('close'))
23+
expect(status.value).toBe('CLOSED')
24+
vi.advanceTimersByTime(AUTO_RECONNECT_DELAY)
25+
26+
expect(status.value).toBe('CONNECTING')
27+
})
28+
29+
it('should not reconnect if autoReconnect is false', () => {
30+
const { ws, status } = useWebSocket('ws://localhost', {
31+
autoReconnect: false,
32+
})
33+
34+
ws.value?.onopen?.(new Event('open'))
35+
36+
ws.value?.onclose?.(new CloseEvent('close'))
37+
expect(status.value).toBe('CLOSED')
38+
vi.advanceTimersByTime(AUTO_RECONNECT_DELAY)
39+
40+
expect(status.value).toBe('CLOSED')
41+
})
42+
43+
it('should call onFailed if autoReconnect is not false', () => {
44+
const onFailed = vi.fn()
45+
const { ws, status } = useWebSocket('ws://localhost', {
46+
autoReconnect: {
47+
retries: () => false,
48+
onFailed,
49+
},
50+
})
51+
52+
ws.value?.onopen?.(new Event('open'))
53+
54+
ws.value?.onclose?.(new CloseEvent('close'))
55+
56+
expect(onFailed).toHaveBeenCalled()
57+
expect(status.value).toBe('CLOSED')
58+
})
59+
60+
it('should reconnect if autoReconnect.retries is number', () => {
61+
const { ws, status } = useWebSocket('ws://localhost', {
62+
autoReconnect: {
63+
retries: 2,
64+
},
65+
})
66+
67+
ws.value?.onopen?.(new Event('open'))
68+
69+
ws.value?.onclose?.(new CloseEvent('close'))
70+
expect(status.value).toBe('CLOSED')
71+
vi.advanceTimersByTime(AUTO_RECONNECT_DELAY)
72+
73+
expect(status.value).toBe('CONNECTING')
74+
})
75+
76+
it('should reconnect if autoReconnect.retries is function', () => {
77+
const { ws, status } = useWebSocket('ws://localhost', {
78+
autoReconnect: {
79+
retries: retried => retried < 1,
80+
},
81+
})
82+
83+
ws.value?.onopen?.(new Event('open'))
84+
85+
ws.value?.onclose?.(new CloseEvent('close'))
86+
expect(status.value).toBe('CLOSED')
87+
vi.advanceTimersByTime(AUTO_RECONNECT_DELAY)
88+
89+
expect(status.value).toBe('CONNECTING')
90+
})
91+
})
92+
})

packages/core/useWebSocket/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface UseWebSocketOptions {
6161
*
6262
* @default -1
6363
*/
64-
retries?: number | (() => boolean)
64+
retries?: number | ((retried: number) => boolean)
6565

6666
/**
6767
* Delay for reconnect, in milliseconds
@@ -256,13 +256,14 @@ export function useWebSocket<Data = any>(
256256
onFailed,
257257
} = resolveNestedOptions(options.autoReconnect)
258258

259-
if (typeof retries === 'number' && (retries < 0 || retried < retries)) {
259+
const checkRetires = typeof retries === 'function'
260+
? retries
261+
: () => typeof retries === 'number' && (retries < 0 || retried < retries)
262+
263+
if (checkRetires(retried)) {
260264
retried += 1
261265
retryTimeout = setTimeout(_init, delay)
262266
}
263-
else if (typeof retries === 'function' && retries()) {
264-
retryTimeout = setTimeout(_init, delay)
265-
}
266267
else {
267268
onFailed?.()
268269
}

0 commit comments

Comments
 (0)