-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Network.js
332 lines (289 loc) · 11.1 KB
/
Network.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import _ from 'underscore';
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import HttpUtils from './HttpUtils';
import ONYXKEYS from '../ONYXKEYS';
import * as ActiveClientManager from './ActiveClientManager';
import CONST from '../CONST';
let isQueuePaused = false;
// Queue for network requests so we don't lose actions done by the user while offline
let networkRequestQueue = [];
// This is an optional function that this lib can be configured with (via registerParameterEnhancer())
// that accepts all request parameters and returns a new copy of them. This allows other code to inject
// parameters such as authTokens or CSRF tokens, etc.
let enhanceParameters;
// These handlers must be registered in order to process the response or network errors returned from the queue.
// The first argument passed will be the queuedRequest object and the second will be either the response or error.
let onResponse = () => {};
let onError = () => {};
let didLoadPersistedRequests;
let isOffline;
/**
* Process the offline NETWORK_REQUEST_QUEUE
* @param {Array<Object> | null} persistedRequests - Requests
*/
function processOfflineQueue(persistedRequests) {
// NETWORK_REQUEST_QUEUE is shared across clients, thus every client will have similiar copy of
// NETWORK_REQUEST_QUEUE. It is very important to only process the queue from leader client
// otherwise requests will be duplicated.
// We only process the persisted requests when
// a) Client is leader.
// b) User is online.
// c) requests are not already loaded,
// d) When there is at least one request
if (!ActiveClientManager.isClientTheLeader()
|| isOffline
|| didLoadPersistedRequests
|| !persistedRequests
|| !persistedRequests.length) {
return;
}
// Queue processing expects handlers but due to we are loading the requests from Storage
// we just noop them to ignore the errors.
_.each(persistedRequests, (request) => {
request.resolve = () => {};
request.reject = () => {};
});
// Merge the persisted requests with the requests in memory then clear out the queue as we only need to load
// this once when the app initializes
networkRequestQueue = [...networkRequestQueue, ...persistedRequests];
Onyx.set(ONYXKEYS.NETWORK_REQUEST_QUEUE, []);
didLoadPersistedRequests = true;
}
// We subscribe to changes to the online/offline status of the network to determine when we should fire off API calls
// vs queueing them for later.
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (val) => {
if (!val) {
return;
}
// Client becomes online, process the queue.
if (isOffline && !val.isOffline) {
const connection = Onyx.connect({
key: ONYXKEYS.NETWORK_REQUEST_QUEUE,
callback: processOfflineQueue,
});
Onyx.disconnect(connection);
}
isOffline = val.isOffline;
},
});
// Subscribe to NETWORK_REQUEST_QUEUE queue as soon as Client is ready
ActiveClientManager.isReady().then(() => {
Onyx.connect({
key: ONYXKEYS.NETWORK_REQUEST_QUEUE,
callback: processOfflineQueue,
});
});
// Subscribe to the user's session so we can include their email in every request and include it in the server logs
let email;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => email = val ? val.email : null,
});
/**
* Checks to see if a request can be made.
*
* @param {Object} request
* @param {Object} request.data
* @param {Boolean} request.data.forceNetworkRequest
* @return {Boolean}
*/
function canMakeRequest(request) {
// These requests are always made even when the queue is paused
if (request.data.forceNetworkRequest === true) {
return true;
}
// If the queue is paused we will not make the request right now
return !isQueuePaused;
}
/**
* Checks to see if a request should be retried when the queue is "paused" and logs the command name + returnValueList
* to give us some limited debugging info. We don't want to log the entire request since this could lead to
* unintentional sharing of sensitive information.
*
* @param {Object} request
* @param {String} request.command
* @param {Object} request.data
* @param {Boolean} request.data.doNotRetry
* @param {String} [request.data.returnValueList]
* @return {Boolean}
*/
function canRetryRequest(request) {
const doNotRetry = lodashGet(request, 'data.doNotRetry', false);
const logParams = {command: request.command, doNotRetry, isQueuePaused};
const returnValueList = lodashGet(request, 'data.returnValueList');
if (returnValueList) {
logParams.returnValueList = returnValueList;
}
if (doNotRetry) {
console.debug('Skipping request that should not be re-tried: ', logParams);
} else {
console.debug('Skipping request and re-queueing: ', logParams);
}
return !doNotRetry;
}
/**
* Process the networkRequestQueue by looping through the queue and attempting to make the requests
*/
function processNetworkRequestQueue() {
// NetInfo tells us whether the app is offline
if (isOffline) {
if (!networkRequestQueue.length) {
return;
}
const retryableRequests = [];
// If we have a request then we need to check if it can be persisted in case we close the tab while offline.
// We filter persisted requests from the normal Queue to remove duplicates
networkRequestQueue = _.reject(networkRequestQueue, (request) => {
if (!request.data.doNotRetry && request.data.persist) {
retryableRequests.push(request);
return true;
}
});
if (retryableRequests.length) {
Onyx.merge(ONYXKEYS.NETWORK_REQUEST_QUEUE, retryableRequests);
}
return;
}
// When the queue length is empty an early return is performed since nothing needs to be processed
if (networkRequestQueue.length === 0) {
return;
}
// Some requests should be retried and will end up here if the following conditions are met:
// - the queue is paused
// - the request does not have forceNetworkRequest === true
// - the request does not have doNotRetry === true
const requestsToProcessOnNextRun = [];
_.each(networkRequestQueue, (queuedRequest) => {
// Some requests must be allowed to run even when the queue is paused e.g. an authentication request
// that pauses the network queue while authentication happens, then unpauses it when it's done.
if (!canMakeRequest(queuedRequest)) {
if (canRetryRequest(queuedRequest)) {
requestsToProcessOnNextRun.push(queuedRequest);
}
return;
}
const requestData = queuedRequest.data;
const requestEmail = lodashGet(requestData, 'email', '');
// If we haven't passed an email in the request data, set it to the current user's email
if (email && _.isEmpty(requestEmail)) {
requestData.email = email;
}
const finalParameters = _.isFunction(enhanceParameters)
? enhanceParameters(queuedRequest.command, requestData)
: requestData;
// Check to see if the queue has paused again. It's possible that a call to enhanceParameters()
// has paused the queue and if this is the case we must return. We don't retry these requests
// since if a request is made without an authToken we sign out the user.
if (!canMakeRequest(queuedRequest)) {
return;
}
HttpUtils.xhr(queuedRequest.command, finalParameters, queuedRequest.type, queuedRequest.shouldUseSecure)
.then(response => onResponse(queuedRequest, response))
.catch(error => onError(queuedRequest, error));
});
// We should clear the NETWORK_REQUEST_QUEUE when we have loaded the persisted requests & they are processed.
// As multiple client will be sharing the same Queue and NETWORK_REQUEST_QUEUE is synchronized among clients,
// we only ask Leader client to clear the queue
if (ActiveClientManager.isClientTheLeader() && didLoadPersistedRequests) {
Onyx.set(ONYXKEYS.NETWORK_REQUEST_QUEUE, []);
}
// User could have bad connectivity and he can go offline multiple times
// thus we allow NETWORK_REQUEST_QUEUE to be processed multiple times but only after we have processed
// old requests in the NETWORK_REQUEST_QUEUE
didLoadPersistedRequests = false;
// We clear the request queue at the end by setting the queue to retryableRequests which will either have some
// requests we want to retry or an empty array
networkRequestQueue = requestsToProcessOnNextRun;
}
// Process our write queue very often
setInterval(processNetworkRequestQueue, 1000);
/**
* @param {Object} request
* @returns {Boolean}
*/
function canProcessRequestImmediately(request) {
return lodashGet(request, 'data.shouldProcessImmediately', true);
}
/**
* Perform a queued post request
*
* @param {String} command
* @param {*} [data]
* @param {String} [type]
* @param {Boolean} shouldUseSecure - Whether we should use the secure API
* @returns {Promise}
*/
function post(command, data = {}, type = CONST.NETWORK.METHOD.POST, shouldUseSecure = false) {
return new Promise((resolve, reject) => {
const request = {
command,
data,
type,
resolve,
reject,
shouldUseSecure,
};
// Add the request to a queue of actions to perform
networkRequestQueue.push(request);
if (!canProcessRequestImmediately(request)) {
return;
}
// Try to fire off the request as soon as it's queued so we don't add a delay to every queued command
processNetworkRequestQueue();
});
}
/**
* Prevent the network queue from being processed
*/
function pauseRequestQueue() {
isQueuePaused = true;
}
/**
* Allow the network queue to continue to be processed
*/
function unpauseRequestQueue() {
isQueuePaused = false;
}
/**
* Register a function that will accept all the parameters being sent in a request
* and will return a new set of parameters to send instead. Useful for adding data to every request
* like auth or CRSF tokens.
*
* @param {Function} callback
*/
function registerParameterEnhancer(callback) {
enhanceParameters = callback;
}
/**
* Clear the queue so all pending requests will be cancelled
*/
function clearRequestQueue() {
networkRequestQueue = [];
}
/**
* Register a method to call when the authToken expires
* @param {Function} callback
*/
function registerResponseHandler(callback) {
onResponse = callback;
}
/**
* The error handler will handle fetch() errors. Not used for successful responses that might send expected error codes
* e.g. jsonCode: 407.
* @param {Function} callback
*/
function registerErrorHandler(callback) {
onError = callback;
}
export {
post,
pauseRequestQueue,
unpauseRequestQueue,
registerParameterEnhancer,
clearRequestQueue,
registerResponseHandler,
registerErrorHandler,
};