From 32f895a3156b78a876463ae58fc263ad7356fe57 Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Fri, 12 Jun 2015 14:57:23 -0700 Subject: [PATCH] [ReactNative] NetInfo listener Map --- Libraries/Network/NetInfo.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Libraries/Network/NetInfo.js b/Libraries/Network/NetInfo.js index c62cb209a..63df3802b 100644 --- a/Libraries/Network/NetInfo.js +++ b/Libraries/Network/NetInfo.js @@ -11,6 +11,7 @@ */ 'use strict'; +var Map = require('Map'); var NativeModules = require('NativeModules'); var Platform = require('Platform'); var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); @@ -140,30 +141,32 @@ type ConnectivityStateAndroid = $Enum<{ * ``` */ -var _subscriptions = {}; +var _subscriptions = new Map(); var NetInfo = { addEventListener: function ( eventName: ChangeEventName, handler: Function ): void { - _subscriptions[String(handler)] = RCTDeviceEventEmitter.addListener( + var listener = RCTDeviceEventEmitter.addListener( DEVICE_REACHABILITY_EVENT, (appStateData) => { handler(appStateData.network_reachability); } ); + _subscriptions.set(handler, listener); }, removeEventListener: function( eventName: ChangeEventName, handler: Function ): void { - if (!_subscriptions[String(handler)]) { + var listener = _subscriptions.get(handler); + if (!listener) { return; } - _subscriptions[String(handler)].remove(); - _subscriptions[String(handler)] = null; + listener.remove(); + _subscriptions.delete(handler); }, fetch: function(): Promise { @@ -197,19 +200,20 @@ if (Platform.OS === 'ios') { }; } -var _isConnectedSubscriptions = {}; +var _isConnectedSubscriptions = new Map(); NetInfo.isConnected = { addEventListener: function ( eventName: ChangeEventName, handler: Function ): void { - _isConnectedSubscriptions[String(handler)] = (connection) => { + var listener = (connection) => { handler(_isConnected(connection)); }; + _isConnectedSubscriptions.set(handler, listener); NetInfo.addEventListener( eventName, - _isConnectedSubscriptions[String(handler)] + listener ); }, @@ -217,10 +221,12 @@ NetInfo.isConnected = { eventName: ChangeEventName, handler: Function ): void { + var listener = _isConnectedSubscriptions.get(handler); NetInfo.removeEventListener( eventName, - _isConnectedSubscriptions[String(handler)] + listener ); + _isConnectedSubscriptions.delete(handler); }, fetch: function(): Promise {