async refreshIfNeeded()

in src/desktop/gitlab/token_exchange_service.ts [37:61]


  async refreshIfNeeded(accountId: string): Promise<Account> {
    // before we start refreshing token, let's check if some other VS Code instance already has refreshed it
    await this.#accountService.reloadCache();
    const latestAccount = this.#accountService.getAccount(accountId);
    // this would happen if another VS Code Window deleted the account
    assert(latestAccount, `Account with id ${accountId} doesn't exist.`);
    if (latestAccount.type === 'token') return latestAccount;
    if (!needsRefresh(latestAccount)) {
      return latestAccount;
    }
    const refreshInProgress = this.#refreshesInProgress[accountId];
    if (refreshInProgress) {
      log.info(
        `[auth] Token refresh already in progress for account ${accountId}, reusing existing promise`,
      );
      return refreshInProgress;
    }
    log.info(`[auth]Refreshing expired token for account ${serializeAccountSafe(latestAccount)}`);
    const refresh = this.#refreshToken(latestAccount).finally(() => {
      // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
      delete this.#refreshesInProgress[accountId];
    });
    this.#refreshesInProgress[accountId] = refresh;
    return refresh;
  }