586e46fa63
新增 network.status 与 network.statusChanged HostBridge 能力 Expo 壳通过 expo-network 查询并订阅真实网络状态 Tauri 壳通过主站可达性查询和 WebView online/offline 事件同步网络状态 更新壳能力检查、测试和架构文档
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import * as Network from 'expo-network';
|
|
|
|
import {
|
|
type NetworkStatusResult,
|
|
normalizeHostBridgeConnectionType,
|
|
} from '../../../packages/shared/src/contracts/hostBridge';
|
|
|
|
export function normalizeMobileNetworkStatus(
|
|
state: Network.NetworkState,
|
|
): NetworkStatusResult {
|
|
const nativeType = state.type;
|
|
const connectionType = normalizeHostBridgeConnectionType(nativeType);
|
|
|
|
return {
|
|
isConnected:
|
|
typeof state.isConnected === 'boolean'
|
|
? state.isConnected
|
|
: connectionType !== 'none' && connectionType !== 'unknown',
|
|
isInternetReachable:
|
|
typeof state.isInternetReachable === 'boolean'
|
|
? state.isInternetReachable
|
|
: null,
|
|
connectionType,
|
|
...(nativeType ? { nativeType } : {}),
|
|
};
|
|
}
|
|
|
|
export async function getMobileNetworkStatus() {
|
|
return normalizeMobileNetworkStatus(await Network.getNetworkStateAsync());
|
|
}
|
|
|
|
export function subscribeMobileNetworkStatus(
|
|
listener: (status: NetworkStatusResult) => void,
|
|
) {
|
|
const subscription = Network.addNetworkStateListener((state) => {
|
|
listener(normalizeMobileNetworkStatus(state));
|
|
});
|
|
|
|
return () => subscription.remove();
|
|
}
|