Skip to content

Commit e71000a

Browse files
committed
Fix commit hash colouring async issue
1 parent 3987045 commit e71000a

File tree

1 file changed

+49
-54
lines changed

1 file changed

+49
-54
lines changed

download.html

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -376,38 +376,36 @@
376376
let onHashUpdate = [];
377377

378378
let latest_hash = "";
379+
379380
const getLatestHash = async () => {
380-
fetch(
381-
`https://api.github.com/repos/plugdata-team/plugdata/commits/develop`,
382-
)
383-
.then((response) => response.json())
384-
.then((data) => {
385-
latest_hash = data.sha;
386-
console.log("Last Commit Hash:", latest_hash);
387-
for (let i = 0; i < onHashUpdate.length; i++) {
388-
onHashUpdate[i]();
389-
}
390-
})
391-
.catch((error) => console.error("Error:", error));
381+
try {
382+
const response = await fetch(
383+
`https://api.github.com/repos/plugdata-team/plugdata/commits/develop`,
384+
);
385+
const data = await response.json();
386+
latest_hash = data.sha;
387+
console.log("Last Commit Hash:", latest_hash);
388+
onHashUpdate.forEach((fn) => fn());
389+
} catch (error) {
390+
console.error("Error:", error);
391+
}
392392
};
393393

394394
const table = document.getElementById("nightly-table");
395395

396-
for (let i = 0; i < unstable_downloads.length; i++) {
396+
const getDownloadInfo = async (download, i) => {
397397
const downloadRow = document.createElement("tr");
398398

399399
let name = document.createElement("td");
400-
name.textContent = unstable_downloads[i][0];
400+
name.textContent = download[0];
401401

402402
let link = document.createElement("td");
403403
let clickableLink = document.createElement("a");
404404

405-
clickableLink.href = unstable_downloads[i][1];
406-
clickableLink.textContent = unstable_downloads[i][2];
405+
clickableLink.href = download[1];
406+
clickableLink.textContent = download[2];
407407
link.appendChild(clickableLink);
408408

409-
let has_info = unstable_downloads[i][3];
410-
411409
const dummy1 = document.createElement("td");
412410
const dummy2 = document.createElement("td");
413411

@@ -416,64 +414,61 @@
416414
downloadRow.appendChild(dummy2);
417415
downloadRow.appendChild(link);
418416

419-
const getDownloadInfo = async () => {
420-
let url = unstable_downloads[i][1] + ".txt";
417+
table.appendChild(downloadRow);
418+
419+
if (!download[3]) return;
420+
421+
try {
422+
let url = download[1] + ".txt";
421423
console.log(url);
422424
let response = await fetch(url);
423-
const result = await response.text().then((str) => {
424-
return str.split("\n"); // return the string after splitting it.
425-
});
425+
let result = await response.text();
426+
result = result.split("\n");
426427

427-
// Create new cells and set their content
428+
// Create new cells
428429
const date = document.createElement("td");
429430
date.textContent = result[0];
430431
date.style.minWidth = "200px";
431432

432433
const hash = result[1];
433-
let color = hash == latest_hash ? "#006400" : "#FFA500";
434-
435434
const commit_hash = document.createElement("td");
436435
const commit_link = document.createElement("a");
437436

438437
commit_link.textContent = hash.substring(0, 7);
439-
commit_link.style.cssText = `color: ${color}`;
440-
commit_link.href =
441-
"https://github.com/plugdata-team/plugdata/commits/" +
442-
hash;
438+
commit_link.href = `https://github.com/plugdata-team/plugdata/commits/${hash}`;
443439
commit_hash.appendChild(commit_link);
444440

445-
onHashUpdate.push(() => {
446-
let color =
447-
hash == latest_hash ? "#006400" : "#FFA500";
448-
commit_hash.style.cssText = `color: ${color}`;
449-
console.log(
450-
hash == latest_hash
451-
? "hash was equal"
452-
: "hash was not equal",
453-
);
454-
});
455-
456-
downloadRow.removeChild(name);
457-
downloadRow.removeChild(dummy1);
458-
downloadRow.removeChild(dummy2);
459-
downloadRow.removeChild(link);
441+
// Set initial color (will update later)
442+
const updateColor = () => {
443+
const color =
444+
hash === latest_hash ? "#006400" : "#FFA500";
445+
commit_link.style.cssText = `color: ${color}`;
446+
};
447+
updateColor(); // set now
448+
onHashUpdate.push(updateColor); // also set later
460449

461-
// Append the cells to the row
450+
// Replace old cells
451+
downloadRow.innerHTML = "";
462452
downloadRow.appendChild(name);
463453
downloadRow.appendChild(date);
464454
downloadRow.appendChild(commit_hash);
465455
downloadRow.appendChild(link);
466-
};
467-
468-
if (has_info) {
469-
getDownloadInfo();
456+
} catch (e) {
457+
console.error(
458+
"Error fetching info for",
459+
download[0],
460+
e,
461+
);
470462
}
463+
};
471464

472-
// Append the row to the table
473-
table.appendChild(downloadRow);
474-
}
465+
// Main loop: fetch all info
466+
const promises = unstable_downloads.map((download, i) =>
467+
getDownloadInfo(download, i),
468+
);
475469

476-
getLatestHash();
470+
// After all info is fetched, get the latest hash
471+
Promise.all(promises).then(() => getLatestHash());
477472
}
478473

479474
main();

0 commit comments

Comments
 (0)