From caa5a2893f256efaed5691de85906cb7ff310b90 Mon Sep 17 00:00:00 2001 From: Sam Kent Date: Wed, 16 Oct 2019 11:24:20 +0200 Subject: [PATCH 1/3] Moved webusb timeout message so it only shows when the timeout is valid --- partial-flashing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/partial-flashing.js b/partial-flashing.js index 550de37e..772eb805 100644 --- a/partial-flashing.js +++ b/partial-flashing.js @@ -699,7 +699,6 @@ let PartialFlashing = { let timeout = new Promise((resolve, reject) => { setTimeout(() => { - PartialFlashingUtils.log("Resetting micro:bit timed out"); reject('Timeout') }, 1000) }) @@ -717,6 +716,7 @@ let PartialFlashing = { } catch (err) { // Fall back to full flash if attempting to reset times out. if (err === "Timeout") { + PartialFlashingUtils.log("Resetting micro:bit timed out"); PartialFlashingUtils.log("Partial flashing failed. Attempting Full Flash"); // Send event var details = { From 5f8445b8f2dc42e8bc03ebd6464d1f813d0102e4 Mon Sep 17 00:00:00 2001 From: Sam Kent Date: Wed, 16 Oct 2019 11:51:41 +0200 Subject: [PATCH 2/3] clear timeout instead --- partial-flashing.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/partial-flashing.js b/partial-flashing.js index 772eb805..f1df420e 100644 --- a/partial-flashing.js +++ b/partial-flashing.js @@ -685,6 +685,8 @@ let PartialFlashing = { // Drawn from https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/editor/extension.tsx#L439 flashAsync: async function(dapwrapper, image, updateProgress) { try { + let resetTimeout = null; + let p = Promise.resolve() .then(() => { // Reset micro:bit to ensure interface responds correctly. @@ -698,7 +700,8 @@ let PartialFlashing = { }); let timeout = new Promise((resolve, reject) => { - setTimeout(() => { + resetTimeout = setTimeout(() => { + PartialFlashingUtils.log("Resetting micro:bit timed out"); reject('Timeout') }, 1000) }) @@ -706,6 +709,10 @@ let PartialFlashing = { // Use race to timeout the reset. let ret = await Promise.race([p, timeout]) .then(() => { + // Cancel reset timeout + clearTimeout(resetTimeout); + + // Start flashing PartialFlashingUtils.log("Begin Flashing"); return this.partialFlashAsync(dapwrapper, image, updateProgress); }) @@ -716,7 +723,6 @@ let PartialFlashing = { } catch (err) { // Fall back to full flash if attempting to reset times out. if (err === "Timeout") { - PartialFlashingUtils.log("Resetting micro:bit timed out"); PartialFlashingUtils.log("Partial flashing failed. Attempting Full Flash"); // Send event var details = { From dffcbbdbdb010dca6e6e7745418ed7b414b32c3a Mon Sep 17 00:00:00 2001 From: Sam Kent Date: Wed, 16 Oct 2019 14:17:45 +0200 Subject: [PATCH 3/3] Resolve race without reject --- partial-flashing.js | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/partial-flashing.js b/partial-flashing.js index f1df420e..aaa4eb11 100644 --- a/partial-flashing.js +++ b/partial-flashing.js @@ -685,8 +685,6 @@ let PartialFlashing = { // Drawn from https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/editor/extension.tsx#L439 flashAsync: async function(dapwrapper, image, updateProgress) { try { - let resetTimeout = null; - let p = Promise.resolve() .then(() => { // Reset micro:bit to ensure interface responds correctly. @@ -700,40 +698,37 @@ let PartialFlashing = { }); let timeout = new Promise((resolve, reject) => { - resetTimeout = setTimeout(() => { - PartialFlashingUtils.log("Resetting micro:bit timed out"); - reject('Timeout') + setTimeout(() => { + resolve("timeout"); }, 1000) }) // Use race to timeout the reset. let ret = await Promise.race([p, timeout]) - .then(() => { - // Cancel reset timeout - clearTimeout(resetTimeout); - - // Start flashing - PartialFlashingUtils.log("Begin Flashing"); - return this.partialFlashAsync(dapwrapper, image, updateProgress); + .then((result) => { + if(result === "timeout") { + PartialFlashingUtils.log("Resetting micro:bit timed out"); + PartialFlashingUtils.log("Partial flashing failed. Attempting Full Flash"); + // Send event + var details = { + "flash-type": "partial-flash", + "event-type": "error", + "message": "flash-failed" + "/" + "attempting-full-flash" + }; + + document.dispatchEvent(new CustomEvent('webusb', { detail: details })); + return this.fullFlashAsync(dapwrapper, image); + } else { + // Start flashing + PartialFlashingUtils.log("Begin Flashing"); + return this.partialFlashAsync(dapwrapper, image, updateProgress); + } }) .finally(() => { return dapwrapper.disconnectAsync(); }); return ret; } catch (err) { - // Fall back to full flash if attempting to reset times out. - if (err === "Timeout") { - PartialFlashingUtils.log("Partial flashing failed. Attempting Full Flash"); - // Send event - var details = { - "flash-type": "partial-flash", - "event-type": "error", - "message": "flash-failed" + "/" + "attempting-full-flash" - }; - - document.dispatchEvent(new CustomEvent('webusb', { detail: details })); - return this.fullFlashAsync(dapwrapper, image); - } return Promise.reject(err); } }