feat(ui): implement new event stream connection logic
Added a new event stream connection method to enhance the handling of server events. This includes a reconnect mechanism for improved reliability in case of connection errors. The configuration now allows toggling the new event stream feature via `devNewEventStream`. Additionally, tests were added to ensure the new functionality works as expected, including reconnection behavior after an error. Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { describe, it, beforeEach, vi, expect } from 'vitest'
|
||||
import { startEventStream } from './eventStream'
|
||||
import { serverDown } from './actions'
|
||||
import config from './config'
|
||||
|
||||
class MockEventSource {
|
||||
constructor(url) {
|
||||
this.url = url
|
||||
this.readyState = 1
|
||||
this.listeners = {}
|
||||
this.onerror = null
|
||||
}
|
||||
addEventListener(type, handler) {
|
||||
this.listeners[type] = handler
|
||||
}
|
||||
close() {
|
||||
this.readyState = 2
|
||||
}
|
||||
}
|
||||
|
||||
describe('startEventStream', () => {
|
||||
vi.useFakeTimers()
|
||||
let dispatch
|
||||
let instance
|
||||
|
||||
beforeEach(() => {
|
||||
dispatch = vi.fn()
|
||||
global.EventSource = vi.fn((url) => {
|
||||
instance = new MockEventSource(url)
|
||||
return instance
|
||||
})
|
||||
localStorage.setItem('is-authenticated', 'true')
|
||||
localStorage.setItem('token', 'abc')
|
||||
config.devNewEventStream = true
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
config.devNewEventStream = false
|
||||
})
|
||||
|
||||
it('reconnects after an error', async () => {
|
||||
await startEventStream(dispatch)
|
||||
expect(global.EventSource).toHaveBeenCalledTimes(1)
|
||||
instance.onerror(new Event('error'))
|
||||
expect(dispatch).toHaveBeenCalledWith(serverDown())
|
||||
vi.advanceTimersByTime(5000)
|
||||
expect(global.EventSource).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user