@@ -8,7 +8,6 @@ import { IBMiComponent } from "../components/component";
88import { CopyToImport } from "../components/copyToImport" ;
99import { CustomQSh } from '../components/cqsh' ;
1010import { ComponentManager } from "../components/manager" ;
11- import { instance } from "../instantiate" ;
1211import { CommandData , CommandResult , ConnectionData , IBMiMember , RemoteCommand , SpecialAuthorities , WrapResult } from "../typings" ;
1312import { CompileTools } from "./CompileTools" ;
1413import { 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+
5556export 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
0 commit comments