fix: reset traffic fetched status on connection failure/shutdown

This commit is contained in:
Haishan 2018-10-21 01:13:53 +08:00
parent 4646c216a9
commit 9d81f9c045

View file

@ -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;
}