Fix stacked updates

This commit is contained in:
Jiang Yio 2023-05-03 01:44:21 -04:00
parent 85cefa1b7b
commit 6a284e9b6b

View File

@ -62,6 +62,12 @@
import Autocomplete from './Autocomplete.vue'; import Autocomplete from './Autocomplete.vue';
function clearTimeouts(timers) {
if(timers.length > 1) console.warn('Clearing multiple timeouts', timers.slice());
for(var i = 0; i < timers.length; ++i) window.clearTimeout(timers[i]);
timers.length = 0;
}
export default { export default {
components: { components: {
Autocomplete Autocomplete
@ -78,6 +84,7 @@
data() { data() {
return { return {
appointments: [], appointments: [],
timers: [],
ts: null, ts: null,
age: undefined, age: undefined,
filter: {} filter: {}
@ -118,16 +125,17 @@
return true; return true;
}, },
async update() { async update() {
clearTimeouts(this.timers);
try { try {
this.appointments = (await this.client.SDEC_CRSCHED(this.selection.join('|') + '|', strfdate_vista(this.date_begin), strfdate_vista(this.date_end) + '@2359')).sort((a, b) => (new Date(a.START_TIME)) - (new Date(b.START_TIME))); this.appointments = (await this.client.SDEC_CRSCHED(this.selection.join('|') + '|', strfdate_vista(this.date_begin), strfdate_vista(this.date_end) + '@2359')).sort((a, b) => (new Date(a.START_TIME)) - (new Date(b.START_TIME)));
var now = new Date(); var now = new Date();
this.ts = this.appointments._ts ? new Date(1000*this.appointments._ts) : now; this.ts = this.appointments._ts ? new Date(1000*this.appointments._ts) : now;
this.age = now - this.ts; this.age = now - this.ts;
this.timer = window.setTimeout(this.update, Math.max(60000 - this.age, 10000)); this.timers.push(window.setTimeout(this.update, Math.max(60000 - this.age, 10000)));
} catch(ex) { } catch(ex) {
this.age = this.ts ? (new Date()) - this.ts : Infinity; this.age = this.ts ? (new Date()) - this.ts : Infinity;
console.warn(ex); console.warn(ex);
this.timer = window.setTimeout(this.update, 30000); this.timers.push(window.setTimeout(this.update, 30000));
} }
} }
}, },
@ -136,13 +144,12 @@
() => (this.client, this.selection, this.date_begin, this.date_end, {}), () => (this.client, this.selection, this.date_begin, this.date_end, {}),
debounce(async () => { debounce(async () => {
this.filter = {}; this.filter = {};
window.clearTimeout(this.timer);
this.update(); this.update();
}, 500) }, 500)
); );
}, },
unmounted() { unmounted() {
window.clearTimeout(this.timer); clearTimeouts(this.timers);
} }
}; };
</script> </script>