From 9d81f9c04540d92f1a2a075b932461432ef3ce66 Mon Sep 17 00:00:00 2001 From: Haishan Date: Sun, 21 Oct 2018 01:13:53 +0800 Subject: [PATCH] fix: reset traffic fetched status on connection failure/shutdown --- src/api/traffic.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/api/traffic.js b/src/api/traffic.js index 434384a..2eca663 100644 --- a/src/api/traffic.js +++ b/src/api/traffic.js @@ -51,10 +51,13 @@ const traffic = { } }; +let fetched = false; + function pump(reader) { return reader.read().then(({ done, value }) => { if (done) { console.log('done'); + fetched = false; return; } const t = textDecoder.decode(value); @@ -67,14 +70,15 @@ function pump(reader) { }); } -let fetched = false; function fetchData() { if (fetched) return traffic; const { url, init } = getURLAndInit(); fetch(url, init).then(response => { - fetched = true; - const reader = response.body.getReader(); - pump(reader); + if (response.ok) { + fetched = true; + const reader = response.body.getReader(); + pump(reader); + } }); return traffic; }