621719077f
新增宿主网络在线 Hook 并覆盖离线与恢复事件测试 平台外部生成队列概览在原生壳离线时暂停轮询 同步宿主壳网络状态消费方案与共享决策记录
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
import {
|
|
getHostNetworkStatus,
|
|
type HostNetworkStatusSnapshot,
|
|
subscribeHostNetworkStatusChange,
|
|
} from '../services/host-bridge/hostBridge';
|
|
|
|
export function resolveHostNetworkOnline(
|
|
status: HostNetworkStatusSnapshot | false | null | undefined,
|
|
) {
|
|
if (!status) {
|
|
return true;
|
|
}
|
|
|
|
return status.isConnected && status.isInternetReachable !== false;
|
|
}
|
|
|
|
export function useHostNetworkOnline() {
|
|
const [isHostNetworkOnline, setIsHostNetworkOnline] = useState(true);
|
|
|
|
useEffect(() => {
|
|
let isDisposed = false;
|
|
|
|
const applyNetworkStatus = (
|
|
status: HostNetworkStatusSnapshot | false | null | undefined,
|
|
) => {
|
|
if (!isDisposed) {
|
|
setIsHostNetworkOnline(resolveHostNetworkOnline(status));
|
|
}
|
|
};
|
|
|
|
void getHostNetworkStatus().then(applyNetworkStatus);
|
|
const unsubscribe = subscribeHostNetworkStatusChange(applyNetworkStatus);
|
|
|
|
return () => {
|
|
isDisposed = true;
|
|
unsubscribe();
|
|
};
|
|
}, []);
|
|
|
|
return isHostNetworkOnline;
|
|
}
|