-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathNetworkStore.js
134 lines (116 loc) · 2.7 KB
/
NetworkStore.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
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import ONYXKEYS from '../../ONYXKEYS';
import * as NetworkEvents from './NetworkEvents';
let credentials;
let authToken;
let currentUserEmail;
let hasReadRequiredData = false;
let isAuthenticating = false;
let isOffline = false;
/**
* @param {Boolean} val
*/
function setHasReadRequiredDataFromStorage(val) {
hasReadRequiredData = val;
}
/**
* This is a hack to workaround the fact that Onyx may not yet have read these values from storage by the time Network starts processing requests.
* If the values are undefined we haven't read them yet. If they are null or have a value then we have and the network is "ready".
*/
function checkRequiredData() {
if (_.isUndefined(authToken) || _.isUndefined(credentials)) {
return;
}
setHasReadRequiredDataFromStorage(true);
}
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
authToken = lodashGet(val, 'authToken', null);
currentUserEmail = lodashGet(val, 'email', null);
checkRequiredData();
},
});
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (val) => {
credentials = val || null;
checkRequiredData();
},
});
// We subscribe 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: (network) => {
if (!network) {
return;
}
// Client becomes online emit connectivity resumed event
if (isOffline && !network.isOffline) {
NetworkEvents.triggerConnectivityResumed();
}
isOffline = network.isOffline;
},
});
/**
* @returns {Boolean}
*/
function getIsOffline() {
return isOffline;
}
/**
* @returns {String}
*/
function getAuthToken() {
return authToken;
}
/**
* @param {String} newAuthToken
*/
function setAuthToken(newAuthToken) {
authToken = newAuthToken;
}
/**
* @returns {Object}
*/
function getCredentials() {
return credentials;
}
/**
* @returns {String}
*/
function getCurrentUserEmail() {
return currentUserEmail;
}
/**
* @returns {Boolean}
*/
function hasReadRequiredDataFromStorage() {
return hasReadRequiredData;
}
/**
* @param {Boolean} value
*/
function setIsAuthenticating(value) {
isAuthenticating = value;
}
/**
* @returns {Boolean}
*/
function getIsAuthenticating() {
return isAuthenticating;
}
export {
getAuthToken,
setAuthToken,
getCredentials,
getCurrentUserEmail,
hasReadRequiredDataFromStorage,
setHasReadRequiredDataFromStorage,
setIsAuthenticating,
getIsAuthenticating,
getIsOffline,
};