@@ -84,6 +84,7 @@ export abstract class InteractiveBase extends WebViewHost<IInteractiveWindowMapp
84
84
private _id : string ;
85
85
private executeEvent : EventEmitter < string > = new EventEmitter < string > ( ) ;
86
86
private serverAndNotebookPromise : Promise < void > | undefined ;
87
+ private notebookPromise : Promise < void > | undefined ;
87
88
private setDarkPromise : Deferred < boolean > | undefined ;
88
89
public get notebook ( ) : INotebook | undefined {
89
90
return this . _notebook ;
@@ -936,18 +937,39 @@ export abstract class InteractiveBase extends WebViewHost<IInteractiveWindowMapp
936
937
} ;
937
938
938
939
private async stopServer ( ) : Promise < void > {
940
+ // Finish either of our notebook promises
939
941
if ( this . serverAndNotebookPromise ) {
940
942
await this . serverAndNotebookPromise ;
941
943
this . serverAndNotebookPromise = undefined ;
942
- if ( this . _notebook ) {
943
- const server = this . _notebook ;
944
- this . _notebook = undefined ;
945
- await server . dispose ( ) ;
946
- }
944
+ }
945
+ if ( this . notebookPromise ) {
946
+ await this . notebookPromise ;
947
+ this . notebookPromise = undefined ;
948
+ }
949
+ // If we have a notebook dispose of it
950
+ if ( this . _notebook ) {
951
+ const server = this . _notebook ;
952
+ this . _notebook = undefined ;
953
+ await server . dispose ( ) ;
947
954
}
948
955
}
949
956
957
+ // ensureNotebook can be called apart from ensureNotebookAndServer and it needs
958
+ // the same protection to not be called twice
950
959
private async ensureNotebook ( server : INotebookServer ) : Promise < void > {
960
+ if ( ! this . notebookPromise ) {
961
+ this . notebookPromise = this . ensureNotebookImpl ( server ) ;
962
+ }
963
+ try {
964
+ await this . notebookPromise ;
965
+ } catch ( e ) {
966
+ // Reset the load promise. Don't want to keep hitting the same error
967
+ this . notebookPromise = undefined ;
968
+ throw e ;
969
+ }
970
+ }
971
+
972
+ private async ensureNotebookImpl ( server : INotebookServer ) : Promise < void > {
951
973
// Create a new notebook if we need to.
952
974
if ( ! this . _notebook ) {
953
975
this . _notebook = await server . createNotebook ( await this . getNotebookIdentity ( ) ) ;
@@ -1019,6 +1041,7 @@ export abstract class InteractiveBase extends WebViewHost<IInteractiveWindowMapp
1019
1041
this . _notebook = undefined ;
1020
1042
} finally {
1021
1043
this . serverAndNotebookPromise = undefined ;
1044
+ this . notebookPromise = undefined ;
1022
1045
}
1023
1046
await this . ensureServerAndNotebook ( ) ;
1024
1047
}
0 commit comments