@@ -65,10 +65,10 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
65
65
protected _availablePorts : Port [ ] = [ ] ;
66
66
protected _availableBoards : AvailableBoard [ ] = [ ] ;
67
67
68
- private lastItemRemovedForUpload : { board : Board ; port : Port } | undefined ;
68
+ private lastBoardsConfigOnUpload : BoardsConfig . Config | undefined ;
69
69
// "lastPersistingUploadPort", is a port created during an upload, that persisted after
70
70
// the upload finished, it's "substituting" the port selected when the user invoked the upload
71
- private lastPersistingUploadPort : Port | undefined ;
71
+ private lastPersistingUploadPortWithBoard : BoardsConfig . Config | undefined ;
72
72
73
73
/**
74
74
* Unlike `onAttachedBoardsChanged` this even fires when the user modifies the selected board in the IDE.\
@@ -116,62 +116,57 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
116
116
return this . _reconciled . promise ;
117
117
}
118
118
119
- private checkForItemRemoved ( event : AttachedBoardsChangeEvent ) : void {
120
- if ( ! this . lastItemRemovedForUpload ) {
121
- const {
122
- oldState : { ports : oldPorts , boards : oldBoards } ,
123
- newState : { ports : newPorts } ,
124
- } = event ;
119
+ public setLastBoardsConfigOnUpload (
120
+ value : BoardsConfig . Config | undefined
121
+ ) : void {
122
+ this . lastBoardsConfigOnUpload = value ;
123
+ }
125
124
126
- const disappearedPorts = oldPorts . filter ( ( oldPort : Port ) =>
127
- newPorts . every ( ( newPort : Port ) => ! Port . sameAs ( oldPort , newPort ) )
128
- ) ;
125
+ private derivePersistingUploadPort ( event : AttachedBoardsChangeEvent ) : void {
126
+ if ( ! this . lastBoardsConfigOnUpload ) {
127
+ this . lastPersistingUploadPortWithBoard = undefined ;
128
+ return ;
129
+ }
129
130
130
- if ( disappearedPorts . length > 0 ) {
131
- this . lastItemRemovedForUpload = {
132
- board : oldBoards . find ( ( board : Board ) =>
133
- Port . sameAs ( board . port , disappearedPorts [ 0 ] )
134
- ) as Board ,
135
- port : disappearedPorts [ 0 ] ,
136
- } ;
137
- }
131
+ const {
132
+ oldState : { ports : oldPorts } ,
133
+ newState : { ports : newPorts , boards : newBoards } ,
134
+ } = event ;
138
135
136
+ if ( newPorts . length === 0 ) {
137
+ setTimeout ( ( ) => {
138
+ this . setLastBoardsConfigOnUpload ( undefined ) ;
139
+ } , 5000 ) ;
139
140
return ;
140
141
}
141
- }
142
142
143
- private checkForPersistingPort ( event : AttachedBoardsChangeEvent ) : void {
144
- if ( this . lastItemRemovedForUpload ) {
145
- const {
146
- oldState : { ports : oldPorts } ,
147
- newState : { ports : newPorts , boards : newBoards } ,
148
- } = event ;
143
+ const lastSelectionOnUpload = this . lastBoardsConfigOnUpload ;
144
+ this . setLastBoardsConfigOnUpload ( undefined ) ;
149
145
150
- const disappearedItem = this . lastItemRemovedForUpload ;
151
- this . lastItemRemovedForUpload = undefined ;
146
+ const appearedPorts =
147
+ oldPorts . length > 0
148
+ ? newPorts . filter ( ( newPort : Port ) =>
149
+ oldPorts . every ( ( oldPort : Port ) => ! Port . sameAs ( newPort , oldPort ) )
150
+ )
151
+ : newPorts ;
152
152
153
- const appearedPorts = newPorts . filter ( ( newPort : Port ) =>
154
- oldPorts . every ( ( oldPort : Port ) => ! Port . sameAs ( newPort , oldPort ) )
153
+ if ( appearedPorts . length > 0 ) {
154
+ const boardOnAppearedPort = newBoards . find ( ( board : Board ) =>
155
+ Port . sameAs ( board . port , appearedPorts [ 0 ] )
155
156
) ;
156
157
157
- if ( appearedPorts . length > 0 ) {
158
- const boardOnAppearedPort = newBoards . find ( ( board : Board ) =>
159
- Port . sameAs ( board . port , appearedPorts [ 0 ] )
160
- ) ;
161
-
162
- if (
163
- boardOnAppearedPort &&
164
- Board . sameAs ( boardOnAppearedPort , disappearedItem . board )
165
- ) {
166
- this . lastPersistingUploadPort = appearedPorts [ 0 ] ;
167
- return ;
168
- }
158
+ if (
159
+ boardOnAppearedPort &&
160
+ lastSelectionOnUpload . selectedBoard &&
161
+ Board . sameAs ( boardOnAppearedPort , lastSelectionOnUpload . selectedBoard )
162
+ ) {
163
+ this . lastPersistingUploadPortWithBoard = {
164
+ selectedBoard : boardOnAppearedPort ,
165
+ selectedPort : appearedPorts [ 0 ] ,
166
+ } ;
167
+ return ;
169
168
}
170
-
171
- return ;
172
169
}
173
-
174
- this . lastPersistingUploadPort = undefined ;
175
170
}
176
171
177
172
protected notifyAttachedBoardsChanged (
@@ -185,10 +180,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
185
180
186
181
const { uploadInProgress } = event ;
187
182
188
- if ( uploadInProgress ) {
189
- this . checkForItemRemoved ( event ) ;
190
- } else {
191
- this . checkForPersistingPort ( event ) ;
183
+ if ( ! uploadInProgress ) {
184
+ this . derivePersistingUploadPort ( event ) ;
192
185
}
193
186
194
187
this . _attachedBoards = event . newState . boards ;
@@ -317,36 +310,11 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
317
310
// If we could not find an exact match, we compare the board FQBN-name pairs and ignore the port, as it might have changed.
318
311
// See documentation on `latestValidBoardsConfig`.
319
312
320
- if ( ! this . lastPersistingUploadPort ) return false ;
321
-
322
- const lastPersistingUploadPort = this . lastPersistingUploadPort ;
323
- this . lastPersistingUploadPort = undefined ;
313
+ if ( ! this . lastPersistingUploadPortWithBoard ) return false ;
324
314
325
- if (
326
- ! Port . sameAs (
327
- lastPersistingUploadPort ,
328
- this . latestValidBoardsConfig . selectedPort
329
- )
330
- ) {
331
- return false ;
332
- }
333
-
334
- for ( const board of this . availableBoards . filter (
335
- ( { state } ) => state !== AvailableBoard . State . incomplete
336
- ) ) {
337
- if (
338
- this . latestValidBoardsConfig . selectedBoard . fqbn === board . fqbn &&
339
- this . latestValidBoardsConfig . selectedBoard . name === board . name &&
340
- this . latestValidBoardsConfig . selectedPort . protocol ===
341
- board . port ?. protocol
342
- ) {
343
- this . boardsConfig = {
344
- ...this . latestValidBoardsConfig ,
345
- selectedPort : board . port ,
346
- } ;
347
- return true ;
348
- }
349
- }
315
+ this . boardsConfig = this . lastPersistingUploadPortWithBoard ;
316
+ this . lastPersistingUploadPortWithBoard = undefined ;
317
+ return true ;
350
318
}
351
319
return false ;
352
320
}
0 commit comments