From 98dbc4aff23626a061d923945c2fb2293bf7ce30 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Fri, 22 Sep 2023 10:52:12 +0200 Subject: [PATCH 1/3] fix: added types to MapboxToken lib --- .../{MapboxToken.js => MapboxToken.ts} | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) rename src/libs/actions/{MapboxToken.js => MapboxToken.ts} (85%) diff --git a/src/libs/actions/MapboxToken.js b/src/libs/actions/MapboxToken.ts similarity index 85% rename from src/libs/actions/MapboxToken.js rename to src/libs/actions/MapboxToken.ts index 2ce56eb1a11e..45441ec8c5ca 100644 --- a/src/libs/actions/MapboxToken.js +++ b/src/libs/actions/MapboxToken.ts @@ -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; 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); }; -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'); clearToken(); } From 255fbab47a128f61f0b0324199b7009f21ccb3dd Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 28 Sep 2023 13:14:33 +0200 Subject: [PATCH 2/3] fix: resolve merge conflicts --- src/libs/actions/MapboxToken.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/MapboxToken.ts b/src/libs/actions/MapboxToken.ts index 3ed8affc230b..e0d979edb74e 100644 --- a/src/libs/actions/MapboxToken.ts +++ b/src/libs/actions/MapboxToken.ts @@ -118,10 +118,10 @@ const init = () => { let network: Network | null; connectionIDForNetwork = Onyx.connect({ key: ONYXKEYS.NETWORK, - callback: (val) => { + callback: (value) => { // 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) { + if (network && network.isOffline && value && !value.isOffline) { if (Object.keys(currentToken ?? {}).length === 0) { fetchToken(); } else if (!isCurrentlyFetchingToken && hasTokenExpired()) { @@ -129,7 +129,7 @@ const init = () => { clearToken(); } } - network = val; + network = value; }, }); } From 1632cbc874ec2d31bc3bbc2b3c13d7259f5eaa4b Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Mon, 9 Oct 2023 10:16:27 +0200 Subject: [PATCH 3/3] fix: resolve comments --- src/libs/actions/MapboxToken.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/MapboxToken.ts b/src/libs/actions/MapboxToken.ts index 3ddba151e80f..f795adf0df2b 100644 --- a/src/libs/actions/MapboxToken.ts +++ b/src/libs/actions/MapboxToken.ts @@ -1,4 +1,3 @@ -// import _ from 'underscore'; import {isAfter} from 'date-fns'; import Onyx from 'react-native-onyx'; import {AppState, NativeEventSubscription} from 'react-native'; @@ -8,7 +7,7 @@ import CONST from '../../CONST'; import * as ActiveClientManager from '../ActiveClientManager'; import {MapboxAccessToken, Network} from '../../types/onyx'; -let authToken: string | undefined | null; +let authToken: string | null; Onyx.connect({ key: ONYXKEYS.SESSION, callback: (value) => { @@ -20,7 +19,7 @@ let connectionIDForToken: number | null; let connectionIDForNetwork: number | null; let appStateSubscription: NativeEventSubscription | null; let currentToken: MapboxAccessToken | null; -let refreshTimeoutID: NodeJS.Timeout; +let refreshTimeoutID: NodeJS.Timeout | undefined; let isCurrentlyFetchingToken = false; const REFRESH_INTERVAL = 1000 * 60 * 25;