1
1
import { injectable , inject } from '@theia/core/shared/inversify' ;
2
2
import { Emitter } from '@theia/core/lib/common/event' ;
3
3
import { ILogger } from '@theia/core/lib/common/logger' ;
4
- import { CommandService } from '@theia/core/lib/common/command' ;
4
+ import {
5
+ Command ,
6
+ CommandContribution ,
7
+ CommandRegistry ,
8
+ CommandService ,
9
+ } from '@theia/core/lib/common/command' ;
5
10
import { MessageService } from '@theia/core/lib/common/message-service' ;
6
11
import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application' ;
7
12
import { RecursiveRequired } from '../../common/types' ;
@@ -23,9 +28,18 @@ import { nls } from '@theia/core/lib/common';
23
28
import { Deferred } from '@theia/core/lib/common/promise-util' ;
24
29
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state' ;
25
30
import { Unknown } from '../../common/nls' ;
31
+ import {
32
+ StartupTask ,
33
+ StartupTaskProvider ,
34
+ } from '../../electron-common/startup-task' ;
26
35
27
36
@injectable ( )
28
- export class BoardsServiceProvider implements FrontendApplicationContribution {
37
+ export class BoardsServiceProvider
38
+ implements
39
+ FrontendApplicationContribution ,
40
+ StartupTaskProvider ,
41
+ CommandContribution
42
+ {
29
43
@inject ( ILogger )
30
44
protected logger : ILogger ;
31
45
@@ -50,6 +64,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
50
64
AvailableBoard [ ]
51
65
> ( ) ;
52
66
protected readonly onAvailablePortsChangedEmitter = new Emitter < Port [ ] > ( ) ;
67
+ private readonly inheritedConfig = new Deferred < BoardsConfig . Config > ( ) ;
53
68
54
69
/**
55
70
* Used for the auto-reconnecting. Sometimes, the attached board gets disconnected after uploading something to it.
@@ -115,6 +130,13 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
115
130
} ) ;
116
131
}
117
132
133
+ registerCommands ( registry : CommandRegistry ) : void {
134
+ registry . registerCommand ( USE_INHERITED_CONFIG , {
135
+ execute : ( inheritedConfig : BoardsConfig . Config ) =>
136
+ this . inheritedConfig . resolve ( inheritedConfig ) ,
137
+ } ) ;
138
+ }
139
+
118
140
get reconciled ( ) : Promise < void > {
119
141
return this . _reconciled . promise ;
120
142
}
@@ -655,11 +677,14 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
655
677
let storedLatestBoardsConfig = await this . getData <
656
678
BoardsConfig . Config | undefined
657
679
> ( 'latest-boards-config' ) ;
658
- // Try to get from the URL if it was not persisted .
680
+ // Try to get from the startup task. Wait for it, then timeout. Maybe it never arrives .
659
681
if ( ! storedLatestBoardsConfig ) {
660
- storedLatestBoardsConfig = BoardsConfig . Config . getConfig (
661
- new URL ( window . location . href )
662
- ) ;
682
+ storedLatestBoardsConfig = await Promise . race ( [
683
+ this . inheritedConfig . promise ,
684
+ new Promise < undefined > ( ( resolve ) =>
685
+ setTimeout ( ( ) => resolve ( undefined ) , 2_000 )
686
+ ) ,
687
+ ] ) ;
663
688
}
664
689
if ( storedLatestBoardsConfig ) {
665
690
this . latestBoardsConfig = storedLatestBoardsConfig ;
@@ -682,8 +707,31 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
682
707
key
683
708
) ;
684
709
}
710
+
711
+ tasks ( ) : StartupTask [ ] {
712
+ return [
713
+ {
714
+ command : USE_INHERITED_CONFIG . id ,
715
+ args : [ this . boardsConfig ] ,
716
+ } ,
717
+ ] ;
718
+ }
685
719
}
686
720
721
+ /**
722
+ * It should be neither visible nor called from outside.
723
+ *
724
+ * This service creates a startup task with the current board config and
725
+ * passes the task to the electron-main process so that the new window
726
+ * can inherit the boards config state of this service.
727
+ *
728
+ * Note that the state is always set, but new windows might ignore it.
729
+ * For example, the new window already has a valid boards config persisted to the local storage.
730
+ */
731
+ const USE_INHERITED_CONFIG : Command = {
732
+ id : 'arduino-use-inherited-boards-config' ,
733
+ } ;
734
+
687
735
/**
688
736
* Representation of a ready-to-use board, either the user has configured it or was automatically recognized by the CLI.
689
737
* An available board was not necessarily recognized by the CLI (e.g.: it is a 3rd party board) or correctly configured but ready for `verify`.
0 commit comments