Skip to content

Commit d74f79d

Browse files
committed
refactor: 复用代码
1 parent 266e61e commit d74f79d

File tree

1 file changed

+5
-9
lines changed
  • src/BootstrapBlazor/wwwroot/modules

1 file changed

+5
-9
lines changed

src/BootstrapBlazor/wwwroot/modules/net.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@ import EventHandler from "./event-handler.js";
33

44
export function init(id, options) {
55
const { invoke, onlineStateChangedCallback, onNetworkStateChangedCallback, indicators } = options;
6-
navigator.connection.onchange = e => {
7-
const nt = e.target;
6+
const updateState = nt => {
87
const { downlink, effectiveType, rtt } = nt;
98
invoke.invokeMethodAsync(onNetworkStateChangedCallback, {
109
downlink, networkType: effectiveType, rTT: rtt
1110
});
1211
}
13-
14-
const resetState = () => {
15-
const { downlink, effectiveType, rtt } = navigator.connection;
16-
invoke.invokeMethodAsync(onNetworkStateChangedCallback, {
17-
downlink, networkType: effectiveType, rTT: rtt
18-
});
12+
navigator.connection.onchange = e => {
13+
updateState(e.target);
1914
}
2015

2116
const onlineStateChanged = () => {
@@ -47,7 +42,8 @@ export function init(id, options) {
4742
onlineStateChanged,
4843
offlineStateChanged
4944
});
50-
resetState();
45+
46+
updateState(navigator.connection);
5147
}
5248

5349
export async function dispose(id) {

0 commit comments

Comments
 (0)