fix(island): 🐛 multiple intervals are started at same time checkin is-online
This commit is contained in:
parent
82906907e2
commit
470467d8a8
|
|
@ -32,7 +32,7 @@ export default function IsOnline(
|
|||
? navigator.connection as NetworkConnection
|
||||
: null
|
||||
useEffect(() => {
|
||||
openSocket(status, { id: undefined })
|
||||
openSocket(status)
|
||||
connection?.addEventListener('change', () => {
|
||||
fetch('/api/serviceworker/is-online')
|
||||
.then(() => status.value = true)
|
||||
|
|
@ -43,18 +43,19 @@ export default function IsOnline(
|
|||
return <>{displayed}</>
|
||||
}
|
||||
|
||||
function openSocket(status: Signal<boolean>, ref: { id: number | undefined }) {
|
||||
function openSocket(status: Signal<boolean>) {
|
||||
const socket = new WebSocket(
|
||||
`wss://${location.host}/api/serviceworker/is-online`,
|
||||
)
|
||||
socket.addEventListener('open', () => {
|
||||
status.value = true
|
||||
clearInterval(ref.id)
|
||||
const id = Number(sessionStorage.getItem('$cohabit.is-online.id') ?? -1)
|
||||
clearInterval(id)
|
||||
})
|
||||
socket.addEventListener('close', () => {
|
||||
status.value = false
|
||||
// Try reconnect every 5s
|
||||
const id = setInterval(() => openSocket(status, ref), 5_000)
|
||||
ref.id = id
|
||||
const id = setInterval(() => openSocket(status), 5_000)
|
||||
sessionStorage.setItem('$cohabit.is-online.id', id.toString())
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue