From d591cab882ec81291556706fb876f28125192ff4 Mon Sep 17 00:00:00 2001 From: Peter Staev Date: Sat, 27 Jan 2024 18:50:13 +0200 Subject: [PATCH] feat: Monitor for disconnect fix #47 --- CHANGELOG.md | 3 +++ package-lock.json | 4 ++-- package.json | 2 +- src/extension.ts | 13 ++++++------- src/rpc.ts | 4 ++++ 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85a4b87..a01db0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log +## 1.7.1 - 2024-01-27 +* Disconnect monitoring (#47) + ## 1.7.0 - 2022-04-10 * Multifile support (#58) * Updated SerialPort dependencies diff --git a/package-lock.json b/package-lock.json index 22ded4b..2350414 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "lego-spikeprime-mindstorms-vscode", - "version": "1.7.0", + "version": "1.7.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "lego-spikeprime-mindstorms-vscode", - "version": "1.7.0", + "version": "1.7.1", "license": "Apache-2.0", "dependencies": { "@pybricks/mpy-cross-v5": "^2.0.0", diff --git a/package.json b/package.json index fa26bb5..7cf15bb 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "LEGO SPIKE Prime / MINDSTORMS Robot Inventor Extension", "description": "Helps you connect Lego's SPIKE Prime/MINDSTORMS Robot Inventor", "publisher": "PeterStaev", - "version": "1.7.0", + "version": "1.7.1", "engines": { "vscode": "^1.66.0" }, diff --git a/src/extension.ts b/src/extension.ts index 6628544..31b901f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -68,6 +68,7 @@ export function activate(context: vscode.ExtensionContext) { }, () => rpc.open(), ); + rpc.onClosed.event(() => void updateHubStatusBarItem()); await updateHubStatusBarItem(); showTerminal(); @@ -91,8 +92,6 @@ export function activate(context: vscode.ExtensionContext) { }, () => rpc.close(), ); - - await updateHubStatusBarItem(); }); const uploadProgramCommand = vscode.commands.registerCommand("lego-spikeprime-mindstorms-vscode.uploadProgram", async () => { @@ -332,11 +331,11 @@ async function performUploadProgram(slotId: number, type: "python" | "scratch", const assembledFile = assembleFile(currentlyOpenTabFileUri.fsPath); let assembledFilePath; - if (config.get("legoSpikePrimeMindstorms.saveFileToUpload")){ + if (config.get("legoSpikePrimeMindstorms.saveFileToUpload")) { assembledFilePath = path.join(path.dirname(currentlyOpenTabFilePath), currentlyOpenTabFileName + ".assembled.py"); } - else{ - assembledFilePath =path.join(os.tmpdir(), currentlyOpenTabFileName + ".assembled.py"); + else { + assembledFilePath = path.join(os.tmpdir(), currentlyOpenTabFileName + ".assembled.py"); } fs.writeFileSync(assembledFilePath, assembledFile, "utf8"); @@ -535,12 +534,12 @@ function assembleFile(filePath: string): Uint8Array | undefined { let includePath = match[1] + ".py"; includePath = path.resolve(path.dirname(filePath), includePath); - if(!fs.existsSync(includePath)){ + if (!fs.existsSync(includePath)) { vscode.window.showWarningMessage("File: " + includePath + " not found"); continue; } assembledLines.splice(index, 1); - if((includedFiles.some(includedFile => includedFile === includePath))) + if ((includedFiles.some(includedFile => includedFile === includePath))) continue; try { startLine = index; diff --git a/src/rpc.ts b/src/rpc.ts index f42a0cc..227b1ba 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -8,6 +8,7 @@ import { Logger } from "./logger"; import { getRandomString } from "./utils"; export class Rpc { + public onClosed: vscode.EventEmitter = new vscode.EventEmitter(); public get isOpenIn(): boolean { return this._serialPort?.isOpen; } @@ -86,6 +87,9 @@ export class Rpc { console.error(e); } }); + this._serialPort.on("close", () => { + this.onClosed.fire(); + }); } public open(): Promise {