HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/node_modules/apollo-language-server/src/loadingHandler.ts
import { IConnection, NotificationType } from "vscode-languageserver";

// XXX I think we want to combine this into an interface
// with the errors tooling as well

export interface LoadingHandler {
  handle<T>(message: string, value: Promise<T>): Promise<T>;
  handleSync<T>(message: string, value: () => T): T;
  showError(message: string): void;
}

export class LanguageServerLoadingHandler implements LoadingHandler {
  constructor(private connection: IConnection) {}
  private latestLoadingToken = 0;
  async handle<T>(message: string, value: Promise<T>): Promise<T> {
    const token = this.latestLoadingToken;
    this.latestLoadingToken += 1;
    this.connection.sendNotification(
      new NotificationType<any, void>("apollographql/loading"),
      { message, token }
    );
    try {
      const ret = await value;
      this.connection.sendNotification(
        new NotificationType<any, void>("apollographql/loadingComplete"),
        token
      );
      return ret;
    } catch (e) {
      this.connection.sendNotification(
        new NotificationType<any, void>("apollographql/loadingComplete"),
        token
      );
      this.showError(`Error in "${message}": ${e}`);
      throw e;
    }
  }
  handleSync<T>(message: string, value: () => T): T {
    const token = this.latestLoadingToken;
    this.latestLoadingToken += 1;
    this.connection.sendNotification(
      new NotificationType<any, void>("apollographql/loading"),
      { message, token }
    );
    try {
      const ret = value();
      this.connection.sendNotification(
        new NotificationType<any, void>("apollographql/loadingComplete"),
        token
      );
      return ret;
    } catch (e) {
      this.connection.sendNotification(
        new NotificationType<any, void>("apollographql/loadingComplete"),
        token
      );
      this.showError(`Error in "${message}": ${e}`);
      throw e;
    }
  }
  showError(message: string) {
    this.connection.window.showErrorMessage(message);
  }
}