UNPKG

@react-native-community/netinfo

Version:
2 lines 1.48 kB
import NativeInterface from'./nativeInterface';var DEVICE_CONNECTIVITY_EVENT='netInfo.networkStatusDidChange';var _subscriptions=new Set();var _latestState=null;var _nativeEventSubscription=null;function _listenerHandler(state){_latestState=state;for(var _iterator=_subscriptions,_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[typeof Symbol==="function"?Symbol.iterator:"@@iterator"]();;){var _ref;if(_isArray){if(_i>=_iterator.length)break;_ref=_iterator[_i++];}else{_i=_iterator.next();if(_i.done)break;_ref=_i.value;}var _handler=_ref;_handler(_latestState);}}export function add(handler){var latestOnListen=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;_subscriptions.add(handler);if(latestOnListen){if(_latestState){handler(_latestState);}else{NativeInterface.getCurrentState().then(function(state){_latestState=state;handler(_latestState);});}}if(_subscriptions.size>0&&!_nativeEventSubscription){_nativeEventSubscription=NativeInterface.eventEmitter.addListener(DEVICE_CONNECTIVITY_EVENT,_listenerHandler);}}export function remove(handler){_subscriptions.delete(handler);if(_subscriptions.size===0&&_nativeEventSubscription){_nativeEventSubscription.remove();_nativeEventSubscription=null;}}export function clear(){_subscriptions.clear();if(_nativeEventSubscription){_nativeEventSubscription.remove();_nativeEventSubscription=null;}}export default{add:add,remove:remove,clear:clear}; //# sourceMappingURL=subscriptions.js.map