All files / src/composables useOffline.ts

100% Statements 7/7
50% Branches 2/4
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17            2x   2x 7x 3x       6x    
import { ref } from 'vue'
 
/**
 * Singleton reactive online state — module-level so all consumers share
 * the same ref and only one pair of event listeners is ever registered.
 */
const isOnline = ref(typeof navigator !== 'undefined' ? navigator.onLine : true)
 
Eif (typeof window !== 'undefined') {
  window.addEventListener('online', () => { isOnline.value = true })
  window.addEventListener('offline', () => { isOnline.value = false })
}
 
export function useOffline() {
  return { isOnline }
}