Skip to content

Commit cd81fee

Browse files
author
Eric Vicenti
committed
[ReactNative] Flow and doc formatting for NetInfo
1 parent 96c63c9 commit cd81fee

1 file changed

Lines changed: 57 additions & 35 deletions

File tree

Libraries/Network/NetInfo.js

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,25 @@ type ChangeEventName = $Enum<{
2121
change: string;
2222
}>;
2323

24+
type ReachabilityStateIOS = $Enum<{
25+
cell: string;
26+
none: string;
27+
unknown: string;
28+
wifi: string;
29+
}>;
30+
2431

2532
/**
2633
* NetInfo exposes info about online/offline status
2734
*
28-
* == iOS Reachability
35+
* ### reachabilityIOS
2936
*
3037
* Asyncronously determine if the device is online and on a cellular network.
3138
*
32-
* - "none" - device is offline
33-
* - "wifi" - device is online and connected via wifi, or is the iOS simulator
34-
* - "cell" - device is connected via Edge, 3G, WiMax, or LTE
35-
* - "unknown" - error case and the network status is unknown
39+
* - `none` - device is offline
40+
* - `wifi` - device is online and connected via wifi, or is the iOS simulator
41+
* - `cell` - device is connected via Edge, 3G, WiMax, or LTE
42+
* - `unknown` - error case and the network status is unknown
3643
*
3744
* ```
3845
* NetInfo.reachabilityIOS.fetch().done((reach) => {
@@ -50,11 +57,37 @@ type ChangeEventName = $Enum<{
5057
* handleFirstReachabilityChange
5158
* );
5259
* ```
60+
*
61+
* ### isConnected
62+
*
63+
* Available on all platforms. Asyncronously fetch a boolean to determine
64+
* internet connectivity.
65+
*
66+
* ```
67+
* NetInfo.isConnected.fetch().done((isConnected) => {
68+
* console.log('First, is ' + (isConnected ? 'online' : 'offline'));
69+
* });
70+
* function handleFirstConnectivityChange(isConnected) {
71+
* console.log('Then, is ' + (isConnected ? 'online' : 'offline'));
72+
* NetInfo.isConnected.removeEventListener(
73+
* 'change',
74+
* handleFirstConnectivityChange
75+
* );
76+
* }
77+
* NetInfo.isConnected.addEventListener(
78+
* 'change',
79+
* handleFirstConnectivityChange
80+
* );
81+
* ```
5382
*/
5483

5584
var NetInfo = {};
5685

5786
if (RCTReachability) {
87+
88+
// RCTReachability is exposed, so this is an iOS-like environment and we will
89+
// expose reachabilityIOS
90+
5891
var _reachabilitySubscriptions = {};
5992

6093
NetInfo.reachabilityIOS = {
@@ -84,7 +117,7 @@ if (RCTReachability) {
84117
fetch: function(): Promise {
85118
return new Promise((resolve, reject) => {
86119
RCTReachability.getCurrentReachability(
87-
(resp) => {
120+
function(resp) {
88121
resolve(resp.network_reachability);
89122
},
90123
reject
@@ -93,53 +126,42 @@ if (RCTReachability) {
93126
},
94127
};
95128

96-
/**
97-
*
98-
* == NetInfo.isConnected
99-
*
100-
* Available on all platforms. Asyncronously fetch a boolean to determine
101-
* internet connectivity.
102-
*
103-
* ```
104-
* NetInfo.isConnected.fetch().done((isConnected) => {
105-
* console.log('First, is ' + (isConnected ? 'online' : 'offline'));
106-
* });
107-
* function handleFirstConnectivityChange(isConnected) {
108-
* console.log('Then, is ' + (isConnected ? 'online' : 'offline'));
109-
* NetInfo.isConnected.removeEventListener(
110-
* 'change',
111-
* handleFirstConnectivityChange
112-
* );
113-
* }
114-
* NetInfo.isConnected.addEventListener(
115-
* 'change',
116-
* handleFirstConnectivityChange
117-
* );
118-
* ```
119-
*
120-
*/
121129
var _isConnectedSubscriptions = {};
130+
131+
var _iosReachabilityIsConnected = function(
132+
reachability: ReachabilityStateIOS
133+
): bool {
134+
return reachability !== 'none' &&
135+
reachability !== 'unknown';
136+
};
137+
122138
NetInfo.isConnected = {
123139
addEventListener: function (
124140
eventName: ChangeEventName,
125141
handler: Function
126142
): void {
127143
_isConnectedSubscriptions[handler] = (reachability) => {
128-
handler(reachability !== 'none');
144+
handler(_iosReachabilityIsConnected(reachability));
129145
};
130-
NetInfo.reachabilityIOS.addEventListener(eventName, _isConnectedSubscriptions[handler]);
146+
NetInfo.reachabilityIOS.addEventListener(
147+
eventName,
148+
_isConnectedSubscriptions[handler]
149+
);
131150
},
132151

133152
removeEventListener: function(
134153
eventName: ChangeEventName,
135154
handler: Function
136155
): void {
137-
NetInfo.reachabilityIOS.removeEventListener(eventName, _isConnectedSubscriptions[handler]);
156+
NetInfo.reachabilityIOS.removeEventListener(
157+
eventName,
158+
_isConnectedSubscriptions[handler]
159+
);
138160
},
139161

140162
fetch: function(): Promise {
141163
return NetInfo.reachabilityIOS.fetch().then(
142-
(reachability) => reachability !== 'none'
164+
(reachability) => _iosReachabilityIsConnected(reachability)
143165
);
144166
},
145167
};

0 commit comments

Comments
 (0)