async setCurrentClient()

in common/lib/plugin_service.ts [414:479]


  async setCurrentClient(newClient: ClientWrapper, hostInfo: HostInfo): Promise<Set<HostChangeOptions>> {
    if (!this.getCurrentClient().targetClient) {
      this.getCurrentClient().targetClient = newClient;
      this._currentHostInfo = hostInfo;
      this.sessionStateService.reset();
      const changes = new Set<HostChangeOptions>([HostChangeOptions.INITIAL_CONNECTION]);

      if (this.pluginServiceManagerContainer.pluginManager) {
        await this.pluginServiceManagerContainer.pluginManager.notifyConnectionChanged(changes, null);
      }

      return changes;
    } else {
      if (this._currentHostInfo) {
        const oldClient = this.getCurrentClient().targetClient;
        const changes: Set<HostChangeOptions> = this.compare(this._currentHostInfo, newClient.hostInfo, oldClient!, newClient);

        if (changes.size > 0) {
          const isInTransaction = this.isInTransaction();
          this.sessionStateService.begin();

          try {
            this.getCurrentClient().targetClient = newClient;
            this._currentHostInfo = hostInfo;
            await this.sessionStateService.applyCurrentSessionState(this.getCurrentClient());
            this.setInTransaction(false);

            if (oldClient && (isInTransaction || WrapperProperties.ROLLBACK_ON_SWITCH.get(this.props))) {
              try {
                await oldClient.rollback();
              } catch (error: any) {
                // Ignore.
              }
            }

            const pluginOpinions: Set<OldConnectionSuggestionAction> =
              await this.pluginServiceManagerContainer.pluginManager!.notifyConnectionChanged(changes, null);

            const shouldCloseConnection =
              changes.has(HostChangeOptions.CONNECTION_OBJECT_CHANGED) &&
              !pluginOpinions.has(OldConnectionSuggestionAction.PRESERVE) &&
              oldClient &&
              (await this.isClientValid(oldClient));

            if (shouldCloseConnection) {
              try {
                await this.sessionStateService.applyPristineSessionState(this.getCurrentClient());
              } catch (error: any) {
                // Ignore.
              }

              try {
                await this.abortTargetClient(oldClient);
              } catch (error: any) {
                // Ignore.
              }
            }
          } finally {
            this.sessionStateService.complete();
          }
        }
        return changes;
      }
      throw new AwsWrapperError(Messages.get("HostInfo.noHostParameter")); // Should not be reached
    }
  }