/
internetReachability.ts
176 lines (158 loc) · 5.64 KB
/
internetReachability.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import * as PrivateTypes from './privateTypes';
import * as Types from './types';
interface InternetReachabilityCheckHandler {
promise: Promise<void>;
cancel: () => void;
}
export default class InternetReachability {
private _configuration: Types.NetInfoConfiguration;
private _listener: PrivateTypes.NetInfoInternetReachabilityChangeListener;
private _isInternetReachable: boolean | null | undefined = undefined;
private _currentInternetReachabilityCheckHandler: InternetReachabilityCheckHandler | null = null;
private _currentTimeoutHandle: ReturnType<typeof setTimeout> | null = null;
constructor(
configuration: Types.NetInfoConfiguration,
listener: PrivateTypes.NetInfoInternetReachabilityChangeListener,
) {
this._configuration = configuration;
this._listener = listener;
}
private _setIsInternetReachable = (
isInternetReachable: boolean | null,
): void => {
if (this._isInternetReachable === isInternetReachable) {
return;
}
this._isInternetReachable = isInternetReachable;
this._listener(this._isInternetReachable);
};
private _setExpectsConnection = (expectsConnection: boolean | null): void => {
// Cancel any pending check
if (this._currentInternetReachabilityCheckHandler !== null) {
this._currentInternetReachabilityCheckHandler.cancel();
this._currentInternetReachabilityCheckHandler = null;
}
// Cancel any pending timeout
if (this._currentTimeoutHandle !== null) {
clearTimeout(this._currentTimeoutHandle);
this._currentTimeoutHandle = null;
}
if (expectsConnection && this._configuration.reachabilityShouldRun()) {
// If we expect a connection, start the process for finding if we have one
// Set the state to "null" if it was previously false
if (!this._isInternetReachable) {
this._setIsInternetReachable(null);
}
// Start a network request to check for internet
this._currentInternetReachabilityCheckHandler = this._checkInternetReachability();
} else {
// If we don't expect a connection or don't run reachability check, just change the state to "false"
this._setIsInternetReachable(false);
}
};
private _checkInternetReachability = (): InternetReachabilityCheckHandler => {
const controller = new AbortController();
const responsePromise = fetch(this._configuration.reachabilityUrl, {
headers: this._configuration.reachabilityHeaders,
method: this._configuration.reachabilityMethod,
cache: 'no-cache',
signal: controller.signal,
});
// Create promise that will reject after the request timeout has been reached
let timeoutHandle: ReturnType<typeof setTimeout>;
const timeoutPromise = new Promise<Response>((): void => {
timeoutHandle = setTimeout(
(): void => controller.abort('timedout'),
this._configuration.reachabilityRequestTimeout,
);
});
// Create promise that makes it possible to cancel a pending request through a reject
// eslint-disable-next-line @typescript-eslint/no-empty-function
let cancel: () => void = (): void => {};
const cancelPromise = new Promise<Response>((_, reject): void => {
cancel = (): void => reject('canceled');
});
const promise = Promise.race([
responsePromise,
timeoutPromise,
cancelPromise,
])
.then(
(response): Promise<boolean> => {
return this._configuration.reachabilityTest(response);
},
)
.then(
(result): void => {
this._setIsInternetReachable(result);
const nextTimeoutInterval = this._isInternetReachable
? this._configuration.reachabilityLongTimeout
: this._configuration.reachabilityShortTimeout;
this._currentTimeoutHandle = setTimeout(
this._checkInternetReachability,
nextTimeoutInterval,
);
},
)
.catch(
(error: Error | 'timedout' | 'canceled'): void => {
if ('canceled' === error) {
controller.abort();
} else {
this._setIsInternetReachable(false);
this._currentTimeoutHandle = setTimeout(
this._checkInternetReachability,
this._configuration.reachabilityShortTimeout,
);
}
},
)
// Clear request timeout and propagate any errors
.then(
(): void => {
clearTimeout(timeoutHandle);
},
(error: Error): void => {
clearTimeout(timeoutHandle);
throw error;
},
);
return {
promise,
cancel,
};
};
public update = (state: PrivateTypes.NetInfoNativeModuleState): void => {
if (
typeof state.isInternetReachable === 'boolean' &&
this._configuration.useNativeReachability
) {
this._setIsInternetReachable(state.isInternetReachable);
} else {
this._setExpectsConnection(state.isConnected);
}
};
public currentState = (): boolean | null | undefined => {
return this._isInternetReachable;
};
public tearDown = (): void => {
// Cancel any pending check
if (this._currentInternetReachabilityCheckHandler !== null) {
this._currentInternetReachabilityCheckHandler.cancel();
this._currentInternetReachabilityCheckHandler = null;
}
// Cancel any pending timeout
if (this._currentTimeoutHandle !== null) {
clearTimeout(this._currentTimeoutHandle);
this._currentTimeoutHandle = null;
}
};
}