Skip to content

Commit f922f6b

Browse files
authored
Merge pull request #2231 from codefori/fix/connect_disconnect_change
Change logic for triggering disconnect
2 parents c96c4f2 + 5600e53 commit f922f6b

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/api/IBMi.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,24 @@ export default class IBMi {
159159

160160
let tempLibrarySet = false;
161161

162-
const disconnected = async () => {
163-
const choice = await vscode.window.showWarningMessage(`Connection lost`, {
164-
modal: true,
165-
detail: `Connection to ${this.currentConnectionName} has dropped. Would you like to reconnect?`
166-
}, `Yes`);
162+
const timeoutHandler = async () => {
163+
if (!cancelToken.isCancellationRequested) {
164+
this.disconnect();
167165

168-
let disconnect = true;
169-
if (choice === `Yes`) {
170-
disconnect = !(await this.connect(connectionObject, true)).success;
171-
}
166+
const choice = await vscode.window.showWarningMessage(`Connection lost`, {
167+
modal: true,
168+
detail: `Connection to ${this.currentConnectionName} has dropped. Would you like to reconnect?`
169+
}, `Yes`);
172170

173-
if (disconnect) {
174-
this.end();
175-
};
171+
let disconnect = true;
172+
if (choice === `Yes`) {
173+
disconnect = !(await this.connect(connectionObject, true)).success;
174+
}
175+
176+
if (disconnect) {
177+
this.end();
178+
};
179+
}
176180
};
177181

178182
progress.report({
@@ -217,9 +221,9 @@ export default class IBMi {
217221
}
218222

219223
// Register handlers after we might have to abort due to bad configuration.
220-
this.client.connection!.once(`timeout`, disconnected);
221-
this.client.connection!.once(`end`, disconnected);
222-
this.client.connection!.once(`error`, disconnected);
224+
this.client.connection!.once(`timeout`, timeoutHandler);
225+
this.client.connection!.once(`end`, timeoutHandler);
226+
this.client.connection!.once(`error`, timeoutHandler);
223227

224228
if (!reconnecting) {
225229
instance.setConnection(this);
@@ -935,9 +939,10 @@ export default class IBMi {
935939
for (const operation of delayedOperations) {
936940
await operation();
937941
}
938-
instance.fire("connected");
939942
}
940943

944+
instance.fire(`connected`);
945+
941946
GlobalStorage.get().setServerSettingsCache(this.currentConnectionName, {
942947
aspInfo: this.aspInfo,
943948
qccsid: this.qccsid,
@@ -1104,9 +1109,17 @@ export default class IBMi {
11041109
this.commandsExecuted += 1;
11051110
}
11061111

1107-
async end() {
1112+
private async disconnect() {
11081113
this.client.connection?.removeAllListeners();
11091114
this.client.dispose();
1115+
this.client.connection = null;
1116+
instance.fire(`disconnected`);
1117+
}
1118+
1119+
async end() {
1120+
if (this.client.connection) {
1121+
this.disconnect();
1122+
}
11101123

11111124
if (this.outputChannel) {
11121125
this.outputChannel.hide();
@@ -1124,7 +1137,6 @@ export default class IBMi {
11241137
]);
11251138

11261139
instance.setConnection(undefined);
1127-
instance.fire(`disconnected`);
11281140
await vscode.commands.executeCommand(`setContext`, `code-for-ibmi:connected`, false);
11291141
vscode.window.showInformationMessage(`Disconnected from ${this.currentHost}.`);
11301142
}

0 commit comments

Comments
 (0)