Add retry RAF

This commit is contained in:
Aiden Bai 2022-04-29 16:07:48 -07:00
parent d703cd8c0e
commit e82f2bb5d0
No known key found for this signature in database
GPG Key ID: D37584388675FF3A
2 changed files with 10 additions and 7 deletions

View File

@ -1,8 +1,5 @@
async function drawGraph(url, baseUrl, pathColors, depth, enableDrag, enableLegend, enableZoom) {
const container = document.getElementById("graph-container");
// We should clear the graph in case there is anything within it
if (!container) return;
container.textContent = "";
const { index, links, content } = await fetchData;
const curPage = url.replace(baseUrl, "");

View File

@ -51,8 +51,13 @@
window.navigate = navigate;
router(".singlePage");
const callback = () => {
// RAF waits for Million to complete diffing, then draw
requestAnimationFrame(() => {
const draw = () => {
const container = document.getElementById("graph-container");
// retry if the graph is not ready
if (!container) return requestAnimationFrame(draw);
// clear the graph in case there is anything within it
container.textContent = "";
drawGraph(
{{strings.TrimRight "/" .Page.Permalink}},
{{strings.TrimRight "/" .Site.BaseURL}},
@ -61,9 +66,10 @@
{{$.Site.Data.graphConfig.enableDrag}},
{{$.Site.Data.graphConfig.enableLegend}},
{{$.Site.Data.graphConfig.enableZoom}}
)}
);
};
requestAnimationFrame(draw);
};
// We need on initial load, then subsequent redirs
window.addEventListener("million:navigate", callback);
window.addEventListener("DOMContentLoaded", callback);