@@ -71,7 +71,7 @@ export class DartDebugClient extends DebugClient {
7171 workspaceFolder : undefined ,
7272 } ;
7373 this . debugCommands . handleDebugSessionStart ( this . currentSession ) ;
74- this . on ( "terminated" , ( e : DebugProtocol . TerminatedEvent ) => this . debugCommands . handleDebugSessionEnd ( this . currentSession ) ) ;
74+ this . waitForEvent ( "terminated" ) . then ( ( _ ) => this . debugCommands . handleDebugSessionEnd ( this . currentSession ) ) ;
7575
7676 // We override the base method to swap for attachRequest when required, so that
7777 // all the existing methods that provide useful functionality but assume launching
@@ -166,16 +166,23 @@ export class DartDebugClient extends DebugClient {
166166
167167 public assertOutputContains ( category : string , text : string ) : Promise < DebugProtocol . OutputEvent > {
168168 let output = "" ;
169+ const emitter = this ;
170+ let cleanup = ( ) => { } ; // tslint:disable-line: no-empty
169171 return withTimeout (
170- new Promise ( ( resolve , reject ) => this . on ( "output" , ( event : DebugProtocol . OutputEvent ) => {
171- if ( event . body . category === category ) {
172- output += event . body . output ;
173- if ( output . indexOf ( text ) !== - 1 )
174- resolve ( event ) ;
172+ new Promise < DebugProtocol . OutputEvent > ( ( resolve ) => {
173+ function handleOutput ( event : DebugProtocol . OutputEvent ) {
174+ if ( event . body . category === category ) {
175+ output += event . body . output ;
176+ if ( output . indexOf ( text ) !== - 1 ) {
177+ resolve ( event ) ;
178+ }
179+ }
175180 }
176- } ) ) ,
181+ cleanup = ( ) => this . removeListener ( "output" , handleOutput ) ;
182+ this . on ( "output" , handleOutput ) ;
183+ } ) ,
177184 ( ) => `Didn't find text "${ text } " in ${ category } \nGot: ${ output } ` ,
178- ) ;
185+ ) . finally ( ( ) => cleanup ( ) ) ;
179186 }
180187
181188 public waitForCustomEvent < T > ( type : string , filter : ( notification : T ) => boolean ) : Promise < T > {
@@ -190,15 +197,16 @@ export class DartDebugClient extends DebugClient {
190197 try {
191198 const notification = event . body as T ;
192199 if ( filter ( notification ) ) {
193- resolve ( notification ) ;
194200 this . removeListener ( type , handler ) ;
201+ resolve ( notification ) ;
195202 }
196203 } catch ( e ) {
197- reject ( e ) ;
198204 this . removeListener ( type , handler ) ;
205+ reject ( e ) ;
199206 }
200207 } ;
201208 this . on ( type , handler ) ;
209+ this . on ( "terminated" , ( ) => this . removeListener ( type , handler ) ) ;
202210 } ) ;
203211 }
204212
0 commit comments