Skip to content

Commit 4854e40

Browse files
committed
Improvements to how connected and disconnected are fired
Signed-off-by: worksofliam <[email protected]>
1 parent a69402b commit 4854e40

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

src/api/IBMi.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { IBMiComponent } from "../components/component";
88
import { CopyToImport } from "../components/copyToImport";
99
import { CustomQSh } from '../components/cqsh';
1010
import { ComponentManager } from "../components/manager";
11-
import { instance } from "../instantiate";
1211
import { CommandData, CommandResult, ConnectionData, IBMiMember, RemoteCommand, SpecialAuthorities, WrapResult } from "../typings";
1312
import { CompileTools } from "./CompileTools";
1413
import { ConnectionConfiguration } from "./Configuration";
@@ -52,6 +51,8 @@ const remoteApps = [ // All names MUST also be defined as key in 'remoteFeatures
5251
}
5352
];
5453

54+
type DisconnectCallback = (conn: IBMi) => Promise<void>;
55+
5556
export default class IBMi {
5657
static readonly CCSID_NOCONVERSION = 65535;
5758
static readonly CCSID_SYSVAL = -2;
@@ -75,7 +76,7 @@ export default class IBMi {
7576
*/
7677
content = new IBMiContent(this);
7778

78-
client: node_ssh.NodeSSH;
79+
client: node_ssh.NodeSSH|undefined;
7980
currentHost: string = ``;
8081
currentPort: number = 22;
8182
currentUser: string = ``;
@@ -105,6 +106,15 @@ export default class IBMi {
105106
//Maximum admited length for command's argument - any command whose arguments are longer than this won't be executed by the shell
106107
maximumArgsLength = 0;
107108

109+
private disconnectedCallback: (DisconnectCallback)|undefined;
110+
111+
/**
112+
* Will only be called once per connection.
113+
*/
114+
setDisconnectedCallback(callback: DisconnectCallback) {
115+
this.disconnectedCallback = callback;
116+
}
117+
108118
get canUseCqsh() {
109119
return this.getComponent(CustomQSh.ID) !== undefined;
110120
}
@@ -158,8 +168,6 @@ export default class IBMi {
158168
}
159169

160170
constructor() {
161-
this.client = new node_ssh.NodeSSH;
162-
163171
this.remoteFeatures = {
164172
git: undefined,
165173
grep: undefined,
@@ -212,10 +220,11 @@ export default class IBMi {
212220
});
213221
const delayedOperations: Function[] = [...onConnectedOperations];
214222

215-
await this.client.connect({
216-
...connectionObject,
217-
privateKeyPath: connectionObject.privateKeyPath ? Tools.resolvePath(connectionObject.privateKeyPath) : undefined
218-
} as node_ssh.Config);
223+
this.client = new node_ssh.NodeSSH;
224+
await this.client.connect({
225+
...connectionObject,
226+
privateKeyPath: connectionObject.privateKeyPath ? Tools.resolvePath(connectionObject.privateKeyPath) : undefined
227+
} as node_ssh.Config);
219228

220229
cancelToken.onCancellationRequested(() => {
221230
this.dispose();
@@ -1095,7 +1104,7 @@ export default class IBMi {
10951104
});
10961105

10971106
} catch (e: any) {
1098-
this.disconnect();
1107+
this.disconnect(true);
10991108

11001109
let error = e.message;
11011110
if (e.code === "ENOTFOUND") {
@@ -1194,7 +1203,7 @@ export default class IBMi {
11941203
}
11951204
}
11961205

1197-
const result = await this.client.execCommand(command, {
1206+
const result = await this.client!.execCommand(command, {
11981207
cwd: directory,
11991208
stdin: options.stdin,
12001209
onStdout: options.onStdout,
@@ -1235,13 +1244,13 @@ export default class IBMi {
12351244
this.commandsExecuted += 1;
12361245
}
12371246

1238-
private disconnect() {
1239-
if (this.client.connection) {
1240-
this.client.connection?.removeAllListeners();
1241-
this.client.dispose();
1242-
this.client.connection = null;
1247+
private disconnect(failedToConnect = false) {
1248+
if (this.client) {
1249+
this.client = undefined;
12431250

1244-
instance.fire(`disconnected`);
1251+
if (failedToConnect === false && this.disconnectedCallback) {
1252+
this.disconnectedCallback(this);
1253+
}
12451254
}
12461255
}
12471256

src/api/Instance.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class Instance {
5454
});
5555
}
5656

57-
this.setConnection();
57+
this.disconnect();
5858

5959
if (reconnect) {
6060
await this.connect({...options, reconnecting: true});
@@ -90,23 +90,22 @@ export default class Instance {
9090
}
9191
}
9292

93+
if (result.success === false) {
94+
connection.dispose();
95+
}
96+
9397
return result;
9498
});
9599
}
96100

97101
async disconnect() {
98-
if (this.connection) {
99-
await this.connection.dispose();
100-
await this.setConnection();
102+
await this.setConnection();
101103

102-
await Promise.all([
103-
vscode.commands.executeCommand("code-for-ibmi.refreshObjectBrowser"),
104-
vscode.commands.executeCommand("code-for-ibmi.refreshLibraryListView"),
105-
vscode.commands.executeCommand("code-for-ibmi.refreshIFSBrowser")
106-
]);
107-
108-
vscode.window.showInformationMessage(`Disconnected.`);
109-
}
104+
await Promise.all([
105+
vscode.commands.executeCommand("code-for-ibmi.refreshObjectBrowser"),
106+
vscode.commands.executeCommand("code-for-ibmi.refreshLibraryListView"),
107+
vscode.commands.executeCommand("code-for-ibmi.refreshIFSBrowser")
108+
]);
110109
}
111110

112111
private async setConnection(connection?: IBMi) {
@@ -115,6 +114,11 @@ export default class Instance {
115114
}
116115

117116
if (connection) {
117+
connection.setDisconnectedCallback(async () => {
118+
this.setConnection();
119+
this.fire(`disconnected`);
120+
});
121+
118122
this.connection = connection;
119123
this.storage.setConnectionName(connection.currentConnectionName);
120124
await GlobalStorage.get().setLastConnection(connection.currentConnectionName);

0 commit comments

Comments
 (0)