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:
Deluan
2025-06-29 10:18:05 -04:00
parent 411b32ebb8
commit dce7705999
6 changed files with 116 additions and 4 deletions
+49
View File
@@ -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)
})
})