diff --git a/arduino-ide-extension/src/node/board-discovery.ts b/arduino-ide-extension/src/node/board-discovery.ts index 8a639e07e..e612979e1 100644 --- a/arduino-ide-extension/src/node/board-discovery.ts +++ b/arduino-ide-extension/src/node/board-discovery.ts @@ -60,11 +60,29 @@ export class BoardDiscovery extends CoreClientAware { this.startBoardListWatch(coreClient); } + stopBoardListWatch(coreClient: CoreClientProvider.Client): Promise { + return new Promise((resolve, reject) => { + if (!this.boardWatchDuplex) { + return resolve(); + } + + const { instance } = coreClient; + const req = new BoardListWatchRequest(); + req.setInstance(instance); + try { + this.boardWatchDuplex.write(req.setInterrupt(true), resolve); + } catch (e) { + this.discoveryLogger.error(e); + resolve(); + } + }); + } + startBoardListWatch(coreClient: CoreClientProvider.Client): void { if (this.watching) { // We want to avoid starting the board list watch process multiple // times to meet unforseen consequences - return + return; } this.watching = true; const { client, instance } = coreClient; @@ -73,9 +91,19 @@ export class BoardDiscovery extends CoreClientAware { this.boardWatchDuplex = client.boardListWatch(); this.boardWatchDuplex.on('end', () => { this.watching = false; - console.info('board watch ended') - }) + console.info('board watch ended'); + }); + this.boardWatchDuplex.on('close', () => { + this.watching = false; + console.info('board watch ended'); + }); this.boardWatchDuplex.on('data', (resp: BoardListWatchResponse) => { + if (resp.getEventType() === 'quit') { + this.watching = false; + console.info('board watch ended'); + return; + } + const detectedPort = resp.getPort(); if (detectedPort) { let eventType: 'add' | 'remove' | 'unknown' = 'unknown'; @@ -96,7 +124,7 @@ export class BoardDiscovery extends CoreClientAware { const address = (detectedPort as any).getPort().getAddress(); const protocol = (detectedPort as any).getPort().getProtocol(); - const label = (detectedPort as any).getPort().getLabel();; + const label = (detectedPort as any).getPort().getLabel(); const port = { address, protocol, label }; const boards: Board[] = []; for (const item of detectedPort.getMatchingBoardsList()) { @@ -111,7 +139,9 @@ export class BoardDiscovery extends CoreClientAware { if (newState[port.address]) { const [, knownBoards] = newState[port.address]; console.warn( - `Port '${port.address}' was already available. Known boards before override: ${JSON.stringify( + `Port '${ + port.address + }' was already available. Known boards before override: ${JSON.stringify( knownBoards )}` ); diff --git a/arduino-ide-extension/src/node/boards-service-impl.ts b/arduino-ide-extension/src/node/boards-service-impl.ts index acb4a7315..d7c5da331 100644 --- a/arduino-ide-extension/src/node/boards-service-impl.ts +++ b/arduino-ide-extension/src/node/boards-service-impl.ts @@ -45,7 +45,8 @@ import { InstallWithProgress } from './grpc-installable'; @injectable() export class BoardsServiceImpl extends CoreClientAware - implements BoardsService { + implements BoardsService +{ @inject(ILogger) protected logger: ILogger; @@ -247,7 +248,10 @@ export class BoardsServiceImpl return boards; } - async getBoardUserFields(options: { fqbn: string, protocol: string }): Promise { + async getBoardUserFields(options: { + fqbn: string; + protocol: string; + }): Promise { await this.coreClientProvider.initialized; const coreClient = await this.coreClient(); const { client, instance } = coreClient; @@ -257,25 +261,23 @@ export class BoardsServiceImpl supportedUserFieldsReq.setFqbn(options.fqbn); supportedUserFieldsReq.setProtocol(options.protocol); - const supportedUserFieldsResp = await new Promise( - (resolve, reject) => { + const supportedUserFieldsResp = + await new Promise((resolve, reject) => { client.supportedUserFields(supportedUserFieldsReq, (err, resp) => { - (!!err ? reject : resolve)(!!err ? err : resp) - }) - } - ); - return supportedUserFieldsResp.getUserFieldsList().map(e => { + (!!err ? reject : resolve)(!!err ? err : resp); + }); + }); + return supportedUserFieldsResp.getUserFieldsList().map((e) => { return { toolId: e.getToolId(), name: e.getName(), label: e.getLabel(), secret: e.getSecret(), - value: "", + value: '', }; }); } - async search(options: { query?: string }): Promise { await this.coreClientProvider.initialized; const coreClient = await this.coreClient(); @@ -408,6 +410,10 @@ export class BoardsServiceImpl req.setVersion(version); console.info('>>> Starting boards package installation...', item); + + // stop the board discovery + await this.boardDiscovery.stopBoardListWatch(coreClient); + const resp = client.platformInstall(req); resp.on( 'data', @@ -418,7 +424,7 @@ export class BoardsServiceImpl ); await new Promise((resolve, reject) => { resp.on('end', () => { - this.boardDiscovery.startBoardListWatch(coreClient) + this.boardDiscovery.startBoardListWatch(coreClient); resolve(); }); resp.on('error', (error) => { @@ -456,6 +462,10 @@ export class BoardsServiceImpl req.setPlatformPackage(platform); console.info('>>> Starting boards package uninstallation...', item); + + // stop the board discovery + await this.boardDiscovery.stopBoardListWatch(coreClient); + const resp = client.platformUninstall(req); resp.on( 'data', @@ -466,7 +476,7 @@ export class BoardsServiceImpl ); await new Promise((resolve, reject) => { resp.on('end', () => { - this.boardDiscovery.startBoardListWatch(coreClient) + this.boardDiscovery.startBoardListWatch(coreClient); resolve(); }); resp.on('error', reject); diff --git a/arduino-ide-extension/src/node/library-service-server-impl.ts b/arduino-ide-extension/src/node/library-service-server-impl.ts index 1ff3acb73..e1f1b48dc 100644 --- a/arduino-ide-extension/src/node/library-service-server-impl.ts +++ b/arduino-ide-extension/src/node/library-service-server-impl.ts @@ -271,6 +271,10 @@ export class LibraryServiceImpl req.setNoDeps(!options.installDependencies); console.info('>>> Starting library package installation...', item); + + // stop the board discovery + await this.boardDiscovery.stopBoardListWatch(coreClient); + const resp = client.libraryInstall(req); resp.on( 'data', @@ -322,6 +326,10 @@ export class LibraryServiceImpl if (typeof overwrite === 'boolean') { req.setOverwrite(overwrite); } + + // stop the board discovery + await this.boardDiscovery.stopBoardListWatch(coreClient); + const resp = client.zipLibraryInstall(req); resp.on( 'data', @@ -354,6 +362,10 @@ export class LibraryServiceImpl req.setVersion(item.installedVersion!); console.info('>>> Starting library package uninstallation...', item); + + // stop the board discovery + await this.boardDiscovery.stopBoardListWatch(coreClient); + const resp = client.libraryUninstall(req); resp.on( 'data',