Skip to content

Commit

Permalink
chore: refactor timeout code
Browse files Browse the repository at this point in the history
  • Loading branch information
asafshen committed Jan 13, 2025
1 parent 14787ca commit 897fe1a
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { addHooks, getAuthInfoFromResponse } from '../helpers';
import {
createTimerFunctions,
getTokenExpiration,
millisecondsUntilDate,
getAutoRefreshTimeout,
} from './helpers';
import { AutoRefreshOptions } from './types';
import logger from '../helpers/logger';
import { IS_BROWSER, MAX_TIMEOUT } from '../../constants';
import { IS_BROWSER } from '../../constants';
import { getRefreshToken } from '../withPersistTokens/helpers';

// The amount of time (ms) to trigger the refresh before session expires
const REFRESH_THRESHOLD = 20 * 1000; // 20 sec

/**
* Automatically refresh the session token before it expires
* It uses the the refresh token that is extracted from API response to do that
Expand Down Expand Up @@ -62,15 +59,8 @@ export const withAutoRefresh =
return;
}
refreshToken = refreshJwt;
let timeout =
millisecondsUntilDate(sessionExpiration) - REFRESH_THRESHOLD;
const timeout = getAutoRefreshTimeout(sessionExpiration);

if (timeout > MAX_TIMEOUT) {
logger.debug(
`Timeout is too large (${timeout}ms), setting it to ${MAX_TIMEOUT}ms`,
);
timeout = MAX_TIMEOUT;
}
clearAllTimers();

const refreshTimeStr = new Date(
Expand Down

0 comments on commit 897fe1a

Please sign in to comment.