Skip to content
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

Merged
Merged
Changes from 1 commit
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
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({
Copy link
Contributor

@rezkiy37 rezkiy37 Sep 22, 2023

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.

Suggested change
let authToken: string | undefined | null;
let authToken: string | null;

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let refreshTimeoutID: NodeJS.Timeout;
let refreshTimeoutID: NodeJS.Timeout | undefined;

const REFRESH_INTERVAL = 1000 * 60 * 25;

Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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');
Expand All @@ -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.
Expand All @@ -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;
}
Expand Down Expand Up @@ -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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename and use value name.

Suggested change
if (network?.isOffline && val && !val.isOffline && !isCurrentlyFetchingToken && hasTokenExpired()) {
if (network?.isOffline && val && !value.isOffline && !isCurrentlyFetchingToken && hasTokenExpired()) {

clearToken();
}
Expand Down
Loading