Permalink
Browse files

deprecate some usage of NetInfo

Reviewed By: wwjholmes

Differential Revision: D5493891

fbshipit-source-id: f86f034294f3fd07a535d2856ca6c7d4e2eb7824
  • Loading branch information...
zjj010104 authored and facebook-github-bot committed Jul 28, 2017
1 parent aadeff0 commit bca825ee50d6ca343ce704086dff5841312a7f8b
Showing with 13 additions and 7 deletions.
  1. +13 −7 Libraries/Network/NetInfo.js
@@ -59,21 +59,25 @@ type ConnectivityStateAndroid = $Enum<{
const _subscriptions = new Map();
let _isConnected;
let _isConnectedDeprecated;
if (Platform.OS === 'ios') {
_isConnected = function(
_isConnectedDeprecated = function(
reachability: ReachabilityStateIOS,
): bool {
return reachability !== 'none' && reachability !== 'unknown';
};
} else if (Platform.OS === 'android') {
_isConnected = function(
_isConnectedDeprecated = function(
connectionType: ConnectivityStateAndroid,
): bool {
return connectionType !== 'NONE' && connectionType !== 'UNKNOWN';
};
}
function _isConnected(connection) {
return connection.type !== 'none' && connection.type !== 'unknown';
}
const _isConnectedSubscriptions = new Map();
/**
@@ -290,7 +294,11 @@ const NetInfo = {
handler: Function
): {remove: () => void} {
const listener = (connection) => {
handler(_isConnected(connection));
if (eventName === 'change') {
handler(_isConnectedDeprecated(connection));
} else if (eventName === 'connectionChange') {
handler(_isConnected(connection));
}
};
_isConnectedSubscriptions.set(handler, listener);
NetInfo.addEventListener(
@@ -318,9 +326,7 @@ const NetInfo = {
},
fetch(): Promise<any> {
return NetInfo.fetch().then(
(connection) => _isConnected(connection)
);
return NetInfo.getConnectionInfo().then(_isConnected);
},
},

1 comment on commit bca825e

@rigdern

This comment has been minimized.

Show comment
Hide comment
@rigdern

rigdern Aug 10, 2017

Contributor

@zjj010104 Good catch! One suggestion: instead of supporting both change and connectionChange events in NetInfo.isConnected.addEventListener why not just support the change event but implement it by listening to NetInfo's connectionChange event?

So developers would do:

NetInfo.isConnected.addEventListener('change', ...)

And isConnected.addEventListener would be implemented with something like this:

  isConnected: {
    addEventListener(...) {
       // ...

       NetInfo.addEventListener(
         'connectionChange',
         listener
       );

       // ..
    }
  }
Contributor

rigdern commented on bca825e Aug 10, 2017

@zjj010104 Good catch! One suggestion: instead of supporting both change and connectionChange events in NetInfo.isConnected.addEventListener why not just support the change event but implement it by listening to NetInfo's connectionChange event?

So developers would do:

NetInfo.isConnected.addEventListener('change', ...)

And isConnected.addEventListener would be implemented with something like this:

  isConnected: {
    addEventListener(...) {
       // ...

       NetInfo.addEventListener(
         'connectionChange',
         listener
       );

       // ..
    }
  }
Please sign in to comment.