Skip to content

Commit

Permalink
feat(useNetwork): add rtt properties (vitest-dev#623)
Browse files Browse the repository at this point in the history
Co-authored-by: webfansplz <>
  • Loading branch information
webfansplz committed Jul 25, 2021
1 parent ea08e27 commit 8ce6608
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/core/useNetwork/index.md
Expand Up @@ -78,6 +78,10 @@ export interface NetworkState {
* 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.
*/
Expand All @@ -93,7 +97,7 @@ export interface NetworkState {
* @see https://vueuse.org/useNetwork
* @param options
*/
export declare function useNetwork(options?: ConfigurableWindow): NetworkState
export declare function useNetwork(options?: ConfigurableWindow): Readonly<NetworkState>
```

## Source
Expand Down
13 changes: 10 additions & 3 deletions packages/core/useNetwork/index.ts
Expand Up @@ -27,9 +27,13 @@ export interface NetworkState {
*/
downlinkMax: Ref<number | undefined>
/**
* The detected effective speed type.
*/
* 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.
*/
Expand All @@ -46,7 +50,7 @@ export interface NetworkState {
* @see https://vueuse.org/useNetwork
* @param options
*/
export function useNetwork(options: ConfigurableWindow = {}): NetworkState {
export function useNetwork(options: ConfigurableWindow = {}): Readonly<NetworkState> {
const { window = defaultWindow } = options
const navigator = window?.navigator
const isSupported = Boolean(navigator && 'connection' in navigator)
Expand All @@ -56,6 +60,7 @@ export function useNetwork(options: ConfigurableWindow = {}): NetworkState {
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')

Expand All @@ -72,6 +77,7 @@ export function useNetwork(options: ConfigurableWindow = {}): NetworkState {
downlink.value = connection.downlink
downlinkMax.value = connection.downlinkMax
effectiveType.value = connection.effectiveType
rtt.value = connection.rtt
saveData.value = connection.saveData
type.value = connection.type
}
Expand Down Expand Up @@ -101,6 +107,7 @@ export function useNetwork(options: ConfigurableWindow = {}): NetworkState {
downlink,
downlinkMax,
effectiveType,
rtt,
type,
}
}

0 comments on commit 8ce6608

Please sign in to comment.