1 | import { Connection, NotificationType } from "vscode-languageserver";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export interface LoadingHandler {
|
7 | handle<T>(message: string, value: Promise<T>): Promise<T>;
|
8 | handleSync<T>(message: string, value: () => T): T;
|
9 | showError(message: string): void;
|
10 | }
|
11 |
|
12 | export class LanguageServerLoadingHandler implements LoadingHandler {
|
13 | constructor(private connection: Connection) {}
|
14 | private latestLoadingToken = 0;
|
15 | async handle<T>(message: string, value: Promise<T>): Promise<T> {
|
16 | const token = this.latestLoadingToken;
|
17 | this.latestLoadingToken += 1;
|
18 | this.connection.sendNotification(
|
19 | new NotificationType<any>("apollographql/loading"),
|
20 | { message, token }
|
21 | );
|
22 | try {
|
23 | const ret = await value;
|
24 | this.connection.sendNotification(
|
25 | new NotificationType<any>("apollographql/loadingComplete"),
|
26 | token
|
27 | );
|
28 | return ret;
|
29 | } catch (e) {
|
30 | this.connection.sendNotification(
|
31 | new NotificationType<any>("apollographql/loadingComplete"),
|
32 | token
|
33 | );
|
34 | this.showError(`Error in "${message}": ${e}`);
|
35 | throw e;
|
36 | }
|
37 | }
|
38 | handleSync<T>(message: string, value: () => T): T {
|
39 | const token = this.latestLoadingToken;
|
40 | this.latestLoadingToken += 1;
|
41 | this.connection.sendNotification(
|
42 | new NotificationType<any>("apollographql/loading"),
|
43 | { message, token }
|
44 | );
|
45 | try {
|
46 | const ret = value();
|
47 | this.connection.sendNotification(
|
48 | new NotificationType<any>("apollographql/loadingComplete"),
|
49 | token
|
50 | );
|
51 | return ret;
|
52 | } catch (e) {
|
53 | this.connection.sendNotification(
|
54 | new NotificationType<any>("apollographql/loadingComplete"),
|
55 | token
|
56 | );
|
57 | this.showError(`Error in "${message}": ${e}`);
|
58 | throw e;
|
59 | }
|
60 | }
|
61 | showError(message: string) {
|
62 | this.connection.window.showErrorMessage(message);
|
63 | }
|
64 | }
|
65 |
|
\ | No newline at end of file |