@@ -11,6 +11,9 @@ import {
11
11
12
12
type MonitorID = string ;
13
13
14
+ type UploadState = 'uploadInProgress' | 'pausedForUpload' | 'disposedForUpload' ;
15
+ type MonitorIDsByUploadState = Record < UploadState , MonitorID [ ] > ;
16
+
14
17
export const MonitorManagerName = 'monitor-manager' ;
15
18
16
19
@injectable ( )
@@ -23,15 +26,17 @@ export class MonitorManager extends CoreClientAware {
23
26
// If either the board or port managed changes, a new service must
24
27
// be started.
25
28
private monitorServices = new Map < MonitorID , MonitorService > ( ) ;
26
- private isUploadInProgress : boolean ;
27
29
28
- private servicesPausedForUpload : MonitorID [ ] = [ ] ;
29
- private servicesDisposedForUpload : MonitorID [ ] = [ ] ;
30
+ private monitorIDsByUploadState : MonitorIDsByUploadState = {
31
+ uploadInProgress : [ ] ,
32
+ pausedForUpload : [ ] ,
33
+ disposedForUpload : [ ] ,
34
+ } ;
30
35
31
- private startMonitorPendingRequests : [
32
- [ Board , Port ] ,
33
- ( status : Status ) => void
34
- ] [ ] = [ ] ;
36
+ private startMonitor_PendingRequests : {
37
+ requestFor : [ Board , Port ] ;
38
+ postStartCallback : ( status : Status ) => void ;
39
+ } [ ] = [ ] ;
35
40
36
41
@inject ( MonitorServiceFactory )
37
42
private monitorServiceFactory : MonitorServiceFactory ;
@@ -60,6 +65,25 @@ export class MonitorManager extends CoreClientAware {
60
65
return false ;
61
66
}
62
67
68
+ uploadIsInProgress ( ) : boolean {
69
+ return this . monitorIDsByUploadState . uploadInProgress . length > 0 ;
70
+ }
71
+
72
+ addToMonitorIDsByUploadState ( state : UploadState , monitorID : string ) : void {
73
+ this . monitorIDsByUploadState [ state ] . push ( monitorID ) ;
74
+ }
75
+
76
+ removeFromMonitorIDsByUploadState (
77
+ state : UploadState ,
78
+ monitorID : string
79
+ ) : void {
80
+ this . monitorIDsByUploadState [ state ] . filter ( ( id ) => id !== monitorID ) ;
81
+ }
82
+
83
+ monitorIDIsInUploadState ( state : UploadState , monitorID : string ) : boolean {
84
+ return this . monitorIDsByUploadState [ state ] . includes ( monitorID ) ;
85
+ }
86
+
63
87
/**
64
88
* Start a pluggable monitor that receives and sends messages
65
89
* to the specified board and port combination.
@@ -80,8 +104,11 @@ export class MonitorManager extends CoreClientAware {
80
104
monitor = this . createMonitor ( board , port ) ;
81
105
}
82
106
83
- if ( this . isUploadInProgress ) {
84
- this . startMonitorPendingRequests . push ( [ [ board , port ] , postStartCallback ] ) ;
107
+ if ( this . uploadIsInProgress ( ) ) {
108
+ this . startMonitor_PendingRequests . push ( {
109
+ requestFor : [ board , port ] ,
110
+ postStartCallback,
111
+ } ) ;
85
112
return ;
86
113
}
87
114
@@ -130,21 +157,22 @@ export class MonitorManager extends CoreClientAware {
130
157
* @param port port to monitor
131
158
*/
132
159
async notifyUploadStarted ( board ?: Board , port ?: Port ) : Promise < void > {
133
- this . isUploadInProgress = true ;
134
-
135
160
if ( ! board || ! port ) {
136
161
// We have no way of knowing which monitor
137
162
// to retrieve if we don't have this information.
138
163
return ;
139
164
}
165
+
140
166
const monitorID = this . monitorID ( board , port ) ;
167
+ this . addToMonitorIDsByUploadState ( 'uploadInProgress' , monitorID ) ;
168
+
141
169
const monitor = this . monitorServices . get ( monitorID ) ;
142
170
if ( ! monitor ) {
143
171
// There's no monitor running there, bail
144
172
return ;
145
173
}
146
174
147
- this . servicesPausedForUpload . push ( monitorID ) ;
175
+ this . addToMonitorIDsByUploadState ( 'pausedForUpload' , monitorID ) ;
148
176
return monitor . pause ( ) ;
149
177
}
150
178
@@ -157,33 +185,32 @@ export class MonitorManager extends CoreClientAware {
157
185
* started or if there have been errors.
158
186
*/
159
187
async notifyUploadFinished ( board ?: Board , port ?: Port ) : Promise < Status > {
160
- this . isUploadInProgress = false ;
161
188
let status : Status = Status . NOT_CONNECTED ;
162
189
let portDidChangeOnUpload = false ;
163
190
164
191
// We have no way of knowing which monitor
165
192
// to retrieve if we don't have this information.
166
193
if ( board && port ) {
167
194
const monitorID = this . monitorID ( board , port ) ;
195
+ this . removeFromMonitorIDsByUploadState ( 'uploadInProgress' , monitorID ) ;
196
+
168
197
const monitor = this . monitorServices . get ( monitorID ) ;
169
198
if ( monitor ) {
170
199
status = await monitor . start ( ) ;
171
200
}
172
201
173
- // this monitorID will only be present in "servicesDisposedForUpload "
202
+ // this monitorID will only be present in "disposedForUpload "
174
203
// if the upload changed the board port
175
- portDidChangeOnUpload =
176
- this . servicesDisposedForUpload . includes ( monitorID ) ;
204
+ portDidChangeOnUpload = this . monitorIDIsInUploadState (
205
+ 'disposedForUpload' ,
206
+ monitorID
207
+ ) ;
177
208
if ( portDidChangeOnUpload ) {
178
- this . servicesDisposedForUpload = this . servicesDisposedForUpload . filter (
179
- ( id ) => id !== monitorID
180
- ) ;
209
+ this . removeFromMonitorIDsByUploadState ( 'disposedForUpload' , monitorID ) ;
181
210
}
182
211
183
212
// in case a service was paused but not disposed
184
- this . servicesPausedForUpload = this . servicesPausedForUpload . filter (
185
- ( id ) => id !== monitorID
186
- ) ;
213
+ this . removeFromMonitorIDsByUploadState ( 'pausedForUpload' , monitorID ) ;
187
214
}
188
215
189
216
await this . startQueuedServices ( portDidChangeOnUpload ) ;
@@ -195,11 +222,14 @@ export class MonitorManager extends CoreClientAware {
195
222
// will include a request for our "upload port', most likely at index 0.
196
223
// We remove it, as this port was to be used exclusively for the upload
197
224
const queued = portDidChangeOnUpload
198
- ? this . startMonitorPendingRequests . slice ( 1 )
199
- : this . startMonitorPendingRequests ;
200
- this . startMonitorPendingRequests = [ ] ;
201
-
202
- for ( const [ [ board , port ] , onFinish ] of queued ) {
225
+ ? this . startMonitor_PendingRequests . slice ( 1 )
226
+ : this . startMonitor_PendingRequests ;
227
+ this . startMonitor_PendingRequests = [ ] ;
228
+
229
+ for ( const {
230
+ requestFor : [ board , port ] ,
231
+ postStartCallback : onFinish ,
232
+ } of queued ) {
203
233
const boardsState = await this . boardsService . getState ( ) ;
204
234
const boardIsStillOnPort = Object . keys ( boardsState )
205
235
. map ( ( connection : string ) => {
@@ -281,13 +311,12 @@ export class MonitorManager extends CoreClientAware {
281
311
// we paused it beforehand we know it was disposed
282
312
// of because the upload changed the board port
283
313
if (
284
- this . isUploadInProgress &&
285
- this . servicesPausedForUpload . includes ( monitorID )
314
+ this . uploadIsInProgress ( ) &&
315
+ this . monitorIDIsInUploadState ( 'pausedForUpload' , monitorID )
286
316
) {
287
- this . servicesPausedForUpload = this . servicesPausedForUpload . filter (
288
- ( id ) => id !== monitorID
289
- ) ;
290
- this . servicesDisposedForUpload . push ( monitorID ) ;
317
+ this . removeFromMonitorIDsByUploadState ( 'pausedForUpload' , monitorID ) ;
318
+
319
+ this . addToMonitorIDsByUploadState ( 'disposedForUpload' , monitorID ) ;
291
320
}
292
321
293
322
this . monitorServices . delete ( monitorID ) ;
0 commit comments