Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 54 additions & 17 deletions src/libs/actions/MapboxToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ Onyx.connect({
},
});

let connectionID;
let connectionIDForToken;
let connectionIDForNetwork;
let appStateSubscription;
let currentToken;
let refreshTimeoutID;
let isCurrentlyFetchingToken = false;
const REFRESH_INTERVAL = 1000 * 60 * 25;

const setExpirationTimer = () => {
Expand Down Expand Up @@ -48,13 +51,13 @@ const clearToken = () => {
};

const init = () => {
if (connectionID) {
if (connectionIDForToken) {
console.debug('[MapboxToken] init() is already listening to Onyx so returning early');
return;
}

// When the token changes in Onyx, the expiration needs to be checked so a new token can be retrieved.
connectionID = Onyx.connect({
connectionIDForToken = Onyx.connect({
key: ONYXKEYS.MAPBOX_ACCESS_TOKEN,
/**
* @param {Object} token
Expand All @@ -73,7 +76,8 @@ const init = () => {
// The API sets a token in Onyx with a 30 minute expiration.
if (_.isEmpty(token)) {
console.debug('[MapboxToken] Token does not exist so fetching one');
API.read('GetMapboxAccessToken');
API.write('GetMapboxAccessToken');
isCurrentlyFetchingToken = true;
return;
}

Expand All @@ -88,21 +92,54 @@ const init = () => {

console.debug('[MapboxToken] Token is valid, setting up refresh');
setExpirationTimer();
isCurrentlyFetchingToken = false;
},
});

AppState.addEventListener('change', (nextAppState) => {
// Skip getting a new token if:
// - The app state is not changing to active
// - There is no current token (which means it's not been fetch yet for the first time)
// - The token hasn't expired yet (this would just be a waste of an API call)
// - There is no authToken (which means the user has logged out)
if (nextAppState !== CONST.APP_STATE.ACTIVE || !currentToken || !hasTokenExpired() || !authToken) {
return;
}
console.debug('[MapboxToken] Token is expired after app became active');
clearToken();
});
if (!appStateSubscription) {
appStateSubscription = AppState.addEventListener('change', (nextAppState) => {
// Skip getting a new token if:
// - The app state is not changing to active
// - There is no current token (which means it's not been fetch yet for the first time)
// - The token hasn't expired yet (this would just be a waste of an API call)
// - There is no authToken (which means the user has logged out)
if (nextAppState !== CONST.APP_STATE.ACTIVE || !currentToken || !hasTokenExpired() || !authToken || isCurrentlyFetchingToken) {
return;
}
console.debug('[MapboxToken] Token is expired after app became active');
clearToken();
});
}

if (!connectionIDForNetwork) {
let network;
connectionIDForNetwork = Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (val) => {
// When the network reconnects, check if the token has expired. If it has, then clearing the token will
// trigger the fetch of a new one
if (network && network.isOffline && val && !val.isOffline && !isCurrentlyFetchingToken && hasTokenExpired()) {
console.debug('[MapboxToken] Token is expired after network came online');
clearToken();
}
network = val;
},
});
}
};

const stop = () => {
console.debug('[MapboxToken] Stopping all listeners and timers');
if (connectionIDForToken) {
Onyx.disconnect(connectionIDForToken);
Comment thread
hayata-suenaga marked this conversation as resolved.
}
if (connectionIDForNetwork) {
Onyx.disconnect(connectionIDForNetwork);
}
if (appStateSubscription) {
appStateSubscription.remove();
}
clearTimeout(refreshTimeoutID);
};

export default init;
export {init, stop};