-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathmonitor-connection.ts
60 lines (48 loc) · 2.11 KB
/
monitor-connection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { injectable, inject } from "inversify";
import { MonitorService, ConnectionConfig } from "../../common/protocol/monitor-service";
import { Emitter, Event } from "@theia/core";
@injectable()
export class MonitorConnection {
@inject(MonitorService)
protected readonly monitorService: MonitorService;
protected _connectionId: string | undefined;
protected _connectionConfig: ConnectionConfig;
protected readonly onConnectionChangedEmitter = new Emitter<string | undefined>();
readonly onConnectionChanged: Event<string | undefined> = this.onConnectionChangedEmitter.event;
get connectionId(): string | undefined {
return this._connectionId;
}
set connectionId(cid: string | undefined) {
this._connectionId = cid;
}
get connectionConfig(): ConnectionConfig {
return this._connectionConfig;
}
async connect(config: ConnectionConfig): Promise<string | undefined> {
if (this._connectionId) {
await this.disconnect();
}
const { connectionId } = await this.monitorService.connect(config);
this._connectionId = connectionId;
this._connectionConfig = config;
this.onConnectionChangedEmitter.fire(this._connectionId);
return connectionId;
}
async disconnect(): Promise<boolean> {
let result = true;
const connections = await this.monitorService.getConnectionIds();
if (this._connectionId && connections.findIndex(id => id === this._connectionId) >= 0) {
console.log('>>> Disposing existing monitor connection before establishing a new one...');
result = await this.monitorService.disconnect(this._connectionId);
if (!result) {
// TODO: better!!!
console.error(`Could not close connection: ${this._connectionId}. Check the backend logs.`);
} else {
console.log(`<<< Disposed ${this._connectionId} connection.`);
this._connectionId = undefined;
}
this.onConnectionChangedEmitter.fire(this._connectionId);
}
return result;
}
}