Skip to content

Commit

Permalink
[WIP]: leverage upload event to derived new port
Browse files Browse the repository at this point in the history
  • Loading branch information
davegarthsimpson committed Aug 22, 2022
1 parent b15603c commit aedf059
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 51 deletions.
82 changes: 31 additions & 51 deletions arduino-ide-extension/src/browser/boards/boards-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
protected _availablePorts: Port[] = [];
protected _availableBoards: AvailableBoard[] = [];

private lastItemRemovedForUpload: { board: Board; port: Port } | undefined;
private lastBoardsConfigOnUpload: BoardsConfig.Config | undefined;
// "lastPersistingUploadPort", is a port created during an upload, that persisted after
// the upload finished, it's "substituting" the port selected when the user invoked the upload
private lastPersistingUploadPort: Port | undefined;
Expand Down Expand Up @@ -116,62 +116,44 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
return this._reconciled.promise;
}

private checkForItemRemoved(event: AttachedBoardsChangeEvent): void {
if (!this.lastItemRemovedForUpload) {
const {
oldState: { ports: oldPorts, boards: oldBoards },
newState: { ports: newPorts },
} = event;

const disappearedPorts = oldPorts.filter((oldPort: Port) =>
newPorts.every((newPort: Port) => !Port.sameAs(oldPort, newPort))
);

if (disappearedPorts.length > 0) {
this.lastItemRemovedForUpload = {
board: oldBoards.find((board: Board) =>
Port.sameAs(board.port, disappearedPorts[0])
) as Board,
port: disappearedPorts[0],
};
}
public setLastBoardsConfigOnUpload(
value: BoardsConfig.Config | undefined
): void {
this.lastBoardsConfigOnUpload = value;
}

private derivePersistingUploadPort(event: AttachedBoardsChangeEvent): void {
if (!this.lastBoardsConfigOnUpload) {
this.lastPersistingUploadPort = undefined;
return;
}
}

private checkForPersistingPort(event: AttachedBoardsChangeEvent): void {
if (this.lastItemRemovedForUpload) {
const {
oldState: { ports: oldPorts },
newState: { ports: newPorts, boards: newBoards },
} = event;
const lastSelectionOnUpload = this.lastBoardsConfigOnUpload;
this.setLastBoardsConfigOnUpload(undefined);

const disappearedItem = this.lastItemRemovedForUpload;
this.lastItemRemovedForUpload = undefined;
const {
oldState: { ports: oldPorts },
newState: { ports: newPorts, boards: newBoards },
} = event;

const appearedPorts = newPorts.filter((newPort: Port) =>
oldPorts.every((oldPort: Port) => !Port.sameAs(newPort, oldPort))
);
const appearedPorts = newPorts.filter((newPort: Port) =>
oldPorts.every((oldPort: Port) => !Port.sameAs(newPort, oldPort))
);

if (appearedPorts.length > 0) {
const boardOnAppearedPort = newBoards.find((board: Board) =>
Port.sameAs(board.port, appearedPorts[0])
);
if (appearedPorts.length > 0) {
const boardOnAppearedPort = newBoards.find((board: Board) =>
Port.sameAs(board.port, appearedPorts[0])
);

if (
boardOnAppearedPort &&
Board.sameAs(boardOnAppearedPort, disappearedItem.board)
) {
this.lastPersistingUploadPort = appearedPorts[0];
return;
}
if (
boardOnAppearedPort &&
lastSelectionOnUpload.selectedBoard &&
Board.sameAs(boardOnAppearedPort, lastSelectionOnUpload.selectedBoard)
) {
this.lastPersistingUploadPort = appearedPorts[0];
return;
}

return;
}

this.lastPersistingUploadPort = undefined;
}

protected notifyAttachedBoardsChanged(
Expand All @@ -185,10 +167,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {

const { uploadInProgress } = event;

if (uploadInProgress) {
this.checkForItemRemoved(event);
} else {
this.checkForPersistingPort(event);
if (!uploadInProgress) {
this.derivePersistingUploadPort(event);
}

this._attachedBoards = event.newState.boards;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ export class UploadSketch extends CoreServiceContribution {
// toggle the toolbar button and menu item state.
// uploadInProgress will be set to false whether the upload fails or not
this.uploadInProgress = true;
this.boardsServiceProvider.setLastBoardsConfigOnUpload(
this.boardsServiceProvider.boardsConfig
);
this.onDidChangeEmitter.fire();
this.clearVisibleNotification();

Expand Down

0 comments on commit aedf059

Please sign in to comment.