1
- const debounce = require ( 'lodash.debounce' ) ;
1
+ import { Mutex } from 'async-mutex' ;
2
2
import { MAIN_MENU_BAR , MenuContribution , MenuModelRegistry , SelectionService , ILogger } from '@theia/core' ;
3
3
import {
4
4
ContextMenuRenderer ,
@@ -201,7 +201,6 @@ export class ArduinoFrontendContribution implements FrontendApplicationContribut
201
201
if ( selectedBoard ) {
202
202
const { name, fqbn } = selectedBoard ;
203
203
if ( fqbn ) {
204
- await this . hostedPluginSupport . didStart ;
205
204
this . startLanguageServer ( fqbn , name ) ;
206
205
}
207
206
}
@@ -225,33 +224,44 @@ export class ArduinoFrontendContribution implements FrontendApplicationContribut
225
224
} ) ;
226
225
}
227
226
228
- protected startLanguageServer = debounce ( ( fqbn : string , name : string | undefined ) => this . doStartLanguageServer ( fqbn , name ) ) ;
229
- protected async doStartLanguageServer ( fqbn : string , name : string | undefined ) : Promise < void > {
230
- this . logger . info ( `Starting language server: ${ fqbn } ` ) ;
231
- const log = this . arduinoPreferences . get ( 'arduino.language.log' ) ;
232
- let currentSketchPath : string | undefined = undefined ;
233
- if ( log ) {
234
- const currentSketch = await this . sketchServiceClient . currentSketch ( ) ;
235
- if ( currentSketch ) {
236
- currentSketchPath = await this . fileSystem . fsPath ( new URI ( currentSketch . uri ) ) ;
227
+ protected languageServerFqbn ?: string ;
228
+ protected languageServerStartMutex = new Mutex ( ) ;
229
+ protected async startLanguageServer ( fqbn : string , name : string | undefined ) : Promise < void > {
230
+ const release = await this . languageServerStartMutex . acquire ( ) ;
231
+ try {
232
+ if ( fqbn === this . languageServerFqbn ) {
233
+ // NOOP
234
+ return ;
237
235
}
238
- }
239
- const { clangdUri, cliUri, lsUri } = await this . executableService . list ( ) ;
240
- const [ clangdPath , cliPath , lsPath ] = await Promise . all ( [
241
- this . fileSystem . fsPath ( new URI ( clangdUri ) ) ,
242
- this . fileSystem . fsPath ( new URI ( cliUri ) ) ,
243
- this . fileSystem . fsPath ( new URI ( lsUri ) ) ,
244
- ] ) ;
245
- this . commandRegistry . executeCommand ( 'arduino.languageserver.start' , {
246
- lsPath,
247
- cliPath,
248
- clangdPath,
249
- log : currentSketchPath ? currentSketchPath : log ,
250
- board : {
251
- fqbn,
252
- name : name ? `"${ name } "` : undefined
236
+ await this . hostedPluginSupport . didStart ;
237
+ this . logger . info ( `Starting language server: ${ fqbn } ` ) ;
238
+ const log = this . arduinoPreferences . get ( 'arduino.language.log' ) ;
239
+ let currentSketchPath : string | undefined = undefined ;
240
+ if ( log ) {
241
+ const currentSketch = await this . sketchServiceClient . currentSketch ( ) ;
242
+ if ( currentSketch ) {
243
+ currentSketchPath = await this . fileSystem . fsPath ( new URI ( currentSketch . uri ) ) ;
244
+ }
253
245
}
254
- } ) ;
246
+ const { clangdUri, cliUri, lsUri } = await this . executableService . list ( ) ;
247
+ const [ clangdPath , cliPath , lsPath ] = await Promise . all ( [
248
+ this . fileSystem . fsPath ( new URI ( clangdUri ) ) ,
249
+ this . fileSystem . fsPath ( new URI ( cliUri ) ) ,
250
+ this . fileSystem . fsPath ( new URI ( lsUri ) ) ,
251
+ ] ) ;
252
+ this . languageServerFqbn = await this . commandRegistry . executeCommand ( 'arduino.languageserver.start' , {
253
+ lsPath,
254
+ cliPath,
255
+ clangdPath,
256
+ log : currentSketchPath ? currentSketchPath : log ,
257
+ board : {
258
+ fqbn,
259
+ name : name ? `"${ name } "` : undefined
260
+ }
261
+ } ) ;
262
+ } finally {
263
+ release ( ) ;
264
+ }
255
265
}
256
266
257
267
registerToolbarItems ( registry : TabBarToolbarRegistry ) : void {
0 commit comments