@react-native-community/netinfo
Version:
React Native Network Info API for iOS & Android
2 lines • 4.23 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";import _objectSpread from"@babel/runtime/helpers/objectSpread";import{DEVICE_CONNECTIVITY_EVENT}from'./privateTypes';import{NetInfoStateType,NetInfoCellularGeneration}from'./types';var connection=window.navigator.connection||window.navigator.mozConnection||window.navigator.webkitConnection;var typeMapping={bluetooth:NetInfoStateType.bluetooth,cellular:NetInfoStateType.cellular,ethernet:NetInfoStateType.ethernet,none:NetInfoStateType.none,other:NetInfoStateType.other,unknown:NetInfoStateType.unknown,wifi:NetInfoStateType.wifi,wimax:NetInfoStateType.wimax,mixed:NetInfoStateType.other};var effectiveTypeMapping={'2g':NetInfoCellularGeneration['2g'],'3g':NetInfoCellularGeneration['3g'],'4g':NetInfoCellularGeneration['4g'],'slow-2g':NetInfoCellularGeneration['2g']};var _getCurrentState=function getCurrentState(_requestedInterface){var isConnected=navigator.onLine;var baseState={isInternetReachable:null};if(!connection){if(isConnected){var _state2=_objectSpread({},baseState,{isConnected:true,type:NetInfoStateType.other,details:{isConnectionExpensive:false}});return _state2;}var _state=_objectSpread({},baseState,{isConnected:false,isInternetReachable:false,type:NetInfoStateType.none,details:null});return _state;}var isConnectionExpensive=connection.saveData;var type=connection.type?typeMapping[connection.type]:isConnected?NetInfoStateType.other:NetInfoStateType.unknown;if(type===NetInfoStateType.bluetooth){var _state3=_objectSpread({},baseState,{isConnected:true,type:type,details:{isConnectionExpensive:isConnectionExpensive}});return _state3;}else if(type===NetInfoStateType.cellular){var _state4=_objectSpread({},baseState,{isConnected:true,type:type,details:{isConnectionExpensive:isConnectionExpensive,cellularGeneration:effectiveTypeMapping[connection.effectiveType]||null,carrier:null}});return _state4;}else if(type===NetInfoStateType.ethernet){var _state5=_objectSpread({},baseState,{isConnected:true,type:type,details:{isConnectionExpensive:isConnectionExpensive,ipAddress:null,subnet:null}});return _state5;}else if(type===NetInfoStateType.wifi){var _state6=_objectSpread({},baseState,{isConnected:true,type:type,details:{isConnectionExpensive:isConnectionExpensive,ipAddress:null,subnet:null}});return _state6;}else if(type===NetInfoStateType.wimax){var _state7=_objectSpread({},baseState,{isConnected:true,type:type,details:{isConnectionExpensive:isConnectionExpensive}});return _state7;}else if(type===NetInfoStateType.none){var _state8=_objectSpread({},baseState,{isConnected:false,isInternetReachable:false,type:type,details:null});return _state8;}else if(type===NetInfoStateType.unknown){var _state9=_objectSpread({},baseState,{isConnected:false,isInternetReachable:false,type:type,details:null});return _state9;}var state=_objectSpread({},baseState,{isConnected:true,type:NetInfoStateType.other,details:{isConnectionExpensive:isConnectionExpensive}});return state;};var handlers=[];var nativeHandlers=[];var RNCNetInfo={addListener:function addListener(type,handler){switch(type){case DEVICE_CONNECTIVITY_EVENT:{var nativeHandler=function nativeHandler(){handler(_getCurrentState());};if(connection){connection.addEventListener('change',nativeHandler);}else{window.addEventListener('online',nativeHandler,false);window.addEventListener('offline',nativeHandler,false);}handlers.push(handler);nativeHandlers.push(nativeHandler);break;}}},removeListeners:function removeListeners(type,handler){switch(type){case DEVICE_CONNECTIVITY_EVENT:{var index=handlers.indexOf(handler);var nativeHandler=nativeHandlers[index];if(connection){connection.removeEventListener('change',nativeHandler);}else{window.addEventListener('online',nativeHandler);window.addEventListener('offline',nativeHandler);}handlers.splice(index,1);nativeHandlers.splice(index,1);break;}}},getCurrentState:function getCurrentState(requestedInterface){return _regeneratorRuntime.async(function getCurrentState$(_context){while(1){switch(_context.prev=_context.next){case 0:return _context.abrupt("return",_getCurrentState(requestedInterface));case 1:case"end":return _context.stop();}}},null,this);}};export default RNCNetInfo;
//# sourceMappingURL=nativeModule.web.js.map