Created
June 4, 2024 05:13
-
-
Save anartzdev/bd8fdb2864c070bb4070f3a14bffbf7f to your computer and use it in GitHub Desktop.
Use Network Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { Signal } from '@builder.io/qwik'; | |
import { useSignal, useVisibleTask$ } from '@builder.io/qwik'; | |
export type NetworkType = | |
| 'bluetooth' | |
| 'cellular' | |
| 'ethernet' | |
| 'none' | |
| 'wifi' | |
| 'wimax' | |
| 'other' | |
| 'unknown'; | |
export type NetworkEffectiveType = 'slow-2g' | '2g' | '3g' | '4g' | undefined; | |
export function useNetwork() { | |
const isOnline = useSignal(true); | |
const isSupported = useSignal(true); | |
const saveData = useSignal(false); | |
const offlineAt: Signal<number | undefined> = useSignal(undefined); | |
const onlineAt: Signal<number | undefined> = useSignal(undefined); | |
const downlink: Signal<number | undefined> = useSignal(undefined); | |
const downlinkMax: Signal<number | undefined> = useSignal(undefined); | |
const rtt: Signal<number | undefined> = useSignal(undefined); | |
const effectiveType: Signal<NetworkEffectiveType> = useSignal(undefined); | |
const type: Signal<NetworkType> = useSignal<NetworkType>('unknown'); | |
useVisibleTask$( | |
({ cleanup }) => { | |
const navigator = window?.navigator; | |
isSupported.value = navigator && 'connection' in navigator; | |
const connection = isSupported.value && (navigator as any).connection; | |
function updateNetworkInformation() { | |
if (!navigator) return; | |
isOnline.value = navigator.onLine; | |
offlineAt.value = isOnline.value ? undefined : Date.now(); | |
onlineAt.value = isOnline.value ? Date.now() : undefined; | |
if (connection) { | |
downlink.value = connection.downlink; | |
downlinkMax.value = connection.downlinkMax; | |
effectiveType.value = connection.effectiveType; | |
rtt.value = connection.rtt; | |
saveData.value = connection.saveData; | |
type.value = connection.type; | |
} | |
} | |
window.addEventListener('online', () => { | |
isOnline.value = true; | |
onlineAt.value = Date.now(); | |
}); | |
window.addEventListener('offline', () => { | |
isOnline.value = false; | |
offlineAt.value = Date.now(); | |
}); | |
if (connection) | |
connection.addEventListener('change', updateNetworkInformation); | |
updateNetworkInformation(); | |
cleanup(() => { | |
connection.removeEventListener('change', updateNetworkInformation); | |
}); | |
}, | |
{ | |
strategy: 'document-ready', | |
} | |
); | |
return { | |
isSupported, | |
isOnline, | |
saveData, | |
offlineAt, | |
onlineAt, | |
downlink, | |
downlinkMax, | |
rtt, | |
effectiveType, | |
type, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment