1 Star 0 Fork 0

头上有煎饺/react native 网速监测

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
头上有煎饺 提交于 2019-08-20 14:24 . support ios
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;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jianjianjiao/react-native-network-speed.git
git@gitee.com:jianjianjiao/react-native-network-speed.git
jianjianjiao
react-native-network-speed
react native 网速监测
master

搜索帮助