fix: 优化数据
This commit is contained in:
@@ -47,20 +47,36 @@ export function fetchAndSetSituation(): Promise<void> {
|
||||
}
|
||||
|
||||
let disconnectWs: (() => void) | null = null
|
||||
let pollInterval: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
const POLL_INTERVAL_MS = 5000
|
||||
|
||||
function pollSituation() {
|
||||
fetchSituation()
|
||||
.then((situation) => useSituationStore.getState().setSituation(situation))
|
||||
.catch(() => {})
|
||||
}
|
||||
|
||||
export function startSituationWebSocket(): () => void {
|
||||
useSituationStore.getState().setConnected(true)
|
||||
useSituationStore.getState().setLastError(null)
|
||||
|
||||
disconnectWs = connectSituationWebSocket((data) => {
|
||||
useSituationStore.getState().setConnected(true)
|
||||
useSituationStore.getState().setSituation(data as MilitarySituation)
|
||||
})
|
||||
|
||||
pollSituation()
|
||||
pollInterval = setInterval(pollSituation, POLL_INTERVAL_MS)
|
||||
|
||||
return stopSituationWebSocket
|
||||
}
|
||||
|
||||
export function stopSituationWebSocket(): void {
|
||||
disconnectWs?.()
|
||||
disconnectWs = null
|
||||
if (pollInterval) {
|
||||
clearInterval(pollInterval)
|
||||
pollInterval = null
|
||||
}
|
||||
useSituationStore.getState().setConnected(false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user