forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
113 lines (98 loc) · 2.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* this implementation is original ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */
import { ref, Ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { ConfigurableWindow, defaultWindow } from '../_configurable'
export type NetworkType = 'bluetooth' | 'cellular' | 'ethernet' | 'none' | 'wifi' | 'wimax' | 'other' | 'unknown'
export type NetworkEffectiveType = 'slow-2g' | '2g' | '3g' | '4g' | undefined
export interface NetworkState {
isSupported: boolean
/**
* If the user is currently connected.
*/
isOnline: Ref<boolean>
/**
* The time since the user was last connected.
*/
offlineAt: Ref<number | undefined>
/**
* The download speed in Mbps.
*/
downlink: Ref<number | undefined>
/**
* The max reachable download speed in Mbps.
*/
downlinkMax: Ref<number | undefined>
/**
* The detected effective speed type.
*/
effectiveType: Ref<NetworkEffectiveType | undefined>
/**
* The estimated effective round-trip time of the current connection.
*/
rtt: Ref<number | undefined>
/**
* If the user activated data saver mode.
*/
saveData: Ref<boolean | undefined>
/**
* The detected connection/network type.
*/
type: Ref<NetworkType>
}
/**
* Reactive Network status.
*
* @see https://vueuse.org/useNetwork
* @param options
*/
export function useNetwork(options: ConfigurableWindow = {}): Readonly<NetworkState> {
const { window = defaultWindow } = options
const navigator = window?.navigator
const isSupported = Boolean(navigator && 'connection' in navigator)
const isOnline = ref(true)
const saveData = ref(false)
const offlineAt: 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)
const effectiveType: Ref<NetworkEffectiveType> = ref(undefined)
const type: Ref<NetworkType> = ref<NetworkType>('unknown')
const connection = isSupported && (navigator as any).connection
function updateNetworkInformation() {
if (!navigator)
return
isOnline.value = navigator.onLine
offlineAt.value = isOnline.value ? undefined : Date.now()
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
}
}
if (window) {
useEventListener(window, 'offline', () => {
isOnline.value = false
offlineAt.value = Date.now()
})
useEventListener(window, 'online', () => {
isOnline.value = true
})
}
if (connection)
useEventListener(connection, 'change', updateNetworkInformation, false)
updateNetworkInformation()
return {
isSupported,
isOnline,
saveData,
offlineAt,
downlink,
downlinkMax,
effectiveType,
rtt,
type,
}
}