Skip to content

Commit 914f064

Browse files
committed
[WIP] useNetwork
1 parent bb0c81b commit 914f064

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { useCallback, useMemo } from "react";
22
import { useSyncExternalStore } from "."
3+
import { ConnectionState } from "../models";
34

45
const listeners = new Set<() => void>();
56
const handler = () => listeners.forEach(l => l());
67

78
const getConnetionState = () => {
89
const connection = Reflect.get(navigator, "connection");
9-
let obj = { online: navigator.onLine };
10+
const obj: ConnectionState = {
11+
isSupported: !!connection,
12+
isOnline: navigator.onLine
13+
};
1014
if (connection) {
1115
obj.isSupported = true;
1216
obj.downlink = connection.downlink as number;
@@ -15,23 +19,32 @@ const getConnetionState = () => {
1519
obj.rtt = connection.rtt as number;
1620
obj.saveData = connection.saveData as boolean;
1721
obj.type = connection.type as "bluetooth" | "cellular" | "ethernet" | "none" | "wifi" | "wimax" | "other" | "unknown";
18-
} else {
19-
obj.isSupported = false;
20-
obj.downlink = null;
21-
obj.downlinkMax = null;
22-
obj.effectiveType = null;
23-
obj.rtt = null;
24-
obj.saveData = null;
25-
obj.type = null;
2622
}
23+
return obj;
2724
}
25+
2826
export const useNetwork = () => {
2927
return useSyncExternalStore(
3028
useCallback(notif => {
31-
29+
const connection = Reflect.get(navigator, "connection");
30+
if (listeners.size === 0) {
31+
window.addEventListener("online", handler);
32+
window.addEventListener("offline", handler);
33+
!!connection && connection.addEventListener("change", handler);
34+
}
35+
listeners.add(notif);
36+
return () => {
37+
listeners.delete(handler);
38+
if (listeners.size === 0) {
39+
window.removeEventListener("online", handler);
40+
window.removeEventListener("offline", handler);
41+
!!connection && connection.removeEventListener("change", handler);
42+
}
43+
}
3244
}, []),
3345
useMemo(() => {
3446
let prevState = getConnetionState();
47+
return () => {}
3548
}, [])
3649
);
3750
}

packages/react-tools/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export type {
66
UseScript,
77
TextSelection,
88
useResponsiveKeys,
9-
useResponsiveBreakpoints
9+
useResponsiveBreakpoints,
10+
ConnectionState
1011
} from './models'
1112

1213
export {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type { DependencyListTyped, CompareFn } from "./common.model";
22
export type { UseScriptProps, UseScript, UseScriptStatus } from './useScript.model';
33
export type { TextSelection } from './useTextSelection.model';
4-
export type { useResponsiveKeys, useResponsiveBreakpoints } from './useResponsive.model';
4+
export type { useResponsiveKeys, useResponsiveBreakpoints } from './useResponsive.model';
5+
export type { ConnectionState } from './useNetwork.model';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface ConnectionState {
2+
isSupported: boolean;
3+
isOnline: boolean;
4+
downlink?: number;
5+
downlinkMax?: number;
6+
effectiveType?: "slow-2g" | "2g" | "3g" | "4g";
7+
rtt?: number;
8+
saveData?: boolean;
9+
type?: "bluetooth" | "cellular" | "ethernet" | "none" | "wifi" | "wimax" | "other" | "unknown";
10+
}

0 commit comments

Comments
 (0)