Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useNetwork): new onlineAt attribute #1535

Merged
merged 1 commit into from May 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/core/useNetwork/index.ts
Expand Up @@ -20,6 +20,10 @@ export interface NetworkState {
* The time since the user was last connected.
*/
offlineAt: Ref<number | undefined>
/**
* At this time, if the user is offline and reconnects
*/
onlineAt: Ref<number | undefined>
/**
* The download speed in Mbps.
*/
Expand Down Expand Up @@ -60,6 +64,7 @@ export function useNetwork(options: ConfigurableWindow = {}): Readonly<NetworkSt
const isOnline = ref(true)
const saveData = ref(false)
const offlineAt: Ref<number | undefined> = ref(undefined)
const onlineAt: Ref<number | undefined> = ref(undefined)
const downlink: Ref<number | undefined> = ref(undefined)
const downlinkMax: Ref<number | undefined> = ref(undefined)
const rtt: Ref<number | undefined> = ref(undefined)
Expand All @@ -74,6 +79,7 @@ export function useNetwork(options: ConfigurableWindow = {}): Readonly<NetworkSt

isOnline.value = navigator.onLine
offlineAt.value = isOnline.value ? undefined : Date.now()
onlineAt.value = isOnline.value ? Date.now() : undefined

if (connection) {
downlink.value = connection.downlink
Expand All @@ -93,6 +99,7 @@ export function useNetwork(options: ConfigurableWindow = {}): Readonly<NetworkSt

useEventListener(window, 'online', () => {
isOnline.value = true
onlineAt.value = Date.now()
})
}

Expand All @@ -106,6 +113,7 @@ export function useNetwork(options: ConfigurableWindow = {}): Readonly<NetworkSt
isOnline,
saveData,
offlineAt,
onlineAt,
downlink,
downlinkMax,
effectiveType,
Expand Down