Add chart loading management
This commit is contained in:
parent
3135dbe378
commit
5d2546ad60
3 changed files with 97 additions and 38 deletions
49
static/chartManager.js
Normal file
49
static/chartManager.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
export let currentLoad = null;
|
||||
const loadInfo = new Map();
|
||||
|
||||
export function newLoad(container) {
|
||||
if (currentLoad) {
|
||||
abortLoad(currentLoad);
|
||||
}
|
||||
reset(container);
|
||||
const controller = new AbortController();
|
||||
const token = { controller, charts: new Map() };
|
||||
loadInfo.set(token, token);
|
||||
currentLoad = token;
|
||||
return token;
|
||||
}
|
||||
|
||||
export function abortLoad(token) {
|
||||
const info = loadInfo.get(token);
|
||||
if (!info) return;
|
||||
info.controller.abort();
|
||||
info.charts.forEach(chart => {
|
||||
try {
|
||||
chart.destroy();
|
||||
} catch (e) {}
|
||||
});
|
||||
loadInfo.delete(token);
|
||||
if (currentLoad === token) {
|
||||
currentLoad = null;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerChart(token, id, chart) {
|
||||
const info = loadInfo.get(token);
|
||||
if (info) {
|
||||
info.charts.set(id, chart);
|
||||
} else {
|
||||
chart.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
export function reset(container) {
|
||||
if (!container) return;
|
||||
container.querySelectorAll('canvas').forEach(c => {
|
||||
const chart = Chart.getChart(c);
|
||||
if (chart) {
|
||||
chart.destroy();
|
||||
}
|
||||
});
|
||||
container.innerHTML = '';
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue