You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
const {
|
|
invokeCallbackHandler: invoke
|
|
} = UniServiceJSBridge
|
|
|
|
const callbackIds = []
|
|
|
|
function changeHandler () {
|
|
const {
|
|
networkType
|
|
} = getNetworkType()
|
|
callbackIds.forEach(callbackId => {
|
|
invoke(callbackId, {
|
|
errMsg: 'onNetworkStatusChange:ok',
|
|
isConnected: networkType !== 'none',
|
|
networkType
|
|
})
|
|
})
|
|
}
|
|
|
|
export function onNetworkStatusChange (callbackId) {
|
|
const connection = navigator.connection || navigator.webkitConnection
|
|
callbackIds.push(callbackId)
|
|
if (connection) {
|
|
connection.addEventListener('change', changeHandler)
|
|
} else {
|
|
window.addEventListener('offline', changeHandler)
|
|
window.addEventListener('online', changeHandler)
|
|
}
|
|
}
|
|
|
|
export function getNetworkType () {
|
|
const connection = navigator.connection || navigator.webkitConnection
|
|
let networkType = 'unknown'
|
|
if (connection) {
|
|
networkType = connection.type
|
|
if (networkType === 'cellular' && connection.effectiveType) {
|
|
networkType = connection.effectiveType.replace('slow-', '')
|
|
} else if (!['none', 'wifi'].includes(networkType)) {
|
|
networkType = 'unknown'
|
|
}
|
|
} else if (navigator.onLine === false) {
|
|
networkType = 'none'
|
|
}
|
|
return {
|
|
errMsg: 'getNetworkType:ok',
|
|
networkType
|
|
}
|
|
}
|
|
|