代码拉取完成,页面将自动刷新
import { NativeModules, DeviceEventEmitter, Platform, NativeEventEmitter } from 'react-native';
const { RNNetworkSpeed } = NativeModules;
let defualtCallback = null
let eventEmitter = null
class NetworkSpeed {
static startListenNetworkSpeed(callback) {
// 判断是否已经有监听
if(!callback || typeof callback !== 'function') {throw new Error("callback need a function")}
if(eventEmitter && eventEmitter instanceof NativeEventEmitter) return
if(defualtCallback && typeof defualtCallback == 'function') return
// 初始化监听器
eventEmitter = new NativeEventEmitter(RNNetworkSpeed);
// 初始化监听 调用原生代码开启定时器、监听
RNNetworkSpeed.startListenNetworkSpeed()
defualtCallback = callback
eventEmitter.addListener("onSpeedUpdate", defualtCallback)
}
static stopListenNetworkSpeed() {
if(!defualtCallback && typeof defualtCallback !== 'function') return
if(eventEmitter && eventEmitter instanceof NativeEventEmitter) {
RNNetworkSpeed.stopListenNetworkSpeed()
eventEmitter.removeListener("onSpeedUpdate", defualtCallback)
defualtCallback = null
eventEmitter = null
}
}
}
export default NetworkSpeed;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。