Skip to content

Commit

Permalink
Merge pull request #2231 from codefori/fix/connect_disconnect_change
Browse files Browse the repository at this point in the history
Change logic for triggering disconnect
  • Loading branch information
worksofliam committed Aug 28, 2024
2 parents c96c4f2 + 5600e53 commit f922f6b
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/api/IBMi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,24 @@ export default class IBMi {

let tempLibrarySet = false;

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

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

if (disconnect) {
this.end();
};
let disconnect = true;
if (choice === `Yes`) {
disconnect = !(await this.connect(connectionObject, true)).success;
}

if (disconnect) {
this.end();
};
}
};

progress.report({
Expand Down Expand Up @@ -217,9 +221,9 @@ export default class IBMi {
}

// Register handlers after we might have to abort due to bad configuration.
this.client.connection!.once(`timeout`, disconnected);
this.client.connection!.once(`end`, disconnected);
this.client.connection!.once(`error`, disconnected);
this.client.connection!.once(`timeout`, timeoutHandler);
this.client.connection!.once(`end`, timeoutHandler);
this.client.connection!.once(`error`, timeoutHandler);

if (!reconnecting) {
instance.setConnection(this);
Expand Down Expand Up @@ -935,9 +939,10 @@ export default class IBMi {
for (const operation of delayedOperations) {
await operation();
}
instance.fire("connected");
}

instance.fire(`connected`);

GlobalStorage.get().setServerSettingsCache(this.currentConnectionName, {
aspInfo: this.aspInfo,
qccsid: this.qccsid,
Expand Down Expand Up @@ -1104,9 +1109,17 @@ export default class IBMi {
this.commandsExecuted += 1;
}

async end() {
private async disconnect() {
this.client.connection?.removeAllListeners();
this.client.dispose();
this.client.connection = null;
instance.fire(`disconnected`);
}

async end() {
if (this.client.connection) {
this.disconnect();
}

if (this.outputChannel) {
this.outputChannel.hide();
Expand All @@ -1124,7 +1137,6 @@ export default class IBMi {
]);

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

0 comments on commit f922f6b

Please sign in to comment.