1
1
import { ILogger } from '@theia/core' ;
2
2
import { inject , injectable , named } from '@theia/core/shared/inversify' ;
3
- import { Board , Port , Status } from '../common/protocol' ;
3
+ import { Board , BoardsService , Port , Status } from '../common/protocol' ;
4
4
import { CoreClientAware } from './core-client-provider' ;
5
5
import { MonitorService } from './monitor-service' ;
6
6
import { MonitorServiceFactory } from './monitor-service-factory' ;
@@ -15,14 +15,20 @@ export const MonitorManagerName = 'monitor-manager';
15
15
16
16
@injectable ( )
17
17
export class MonitorManager extends CoreClientAware {
18
+ @inject ( BoardsService )
19
+ protected boardsService : BoardsService ;
20
+
18
21
// Map of monitor services that manage the running pluggable monitors.
19
22
// Each service handles the lifetime of one, and only one, monitor.
20
23
// If either the board or port managed changes, a new service must
21
24
// be started.
22
25
private monitorServices = new Map < MonitorID , MonitorService > ( ) ;
23
26
private isUploadInProgress : boolean ;
24
27
25
- private startMonitorPendingRequests : Array < MonitorID > = [ ] ;
28
+ private startMonitorPendingRequests : [
29
+ [ Board , Port ] ,
30
+ ( status : Status ) => void
31
+ ] [ ] = [ ] ;
26
32
27
33
@inject ( MonitorServiceFactory )
28
34
private monitorServiceFactory : MonitorServiceFactory ;
@@ -59,17 +65,25 @@ export class MonitorManager extends CoreClientAware {
59
65
* @returns a Status object to know if the process has been
60
66
* started or if there have been errors.
61
67
*/
62
- async startMonitor ( board : Board , port : Port ) : Promise < Status > {
68
+ async startMonitor (
69
+ board : Board ,
70
+ port : Port ,
71
+ postStartCallback : ( status : Status ) => void
72
+ ) : Promise < void > {
63
73
const monitorID = this . monitorID ( board , port ) ;
74
+
64
75
let monitor = this . monitorServices . get ( monitorID ) ;
65
76
if ( ! monitor ) {
66
77
monitor = this . createMonitor ( board , port ) ;
67
78
}
79
+
68
80
if ( this . isUploadInProgress ) {
69
- this . startMonitorPendingRequests . push ( monitorID ) ;
70
- return Status . UPLOAD_IN_PROGRESS ;
81
+ this . startMonitorPendingRequests . push ( [ [ board , port ] , postStartCallback ] ) ;
82
+ return ;
71
83
}
72
- return monitor . start ( ) ;
84
+
85
+ const result = await monitor . start ( ) ;
86
+ postStartCallback ( result ) ;
73
87
}
74
88
75
89
/**
@@ -113,6 +127,8 @@ export class MonitorManager extends CoreClientAware {
113
127
* @param port port to monitor
114
128
*/
115
129
async notifyUploadStarted ( board ?: Board , port ?: Port ) : Promise < void > {
130
+ this . isUploadInProgress = true ;
131
+
116
132
if ( ! board || ! port ) {
117
133
// We have no way of knowing which monitor
118
134
// to retrieve if we don't have this information.
@@ -124,8 +140,7 @@ export class MonitorManager extends CoreClientAware {
124
140
// There's no monitor running there, bail
125
141
return ;
126
142
}
127
- this . isUploadInProgress = true ;
128
- return await monitor . pause ( ) ;
143
+ return monitor . pause ( ) ;
129
144
}
130
145
131
146
/**
@@ -137,14 +152,8 @@ export class MonitorManager extends CoreClientAware {
137
152
* started or if there have been errors.
138
153
*/
139
154
async notifyUploadFinished ( board ?: Board , port ?: Port ) : Promise < Status > {
140
- try {
141
- for ( const id of this . startMonitorPendingRequests ) {
142
- const m = this . monitorServices . get ( id ) ;
143
- if ( m ) m . start ( ) ;
144
- }
145
- } finally {
146
- this . startMonitorPendingRequests = [ ] ;
147
- }
155
+ this . isUploadInProgress = false ;
156
+
148
157
if ( ! board || ! port ) {
149
158
// We have no way of knowing which monitor
150
159
// to retrieve if we don't have this information.
@@ -156,11 +165,35 @@ export class MonitorManager extends CoreClientAware {
156
165
// There's no monitor running there, bail
157
166
return Status . NOT_CONNECTED ;
158
167
}
159
- this . isUploadInProgress = false ;
160
168
161
169
return monitor . start ( ) ;
162
170
}
163
171
172
+ async startQueuedServices ( ) : Promise < void > {
173
+ const queued = this . startMonitorPendingRequests ;
174
+ this . startMonitorPendingRequests = [ ] ;
175
+
176
+ for ( const [ [ board , port ] , onFinish ] of queued ) {
177
+ const boardsState = await this . boardsService . getState ( ) ;
178
+ const boardIsStillOnPort = Object . keys ( boardsState )
179
+ . map ( ( connection : string ) => {
180
+ const portAddress = connection . split ( '|' ) [ 0 ] ;
181
+ return portAddress ;
182
+ } )
183
+ . some ( ( portAddress : string ) => port . address === portAddress ) ;
184
+
185
+ if ( boardIsStillOnPort ) {
186
+ const monitorID = this . monitorID ( board , port ) ;
187
+ const monitorService = this . monitorServices . get ( monitorID ) ;
188
+
189
+ if ( monitorService ) {
190
+ const result = await monitorService . start ( ) ;
191
+ onFinish ( result ) ;
192
+ }
193
+ }
194
+ }
195
+ }
196
+
164
197
/**
165
198
* Changes the settings of a pluggable monitor even if it's running.
166
199
* If monitor is not running they're going to be used as soon as it's started.
0 commit comments