-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[No QA] [TS migration] Migrate 'MapboxToken.js' lib to TypeScript #27999
Changes from 1 commit
98dbc4a
09c5e27
255fbab
4c6d24a
1632cbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,26 +1,25 @@ | ||||||
import _ from 'underscore'; | ||||||
import moment from 'moment'; | ||||||
import Onyx from 'react-native-onyx'; | ||||||
import {AppState} from 'react-native'; | ||||||
import lodashGet from 'lodash/get'; | ||||||
import {AppState, NativeEventSubscription} from 'react-native'; | ||||||
import ONYXKEYS from '../../ONYXKEYS'; | ||||||
import * as API from '../API'; | ||||||
import CONST from '../../CONST'; | ||||||
import * as ActiveClientManager from '../ActiveClientManager'; | ||||||
import {MapboxAccessToken, Network} from '../../types/onyx'; | ||||||
|
||||||
let authToken; | ||||||
let authToken: string | undefined | null; | ||||||
Onyx.connect({ | ||||||
key: ONYXKEYS.SESSION, | ||||||
callback: (val) => { | ||||||
authToken = lodashGet(val, 'authToken', null); | ||||||
callback: (value) => { | ||||||
authToken = value?.authToken ?? null; | ||||||
}, | ||||||
}); | ||||||
|
||||||
let connectionIDForToken; | ||||||
let connectionIDForNetwork; | ||||||
let appStateSubscription; | ||||||
let currentToken; | ||||||
let refreshTimeoutID; | ||||||
let connectionIDForToken: number | null; | ||||||
let connectionIDForNetwork: number | null; | ||||||
let appStateSubscription: NativeEventSubscription | null; | ||||||
let currentToken: MapboxAccessToken | null; | ||||||
let refreshTimeoutID: NodeJS.Timeout; | ||||||
let isCurrentlyFetchingToken = false; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const REFRESH_INTERVAL = 1000 * 60 * 25; | ||||||
|
||||||
|
@@ -38,11 +37,11 @@ const setExpirationTimer = () => { | |||||
return; | ||||||
} | ||||||
console.debug(`[MapboxToken] Fetching a new token after waiting ${REFRESH_INTERVAL / 1000 / 60} minutes`); | ||||||
API.read('GetMapboxAccessToken'); | ||||||
API.read('GetMapboxAccessToken', {}, {}); | ||||||
}, REFRESH_INTERVAL); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please test the map extensively in your PR and ensure this won't cause regressions 🙂 |
||||||
}; | ||||||
|
||||||
const hasTokenExpired = () => moment().isAfter(currentToken.expiration); | ||||||
const hasTokenExpired = () => moment().isAfter(currentToken?.expiration); | ||||||
|
||||||
const clearToken = () => { | ||||||
console.debug('[MapboxToken] Deleting the token stored in Onyx'); | ||||||
|
@@ -60,12 +59,6 @@ const init = () => { | |||||
// When the token changes in Onyx, the expiration needs to be checked so a new token can be retrieved. | ||||||
connectionIDForToken = Onyx.connect({ | ||||||
key: ONYXKEYS.MAPBOX_ACCESS_TOKEN, | ||||||
/** | ||||||
* @param {Object} token | ||||||
* @param {String} token.token | ||||||
* @param {String} token.expiration | ||||||
* @param {String[]} [token.errors] | ||||||
*/ | ||||||
callback: (token) => { | ||||||
// Only the leader should be in charge of the mapbox token, or else when you have multiple tabs open, the Onyx connection fires multiple times | ||||||
// and it sets up duplicate refresh timers. This would be a big waste of tokens. | ||||||
|
@@ -82,9 +75,9 @@ const init = () => { | |||||
|
||||||
// If the token is falsy or an empty object, the token needs to be retrieved from the API. | ||||||
// The API sets a token in Onyx with a 30 minute expiration. | ||||||
if (_.isEmpty(token)) { | ||||||
if (Object.keys(token ?? {}).length === 0) { | ||||||
console.debug('[MapboxToken] Token does not exist so fetching one'); | ||||||
API.read('GetMapboxAccessToken'); | ||||||
API.read('GetMapboxAccessToken', {}, {}); | ||||||
isCurrentlyFetchingToken = true; | ||||||
return; | ||||||
} | ||||||
|
@@ -120,13 +113,13 @@ const init = () => { | |||||
} | ||||||
|
||||||
if (!connectionIDForNetwork) { | ||||||
let network; | ||||||
let network: Network | null; | ||||||
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()) { | ||||||
if (network?.isOffline && val && !val.isOffline && !isCurrentlyFetchingToken && hasTokenExpired()) { | ||||||
console.debug('[MapboxToken] Token is expired after network came online'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rename and use
Suggested change
|
||||||
clearToken(); | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see below you check for
undefined
, so this type is never been acceptable.