-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.js
72 lines (62 loc) · 2.55 KB
/
index.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
import lodashGet from 'lodash/get';
import * as ActiveClientManager from '../ActiveClientManager';
import CONST from '../../CONST';
import * as MainQueue from './MainQueue';
import * as SequentialQueue from './SequentialQueue';
import {version} from '../../../package.json';
// We must wait until the ActiveClientManager is ready so that we ensure only the "leader" tab processes any persisted requests
ActiveClientManager.isReady().then(() => {
SequentialQueue.flush();
// Start main queue and process once every n ms delay
setInterval(MainQueue.process, CONST.NETWORK.PROCESS_REQUEST_DELAY_MS);
});
/**
* 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,
shouldUseSecure,
};
// By default, request are retry-able and cancellable
// (e.g. any requests currently happening when the user logs out are cancelled)
request.data = {
...data,
shouldRetry: lodashGet(data, 'shouldRetry', true),
canCancel: lodashGet(data, 'canCancel', true),
appversion: version,
};
const shouldPersist = lodashGet(request, 'data.persist', false);
if (shouldPersist) {
SequentialQueue.push(request);
return;
}
// Add promise handlers to any request that we are not persisting
request.resolve = resolve;
request.reject = reject;
// Add the request to a queue of actions to perform
MainQueue.push(request);
// This check is mainly used to prevent API commands from triggering calls to MainQueue.process() from inside the context of a previous
// call to MainQueue.process() e.g. calling a Log command without this would cause the requests in mainQueue to double process
// since we call Log inside MainQueue.process().
const shouldProcessImmediately = lodashGet(request, 'data.shouldProcessImmediately', true);
if (!shouldProcessImmediately) {
return;
}
// Try to fire off the request as soon as it's queued so we don't add a delay to every queued command
MainQueue.process();
});
}
export {
// eslint-disable-next-line import/prefer-default-export
post,
};