Skip to content

Commit

Permalink
Merge pull request Expensify#26808 from arthurmfgtab/ts-migration/Req…
Browse files Browse the repository at this point in the history
…uestThrottle

[TS migration] Migrate 'RequestThrottle.js' lib to TypeScript Expensify#24807
  • Loading branch information
aldo-expensify authored Sep 12, 2023
2 parents b03e9d7 + ce0a588 commit 764e6c9
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/libs/RequestThrottle.js → src/libs/RequestThrottle.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import _ from 'underscore';
import CONST from '../CONST';
import {generateRandomInt} from './NumberUtils';

let requestWaitTime = 0;

function clear() {
requestWaitTime = 0;
}

/**
* @returns {Number} time to wait in ms
*/
function getRequestWaitTime() {
if (requestWaitTime) {
requestWaitTime = Math.min(requestWaitTime * 2, CONST.NETWORK.MAX_RETRY_WAIT_TIME_MS);
} else {
requestWaitTime = _.random(CONST.NETWORK.MIN_RETRY_WAIT_TIME_MS, CONST.NETWORK.MAX_RANDOM_RETRY_WAIT_TIME_MS);
requestWaitTime = generateRandomInt(CONST.NETWORK.MIN_RETRY_WAIT_TIME_MS, CONST.NETWORK.MAX_RANDOM_RETRY_WAIT_TIME_MS);
}
return requestWaitTime;
}

/**
* @returns {Promise}
*/
function sleep() {
function sleep(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, getRequestWaitTime()));
}

Expand Down

0 comments on commit 764e6c9

Please sign in to comment.