Skip to content

Commit bb0c81b

Browse files
committed
[IMPL] useNetwork hook
1 parent bdfb530 commit bb0c81b

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

apps/react-tools-demo/src/markdown/useStateGetReset.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const UseStateGetReset = () => {
2323
});
2424
}, [state, setState])
2525

26+
const fullState = useMemo(() => {
27+
console.log("fullState")
28+
const state = getState();
29+
return <p>{state.id} - {state.name} - {state.eta}</p>
30+
}, [getState])
31+
2632
return (
2733
<div style={{ display: "grid", gridTemplateColumns: "auto auto", justifyContent: "center", gap: 50 }}>
2834
<div>
@@ -42,6 +48,7 @@ const UseStateGetReset = () => {
4248
<Input id="eta" name="eta" value={state.eta} onChange={onChange} />
4349
</div>
4450
</div>
51+
{fullState}
4552
</div>
4653
);
4754
};

packages/react-tools/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
- [x] useHover
4848
- [x] useResponsive
4949
- [x] useClickOutside
50-
- [ ] useNetwork
50+
- [-] useNetwork
5151
- [ ] useOnline
5252
- [ ] useSize (=useMeasuree?)
5353
- [ ] useScrollIntoView

packages/react-tools/src/hooks/useEventListener.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { useMemoizedFunction } from ".";
1212
*/
1313
export const useEventListener = ({ type, listener, element = window, listenerOpts }: { type: string, listener: (evt: Event | CustomEvent) => void, element?: RefObject<HTMLElement> | Window, listenerOpts?: boolean | AddEventListenerOptions}) => {
1414
const optsMemoized = useRef<typeof listenerOpts>(listenerOpts);
15-
const listenerMemoized = useMemoizedFunction(listener);
1615
const elementReference = useRef<HTMLElement | Window | null>();
1716

1817
useEffect(() => {
@@ -23,14 +22,14 @@ export const useEventListener = ({ type, listener, element = window, listenerOpt
2322
: null
2423
: element as Window
2524

26-
elementReference.current && (elementReference.current as HTMLElement | Window).addEventListener(type, listenerMemoized, opts);
25+
elementReference.current && (elementReference.current as HTMLElement | Window).addEventListener(type, listener, opts);
2726
return () => {
28-
elementReference.current && (elementReference.current as HTMLElement | Window).removeEventListener(type, listenerMemoized, opts);
27+
elementReference.current && (elementReference.current as HTMLElement | Window).removeEventListener(type, listener, opts);
2928
}
30-
}, [element, type, listenerMemoized]);
29+
}, [element, type, listener]);
3130

3231
const remove = useMemoizedFunction(() => {
33-
elementReference.current && (elementReference.current as HTMLElement | Window).removeEventListener(type, listenerMemoized, optsMemoized.current);
32+
elementReference.current && (elementReference.current as HTMLElement | Window).removeEventListener(type, listener, optsMemoized.current);
3433
});
3534

3635
return remove;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useCallback, useMemo } from "react";
2+
import { useSyncExternalStore } from "."
3+
4+
const listeners = new Set<() => void>();
5+
const handler = () => listeners.forEach(l => l());
6+
7+
const getConnetionState = () => {
8+
const connection = Reflect.get(navigator, "connection");
9+
let obj = { online: navigator.onLine };
10+
if (connection) {
11+
obj.isSupported = true;
12+
obj.downlink = connection.downlink as number;
13+
obj.downlinkMax = connection.downlinkMax as number;
14+
obj.effectiveType = connection.effectiveType as "slow-2g" | "2g" | "3g" | "4g";
15+
obj.rtt = connection.rtt as number;
16+
obj.saveData = connection.saveData as boolean;
17+
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;
26+
}
27+
}
28+
export const useNetwork = () => {
29+
return useSyncExternalStore(
30+
useCallback(notif => {
31+
32+
}, []),
33+
useMemo(() => {
34+
let prevState = getConnetionState();
35+
}, [])
36+
);
37+
}

0 commit comments

Comments
 (0)