Skip to content

Commit

Permalink
Use an IIFE to Reduce Global Scope Pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
peregrineshahin committed Jul 11, 2024
1 parent 12981ff commit 1f3a16a
Show file tree
Hide file tree
Showing 8 changed files with 970 additions and 973 deletions.
2 changes: 1 addition & 1 deletion server/fishtest/static/js/spsa.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function handleSPSA() {
async function handleSPSA(spsaData) {
let raw = [],
chart_object,
chart_data,
Expand Down
110 changes: 56 additions & 54 deletions server/fishtest/templates/tests.mak
Original file line number Diff line number Diff line change
Expand Up @@ -48,69 +48,71 @@
</div>

<script>
let fetchedMachinesBefore = false;
let machinesSkeleton = null;
let machinesBody = null;
async function handleRenderMachines(){
await DOMContentLoaded();
machinesBody = document.getElementById("machines");
if (!machinesSkeleton) {
machinesSkeleton = document.querySelector("#machines .ssc-wrapper").cloneNode(true);
(() => {
let fetchedMachinesBefore = false;
let machinesSkeleton = null;
let machinesBody = null;
async function handleRenderMachines(){
await DOMContentLoaded();
machinesBody = document.getElementById("machines");
if (!machinesSkeleton) {
machinesSkeleton = document.querySelector("#machines .ssc-wrapper").cloneNode(true);
}
const machinesButton = document.getElementById("machines-button");
machinesButton?.addEventListener("click", async () => {
await toggleMachines();
})
if (${str(machines_shown).lower()})
await renderMachines();
}
const machinesButton = document.getElementById("machines-button");
machinesButton?.addEventListener("click", async () => {
await toggleMachines();
})
if (${str(machines_shown).lower()})
await renderMachines();
}

async function renderMachines() {
await DOMContentLoaded();
if (fetchedMachinesBefore) {
return Promise.resolve();
}
try {
if (document.querySelector("#machines .retry")) {
machinesBody.replaceChildren(machinesSkeleton);
async function renderMachines() {
await DOMContentLoaded();
if (fetchedMachinesBefore) {
return Promise.resolve();
}
const html = await fetchText("/tests/machines");
machinesBody.replaceChildren();
machinesBody.insertAdjacentHTML("beforeend", html);
const machinesTbody = document.querySelector("#machines tbody");
let newMachinesCount = machinesTbody?.childElementCount;
try {
if (document.querySelector("#machines .retry")) {
machinesBody.replaceChildren(machinesSkeleton);
}
const html = await fetchText("/tests/machines");
machinesBody.replaceChildren();
machinesBody.insertAdjacentHTML("beforeend", html);
const machinesTbody = document.querySelector("#machines tbody");
let newMachinesCount = machinesTbody?.childElementCount;

if (newMachinesCount === 1) {
const noMachines = document.getElementById("no-machines");
if (noMachines) newMachinesCount = 0;
}
if (newMachinesCount === 1) {
const noMachines = document.getElementById("no-machines");
if (noMachines) newMachinesCount = 0;
}

const countSpan = document.getElementById("workers-count");
countSpan.textContent = `Workers - ${"${newMachinesCount}"} machines`;
fetchedMachinesBefore = true;
} catch (error) {
console.log("Request failed: " + error);
machinesBody.replaceChildren();
createRetryMessage(machinesBody, renderMachines);
const countSpan = document.getElementById("workers-count");
countSpan.textContent = `Workers - ${"${newMachinesCount}"} machines`;
fetchedMachinesBefore = true;
} catch (error) {
console.log("Request failed: " + error);
machinesBody.replaceChildren();
createRetryMessage(machinesBody, renderMachines);
}
}
}

async function toggleMachines() {
const button = document.getElementById("machines-button");
const active = button.textContent.trim() === "Hide";
if (active) {
button.textContent = "Show";
}
else {
button.textContent = "Hide";
await renderMachines();
}
async function toggleMachines() {
const button = document.getElementById("machines-button");
const active = button.textContent.trim() === "Hide";
if (active) {
button.textContent = "Show";
}
else {
button.textContent = "Hide";
await renderMachines();
}

document.cookie =
"machines_state" + "=" + button.textContent.trim() + "; max-age=${60 * 60}; SameSite=Lax";
}
document.cookie =
"machines_state" + "=" + button.textContent.trim() + "; max-age=${60 * 60}; SameSite=Lax";
}

handleRenderMachines();
handleRenderMachines();
})();
</script>

<h4>
Expand Down
8 changes: 5 additions & 3 deletions server/fishtest/templates/tests_live_elo.mak
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
></script>

<script>
document.title = "Live Elo - ${page_title} | Stockfish Testing";
const testId = "${str(run['_id'])}";
followLive(testId);
(() => {
document.title = "Live Elo - ${page_title} | Stockfish Testing";
const testId = "${str(run['_id'])}";
followLive(testId);
})();
</script>

<h2>Live Elo for SPRT test <a href="/tests/view/${str(run['_id'])}">${str(run['_id'])}</a></h2>
Expand Down
Loading

0 comments on commit 1f3a16a

Please sign in to comment.