From 9cd1c74d7d598aa5906900d1055503d123490a4a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 4 Nov 2021 23:00:08 -0700 Subject: [PATCH] Fix disconnect (#68) --- README.md | 1 + package-lock.json | 1 + package.json | 2 +- script/develop | 2 +- src/esp_loader.ts | 17 ++++++++++------- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 29290b63..a45bc185 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ This project is used by the [ESP Web Tools](https://github.com/esphome/esp-web-t - Clone this repository. - Install dependencies with `npm` - Run `script/develop` +- Open http://localhost:5004/ ## Fork diff --git a/package-lock.json b/package-lock.json index 923b2d90..2d68ed85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "esp-web-flasher", "version": "3.2.0", "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 5511b2ae..529bfa50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esp-web-flasher", - "version": "3.2.0", + "version": "4.0.0", "description": "Flash ESP devices using WebSerial", "main": "dist/index.js", "repository": "https://github.com/nabucasa/ESP-Web-flasher", diff --git a/script/develop b/script/develop index 4a1571cb..48993339 100755 --- a/script/develop +++ b/script/develop @@ -11,7 +11,7 @@ trap "kill 0" EXIT # Run tsc once as rollup expects those files npm exec -- tsc || true -npm exec -- serve & +npm exec -- serve -p 5004 & npm exec -- tsc --watch & npm exec -- rollup -c --watch & wait diff --git a/src/esp_loader.ts b/src/esp_loader.ts index 3f20d387..1c47a27f 100644 --- a/src/esp_loader.ts +++ b/src/esp_loader.ts @@ -150,10 +150,10 @@ export class ESPLoader extends EventTarget { } } catch (err) { console.error("Read loop got disconnected"); - // Disconnected! - this.connected = false; - this.dispatchEvent(new Event("disconnect")); } + // Disconnected! + this.connected = false; + this.dispatchEvent(new Event("disconnect")); this.logger.debug("Finished read loop"); } @@ -1148,11 +1148,14 @@ export class ESPLoader extends EventTarget { await this._parent.disconnect(); return; } - if (this._reader) { - await this._reader.cancel(); - } await this.port.writable!.getWriter().close(); - await this.port.close(); + await new Promise((resolve) => { + if (!this._reader) { + resolve(undefined); + } + this.addEventListener("disconnect", resolve, { once: true }); + this._reader!.cancel(); + }); this.connected = false; } }