Skip to content

Commit f8cf2fc

Browse files
abstract more
1 parent 0b10323 commit f8cf2fc

1 file changed

Lines changed: 29 additions & 43 deletions

File tree

src/vs/workbench/contrib/debug/node/debugAdapter.ts

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -95,41 +95,26 @@ export abstract class NetworkDebugAdapter extends StreamDebugAdapter {
9595

9696
protected socket?: net.Socket;
9797

98-
abstract startSession(): Promise<void>;
99-
100-
async stopSession(): Promise<void> {
101-
await this.cancelPendingRequests();
102-
if (this.socket) {
103-
this.socket.end();
104-
this.socket = undefined;
105-
}
106-
}
107-
}
108-
109-
/**
110-
* An implementation that connects to a debug adapter via a socket.
111-
*/
112-
export class SocketDebugAdapter extends NetworkDebugAdapter {
113-
114-
constructor(private adapterServer: IDebugAdapterServer) {
115-
super();
116-
}
98+
protected abstract createConnection(connectionListener: () => void): net.Socket;
11799

118100
startSession(): Promise<void> {
119101
return new Promise<void>((resolve, reject) => {
120102
let connected = false;
121-
this.socket = net.createConnection(this.adapterServer.port, this.adapterServer.host || '127.0.0.1', () => {
103+
104+
this.socket = this.createConnection(() => {
122105
this.connect(this.socket!, this.socket!);
123106
resolve();
124107
connected = true;
125108
});
109+
126110
this.socket.on('close', () => {
127111
if (connected) {
128112
this._onError.fire(new Error('connection closed'));
129113
} else {
130114
reject(new Error('connection closed'));
131115
}
132116
});
117+
133118
this.socket.on('error', error => {
134119
if (connected) {
135120
this._onError.fire(error);
@@ -139,6 +124,28 @@ export class SocketDebugAdapter extends NetworkDebugAdapter {
139124
});
140125
});
141126
}
127+
128+
async stopSession(): Promise<void> {
129+
await this.cancelPendingRequests();
130+
if (this.socket) {
131+
this.socket.end();
132+
this.socket = undefined;
133+
}
134+
}
135+
}
136+
137+
/**
138+
* An implementation that connects to a debug adapter via a socket.
139+
*/
140+
export class SocketDebugAdapter extends NetworkDebugAdapter {
141+
142+
constructor(private adapterServer: IDebugAdapterServer) {
143+
super();
144+
}
145+
146+
protected createConnection(connectionListener: () => void): net.Socket {
147+
return net.createConnection(this.adapterServer.port, this.adapterServer.host || '127.0.0.1', connectionListener);
148+
}
142149
}
143150

144151
/**
@@ -150,29 +157,8 @@ export class NamedPipeDebugAdapter extends NetworkDebugAdapter {
150157
super();
151158
}
152159

153-
startSession(): Promise<void> {
154-
return new Promise<void>((resolve, reject) => {
155-
let connected = false;
156-
this.socket = net.createConnection(this.adapterServer.path, () => {
157-
this.connect(this.socket!, this.socket!);
158-
resolve();
159-
connected = true;
160-
});
161-
this.socket.on('close', () => {
162-
if (connected) {
163-
this._onError.fire(new Error('connection closed'));
164-
} else {
165-
reject(new Error('connection closed'));
166-
}
167-
});
168-
this.socket.on('error', error => {
169-
if (connected) {
170-
this._onError.fire(error);
171-
} else {
172-
reject(error);
173-
}
174-
});
175-
});
160+
protected createConnection(connectionListener: () => void): net.Socket {
161+
return net.createConnection(this.adapterServer.path, connectionListener);
176162
}
177163
}
178164

0 commit comments

Comments
 (0)