@@ -44,6 +44,7 @@ export class TestClient<Configuration> {
4444 private openDocuments : Set < string > ;
4545 private workspaceFolder : Promise < string > ;
4646 private watchEnabled : boolean ;
47+ private buildCompleted ;
4748
4849 onRequest : Connection [ "onRequest" ] ;
4950 sendRequest : Connection [ "sendRequest" ] ;
@@ -97,8 +98,21 @@ export class TestClient<Configuration> {
9798 }
9899 } ) ;
99100
100- this . client . onRequest ( WorkDoneProgressCreateRequest . type , ( ) => {
101- // Nothing to do
101+ this . buildCompleted = createPromise ( ) ;
102+ let builds = 0 ;
103+
104+ this . client . onRequest ( WorkDoneProgressCreateRequest . type , ( { token } ) => {
105+ this . client . onProgress ( WorkDoneProgress . type , token , ( { kind } ) => {
106+ if ( kind === "begin" ) {
107+ builds ++ ;
108+ } else if ( kind === "end" ) {
109+ builds -- ;
110+ if ( builds === 0 ) {
111+ this . buildCompleted . resolve ( ) ;
112+ this . buildCompleted = createPromise ( ) ;
113+ }
114+ }
115+ } ) ;
102116 } ) ;
103117
104118 this . client . onRequest ( ConfigurationRequest . type , ( params ) => {
@@ -221,8 +235,6 @@ export class TestClient<Configuration> {
221235 async changeConfiguration ( settings : Partial < Configuration > ) {
222236 this . _settings = settings ;
223237
224- const buildCompleted = this . buildCompleted ( ) ;
225-
226238 if ( this . configurationChangeNotificationOptions === null ) {
227239 await this . client . sendNotification ( DidChangeConfigurationNotification . type , {
228240 settings : null
@@ -235,7 +247,7 @@ export class TestClient<Configuration> {
235247 } ) ;
236248 }
237249
238- await buildCompleted ;
250+ await this . buildCompleted . promise ;
239251 }
240252
241253 async writeDocument ( uri : string , text : string ) {
@@ -244,8 +256,6 @@ export class TestClient<Configuration> {
244256
245257 await writeFile ( fileURLToPath ( fullUri ) , text , "utf-8" ) ;
246258
247- const buildCompleted = this . buildCompleted ( ) ;
248-
249259 if ( this . watchEnabled ) {
250260 await this . client . sendNotification ( DidChangeWatchedFilesNotification . type , {
251261 changes : [ {
@@ -255,7 +265,7 @@ export class TestClient<Configuration> {
255265 } ) ;
256266 }
257267
258- await buildCompleted ;
268+ await this . buildCompleted . promise ;
259269
260270 return fullUri ;
261271 }
@@ -265,8 +275,6 @@ export class TestClient<Configuration> {
265275
266276 await rm ( fileURLToPath ( fullUri ) ) ;
267277
268- const buildCompleted = this . buildCompleted ( ) ;
269-
270278 if ( this . watchEnabled ) {
271279 await this . client . sendNotification ( DidChangeWatchedFilesNotification . type , {
272280 changes : [ {
@@ -276,7 +284,7 @@ export class TestClient<Configuration> {
276284 } ) ;
277285 }
278286
279- await buildCompleted ;
287+ await this . buildCompleted . promise ;
280288
281289 return fullUri ;
282290 }
@@ -308,17 +316,8 @@ export class TestClient<Configuration> {
308316 } ) ;
309317 }
310318
311- // TODO: Duplicated code
312- private buildCompleted ( ) {
313- return new Promise < void > ( ( resolve ) => {
314- this . client . onRequest ( WorkDoneProgressCreateRequest . type , ( { token } ) => {
315- this . client . onProgress ( WorkDoneProgress . type , token , ( { kind } ) => {
316- if ( kind === "end" ) {
317- resolve ( ) ;
318- }
319- } ) ;
320- } ) ;
321- } ) ;
319+ async ready ( ) {
320+ await this . buildCompleted . promise ;
322321 }
323322}
324323
@@ -331,3 +330,15 @@ export class TestStream extends Duplex {
331330 _read ( ) {
332331 }
333332}
333+
334+ const createPromise = ( ) => {
335+ let resolve : ( ) => void ;
336+ let reject : ( reason ?: any ) => void ; // eslint-disable-line @typescript-eslint/no-explicit-any
337+ const promise = new Promise < void > ( ( resolveFn , rejectFn ) => {
338+ resolve = resolveFn ;
339+ reject = rejectFn ;
340+ } ) ;
341+
342+ // @ts -expect-error TypeScript doesn't think resolve and reject have been assigned
343+ return { promise, resolve, reject } ;
344+ } ;
0 commit comments