feat: offline resilience and connection status
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
627a1a0d9e
commit
6e092ee7be
@@ -0,0 +1,28 @@
|
||||
import { ref, onMounted, onUnmounted } from 'vue'
|
||||
|
||||
const isOnline = ref(typeof navigator !== 'undefined' ? navigator.onLine : true)
|
||||
|
||||
let listenerCount = 0
|
||||
|
||||
function handleOnline() { isOnline.value = true }
|
||||
function handleOffline() { isOnline.value = false }
|
||||
|
||||
export function useOnlineStatus() {
|
||||
onMounted(() => {
|
||||
if (listenerCount === 0) {
|
||||
window.addEventListener('online', handleOnline)
|
||||
window.addEventListener('offline', handleOffline)
|
||||
}
|
||||
listenerCount++
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
listenerCount--
|
||||
if (listenerCount === 0) {
|
||||
window.removeEventListener('online', handleOnline)
|
||||
window.removeEventListener('offline', handleOffline)
|
||||
}
|
||||
})
|
||||
|
||||
return { isOnline }
|
||||
}
|
||||
Reference in New Issue
Block a user