diff --git a/src/libs/ActiveClientManager/index.native.js b/src/libs/ActiveClientManager/index.native.js deleted file mode 100644 index c86f847a1a6e..000000000000 --- a/src/libs/ActiveClientManager/index.native.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * For native devices, there will never be more than one - * client running at a time, so this lib is a big no-op - */ - -function init() {} -function isClientTheLeader() { - return true; -} -function isReady() { - return Promise.resolve(); -} - -export {init, isReady, isClientTheLeader}; diff --git a/src/libs/ActiveClientManager/index.native.ts b/src/libs/ActiveClientManager/index.native.ts new file mode 100644 index 000000000000..1d455a84a28a --- /dev/null +++ b/src/libs/ActiveClientManager/index.native.ts @@ -0,0 +1,14 @@ +/** + * For native devices, there will never be more than one + * client running at a time, so this lib is a big no-op + */ + +import {Init, IsReady, IsClientTheLeader} from './types'; + +const init: Init = () => {}; + +const isClientTheLeader: IsClientTheLeader = () => true; + +const isReady: IsReady = () => Promise.resolve(); + +export {init, isClientTheLeader, isReady}; diff --git a/src/libs/ActiveClientManager/index.js b/src/libs/ActiveClientManager/index.ts similarity index 77% rename from src/libs/ActiveClientManager/index.js rename to src/libs/ActiveClientManager/index.ts index 2117e5dac373..f99f54e84aa5 100644 --- a/src/libs/ActiveClientManager/index.js +++ b/src/libs/ActiveClientManager/index.ts @@ -4,27 +4,24 @@ * This file ensures exactly that by tracking all the clientIDs connected, storing the most recent one last and it considers that last clientID the "leader". */ -import _ from 'underscore'; import Onyx from 'react-native-onyx'; import Str from 'expensify-common/lib/str'; import ONYXKEYS from '../../ONYXKEYS'; import * as ActiveClients from '../actions/ActiveClients'; +import {Init, IsReady, IsClientTheLeader} from './types'; const clientID = Str.guid(); const maxClients = 20; -let activeClients = []; -let resolveSavedSelfPromise; -const savedSelfPromise = new Promise((resolve) => { +let activeClients: string[] = []; +let resolveSavedSelfPromise: () => void; +const savedSelfPromise = new Promise((resolve) => { resolveSavedSelfPromise = resolve; }); /** * Determines when the client is ready. We need to wait both till we saved our ID in onyx AND the init method was called - * @returns {Promise} */ -function isReady() { - return savedSelfPromise; -} +const isReady: IsReady = () => savedSelfPromise; Onyx.connect({ key: ONYXKEYS.ACTIVE_CLIENTS, @@ -53,19 +50,19 @@ Onyx.connect({ * Add our client ID to the list of active IDs. * We want to ensure we have no duplicates and that the activeClient gets added at the end of the array (see isClientTheLeader) */ -function init() { - activeClients = _.without(activeClients, clientID); +const init: Init = () => { + activeClients = activeClients.filter((id) => id !== clientID); activeClients.push(clientID); ActiveClients.setActiveClients(activeClients).then(resolveSavedSelfPromise); -} +}; /** * The last GUID is the most recent GUID, so that should be the leader - * - * @returns {Boolean} */ -function isClientTheLeader() { - return _.last(activeClients) === clientID; -} +const isClientTheLeader: IsClientTheLeader = () => { + const lastActiveClient = activeClients.length && activeClients[activeClients.length - 1]; + + return lastActiveClient === clientID; +}; export {init, isClientTheLeader, isReady}; diff --git a/src/libs/ActiveClientManager/types.ts b/src/libs/ActiveClientManager/types.ts new file mode 100644 index 000000000000..a5c7d1e6d721 --- /dev/null +++ b/src/libs/ActiveClientManager/types.ts @@ -0,0 +1,7 @@ +type Init = () => void; + +type IsClientTheLeader = () => boolean; + +type IsReady = () => Promise; + +export type {Init, IsClientTheLeader, IsReady};