Skip to content

Commit

Permalink
feat(useNetwork): new onlineAt attribute (#1535)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklin committed May 3, 2022
1 parent 81f355d commit 4d5cf1c
Showing 1 changed file with 8 additions and 0 deletions.
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

0 comments on commit 4d5cf1c

Please sign in to comment.