Skip to content

Commit

Permalink
Merge pull request #27792 from VickyStash/ts-migration/activeClientMa…
Browse files Browse the repository at this point in the history
…nager-lib

[No QA] [TS migration] Migrate 'ActiveClientManager' lib to TypeScript
  • Loading branch information
MariaHCD authored Oct 6, 2023
2 parents edea48c + 418ebf3 commit 779a1e5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
14 changes: 0 additions & 14 deletions src/libs/ActiveClientManager/index.native.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/libs/ActiveClientManager/index.native.ts
Original file line number Diff line number Diff line change
@@ -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};
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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,
Expand Down Expand Up @@ -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};
7 changes: 7 additions & 0 deletions src/libs/ActiveClientManager/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Init = () => void;

type IsClientTheLeader = () => boolean;

type IsReady = () => Promise<void>;

export type {Init, IsClientTheLeader, IsReady};

0 comments on commit 779a1e5

Please sign in to comment.