Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta 2022-07-13 10:29:18 +02:00 committed by Akos Kitta
parent 813444408e
commit aea550fe33

View File

@ -81,7 +81,10 @@ export class BoardDiscovery
stop(): Promise<void> { stop(): Promise<void> {
this.logger.info('>>> Stopping boards watcher...'); this.logger.info('>>> Stopping boards watcher...');
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
const timeout = this.timeout(BoardDiscovery.StopWatchTimeout, reject); const timeout = this.createTimeout(
BoardDiscovery.StopWatchTimeout,
reject
);
const toDispose = new DisposableCollection(); const toDispose = new DisposableCollection();
toDispose.pushAll([ toDispose.pushAll([
timeout, timeout,
@ -107,7 +110,7 @@ export class BoardDiscovery
}); });
} }
private timeout( private createTimeout(
after: number, after: number,
onTimeout: (error: Error) => void onTimeout: (error: Error) => void
): Disposable { ): Disposable {
@ -118,37 +121,22 @@ export class BoardDiscovery
return Disposable.create(() => clearTimeout(timer)); return Disposable.create(() => clearTimeout(timer));
} }
private async write( private async requestStartWatch(
req: BoardListWatchRequest, req: BoardListWatchRequest,
duplex: Duplex duplex: Duplex
): Promise<void> { ): Promise<void> {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
this.logger.info(`>>> Writing ${this.toJson(req)} to the stream...`);
if ( if (
!duplex.write(req, (err: Error | undefined) => { !duplex.write(req, (err: Error | undefined) => {
if (err) { if (err) {
this.logger.error(
`<<< Error ocurred while writing to the stream.`,
err
);
reject(err); reject(err);
return; return;
} }
}) })
) { ) {
duplex.once('drain', () => { duplex.once('drain', resolve);
this.logger.info(
`<<< Board list watch request has been successfully written to the stream after the handling backpressure.`
);
resolve();
});
} else { } else {
process.nextTick(() => { process.nextTick(resolve);
this.logger.info(
`<<< Board list watch request has been successfully written to the stream.`
);
resolve();
});
} }
}); });
} }
@ -302,7 +290,7 @@ export class BoardDiscovery
this.notificationService.notifyAttachedBoardsDidChange(event); this.notificationService.notifyAttachedBoardsDidChange(event);
} }
}); });
await this.write( await this.requestStartWatch(
new BoardListWatchRequest().setInstance(instance), new BoardListWatchRequest().setInstance(instance),
wrapper.stream wrapper.stream
); );