-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Network.js
255 lines (221 loc) · 8.34 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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import HttpUtils from './HttpUtils';
import ONYXKEYS from '../ONYXKEYS';
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 = () => {};
// 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.
let isOffline;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: val => isOffline = val && val.isOffline,
});
let didLoadPersistedRequests;
Onyx.connect({
key: ONYXKEYS.NETWORK_REQUEST_QUEUE,
callback: (persistedRequests) => {
if (didLoadPersistedRequests || !persistedRequests) {
return;
}
// 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;
},
});
// 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;
}
// If we have a request then we need to check if it can be persisted in case we close the tab while offline
const retryableRequests = _.filter(networkRequestQueue, request => (
!request.data.doNotRetry && request.data.persist
));
Onyx.set(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 = 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 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);
/**
* 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) => {
// Add the write request to a queue of actions to perform
networkRequestQueue.push({
command,
data,
type,
resolve,
reject,
shouldUseSecure,
});
// 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,
};