@@ -26,7 +26,7 @@ import {
26
26
import { LanguageClientConsumer } from "./languageClientConsumer" ;
27
27
import { SemVer , satisfies } from "semver" ;
28
28
29
- export enum SessionStatus {
29
+ enum SessionStatus {
30
30
NotStarted = "Not Started" ,
31
31
Starting = "Starting" ,
32
32
Running = "Running" ,
@@ -146,11 +146,33 @@ export class SessionManager implements Middleware {
146
146
}
147
147
148
148
// The `exeNameOverride` is used by `restartSession` to override ANY other setting.
149
+ // We've made this function idempotent, so it can used to ensure the session has started.
149
150
public async start ( exeNameOverride ?: string ) : Promise < void > {
150
- // A simple lock because this function isn't re-entrant.
151
- if ( this . sessionStatus === SessionStatus . Starting ) {
151
+ switch ( this . sessionStatus ) {
152
+ case SessionStatus . NotStarted :
153
+ // Go ahead and start.
154
+ break ;
155
+ case SessionStatus . Starting :
156
+ // A simple lock because this function isn't re-entrant.
152
157
this . logger . writeWarning ( "Re-entered 'start' so waiting..." ) ;
153
- return await this . waitUntilStarted ( ) ;
158
+ return await this . waitWhileStarting ( ) ;
159
+ case SessionStatus . Running :
160
+ // We're started, just return.
161
+ this . logger . writeVerbose ( "Already started." ) ;
162
+ return ;
163
+ case SessionStatus . Busy :
164
+ // We're started but busy so notify and return.
165
+ void this . logger . writeAndShowInformation ( "The Extension Terminal is currently busy, please wait for your task to finish!" ) ;
166
+ return ;
167
+ case SessionStatus . Stopping :
168
+ // Wait until done stopping, then start.
169
+ this . logger . writeVerbose ( "Still stopping." ) ;
170
+ await this . waitWhileStopping ( ) ;
171
+ break ;
172
+ case SessionStatus . Failed :
173
+ // Try to start again.
174
+ this . logger . writeVerbose ( "Previously failed, starting again." ) ;
175
+ break ;
154
176
}
155
177
156
178
this . setSessionStatus ( "Starting..." , SessionStatus . Starting ) ;
@@ -220,7 +242,7 @@ export class SessionManager implements Middleware {
220
242
}
221
243
}
222
244
223
- public async stop ( ) : Promise < void > {
245
+ private async stop ( ) : Promise < void > {
224
246
this . setSessionStatus ( "Stopping..." , SessionStatus . Stopping ) ;
225
247
// Cancel start-up if we're still waiting.
226
248
this . startCancellationTokenSource ?. cancel ( ) ;
@@ -255,7 +277,7 @@ export class SessionManager implements Middleware {
255
277
this . setSessionStatus ( "Not Started" , SessionStatus . NotStarted ) ;
256
278
}
257
279
258
- public async restartSession ( exeNameOverride ?: string ) : Promise < void > {
280
+ private async restartSession ( exeNameOverride ?: string ) : Promise < void > {
259
281
this . logger . write ( "Restarting session..." ) ;
260
282
await this . stop ( ) ;
261
283
@@ -267,22 +289,18 @@ export class SessionManager implements Middleware {
267
289
}
268
290
269
291
public getSessionDetails ( ) : IEditorServicesSessionDetails | undefined {
270
- // TODO: This is used solely by the debugger and should actually just wait (with a timeout) .
292
+ // This is used by the debugger which should have already called `start` .
271
293
if ( this . sessionDetails === undefined ) {
272
294
void this . logger . writeAndShowError ( "PowerShell session unavailable for debugging!" ) ;
273
295
}
274
296
return this . sessionDetails ;
275
297
}
276
298
277
- public getSessionStatus ( ) : SessionStatus {
278
- return this . sessionStatus ;
279
- }
280
-
281
299
public getPowerShellVersionDetails ( ) : IPowerShellVersionDetails | undefined {
282
300
return this . versionDetails ;
283
301
}
284
302
285
- public getNewSessionFilePath ( ) : vscode . Uri {
303
+ private getNewSessionFilePath ( ) : vscode . Uri {
286
304
const uniqueId : number = Math . floor ( 100000 + Math . random ( ) * 900000 ) ;
287
305
return vscode . Uri . joinPath ( this . sessionsFolder , `PSES-VSCode-${ process . env . VSCODE_PID } -${ uniqueId } .json` ) ;
288
306
}
@@ -334,14 +352,12 @@ export class SessionManager implements Middleware {
334
352
}
335
353
336
354
public async waitUntilStarted ( ) : Promise < void > {
337
- while ( this . sessionStatus === SessionStatus . Starting ) {
338
- if ( this . startCancellationTokenSource ?. token . isCancellationRequested ) {
339
- return ;
340
- }
355
+ while ( this . sessionStatus !== SessionStatus . Running ) {
341
356
await utils . sleep ( 300 ) ;
342
357
}
343
358
}
344
359
360
+ // TODO: Is this used by the magic of "Middleware" in the client library?
345
361
public resolveCodeLens (
346
362
codeLens : vscode . CodeLens ,
347
363
token : vscode . CancellationToken ,
@@ -803,6 +819,21 @@ Type 'help' to get help.
803
819
return languageStatusItem ;
804
820
}
805
821
822
+ private async waitWhileStarting ( ) : Promise < void > {
823
+ while ( this . sessionStatus === SessionStatus . Starting ) {
824
+ if ( this . startCancellationTokenSource ?. token . isCancellationRequested ) {
825
+ return ;
826
+ }
827
+ await utils . sleep ( 300 ) ;
828
+ }
829
+ }
830
+
831
+ private async waitWhileStopping ( ) : Promise < void > {
832
+ while ( this . sessionStatus === SessionStatus . Stopping ) {
833
+ await utils . sleep ( 300 ) ;
834
+ }
835
+ }
836
+
806
837
private setSessionStatus ( detail : string , status : SessionStatus ) : void {
807
838
this . logger . writeVerbose ( `Session status changing from '${ this . sessionStatus } ' to '${ status } '.` ) ;
808
839
this . sessionStatus = status ;
@@ -842,7 +873,6 @@ Type 'help' to get help.
842
873
this . languageStatusItem . severity = vscode . LanguageStatusSeverity . Error ;
843
874
break ;
844
875
}
845
-
846
876
}
847
877
848
878
private setSessionRunningStatus ( ) : void {
@@ -910,7 +940,7 @@ Type 'help' to get help.
910
940
}
911
941
912
942
// Always shows the session terminal.
913
- public showSessionTerminal ( isExecute ?: boolean ) : void {
943
+ private showSessionTerminal ( isExecute ?: boolean ) : void {
914
944
this . languageServerProcess ?. showTerminal ( isExecute && ! this . sessionSettings . integratedConsole . focusConsoleOnExecute ) ;
915
945
}
916
946
0 commit comments