Only update after successful fetch to avoid spamming when MPD is unavailable

This commit is contained in:
Henri Bergius 2018-02-07 15:59:35 +01:00
parent 323a8e454f
commit acaf1da268

View file

@ -33,22 +33,17 @@ class MPD extends Component {
return; return;
} }
this.fetchData(); this.fetchData();
if (!this.interval) {
return;
}
this.runInterval = window.setInterval(() => {
this.fetchData();
}, this.interval * 1000);
} }
disconnecting() { disconnecting() {
if (this.runInterval) { if (this.timeout) {
window.clearInterval(this.runInterval); window.clearTimeout(this.timeout);
delete this.runInterval; delete this.timeout;
} }
} }
fetchData() { fetchData() {
delete this.timeout;
const url = `https://c-beam.cbrp3.c-base.org/mpd/${this.mpd}/status/?_=${Date.now()}`; const url = `https://c-beam.cbrp3.c-base.org/mpd/${this.mpd}/status/?_=${Date.now()}`;
fetch(url, { fetch(url, {
headers: { headers: {
@ -58,6 +53,12 @@ class MPD extends Component {
.then(data => data.json()) .then(data => data.json())
.then((data) => { .then((data) => {
this.data = data.content; this.data = data.content;
if (!this.interval) {
return;
}
this.timeout = window.setTimeout(() => {
this.fetchData();
}, this.interval * 1000);
}); });
} }