Compare commits

..

View file

@ -41,28 +41,6 @@ window.marked = marked;
// Helpers ($, el, esc, form, fmtAgeSecs) moved to ./common.js (#406). // Helpers ($, el, esc, form, fmtAgeSecs) moved to ./common.js (#406).
// #464 — atomic-swap render helper. Each managed section's render
// function used to do `root.innerHTML = ''; root.append(...);` in
// sequence; even though both operations sit in the same JS turn,
// operators could still see a "blink" on every poll cycle because
// (a) on async paths the await yield gave the browser a paint
// opportunity, and (b) complex builds with many `el()` allocations
// can blow the browser's per-task budget enough for layout to flash
// empty before the new children land.
//
// The fix: build the new content off-DOM into a `DocumentFragment`,
// then move it into the live root in a single `replaceChildren`
// call. The browser never sees an intermediate empty state. Builder
// callbacks receive the fragment as their `root` argument, so each
// renderer's existing `root.append(...)` code carries over with
// zero internal changes. Early-return inside the builder is fine —
// the commit still happens with whatever the builder appended.
function paintAtomic(liveRoot, build) {
const buf = document.createDocumentFragment();
build(buf);
liveRoot.replaceChildren(buf);
}
// Side panel singleton (Panel) moved to ./common.js (#406). // Side panel singleton (Panel) moved to ./common.js (#406).
// Path linkification + file-preview side panel (openFilePanel, // Path linkification + file-preview side panel (openFilePanel,
@ -1849,28 +1827,26 @@ window.marked = marked;
// agents themselves and the operator already sees them next time // agents themselves and the operator already sees them next time
// they interact with the page. // they interact with the page.
async function refreshReminders() { async function refreshReminders() {
const liveRoot = $('reminders-section'); const root = $('reminders-section');
if (!liveRoot) return; if (!root) return;
try { try {
const resp = await fetch('/api/reminders'); const resp = await fetch('/api/reminders');
if (!resp.ok) { if (!resp.ok) {
paintAtomic(liveRoot, (root) => { root.innerHTML = '';
root.append(el('p', { class: 'empty' }, 'reminders unavailable: http ' + resp.status)); root.append(el('p', { class: 'empty' }, 'reminders unavailable: http ' + resp.status));
});
return; return;
} }
const rows = await resp.json(); const rows = await resp.json();
renderReminders(rows); renderReminders(rows);
} catch (err) { } catch (err) {
paintAtomic(liveRoot, (root) => { root.innerHTML = '';
root.append(el('p', { class: 'empty' }, 'reminders fetch failed: ' + err)); root.append(el('p', { class: 'empty' }, 'reminders fetch failed: ' + err));
});
} }
} }
function renderReminders(rows) { function renderReminders(rows) {
const liveRoot = $('reminders-section'); const root = $('reminders-section');
if (!liveRoot) return; if (!root) return;
paintAtomic(liveRoot, (root) => { root.innerHTML = '';
if (!rows.length) { if (!rows.length) {
root.append(el('p', { class: 'empty' }, 'no queued reminders')); root.append(el('p', { class: 'empty' }, 'no queued reminders'));
return; return;
@ -1934,7 +1910,6 @@ window.marked = marked;
ul.append(li); ul.append(li);
} }
root.append(ul); root.append(ul);
});
} }
function fmtDuration(secs) { function fmtDuration(secs) {
if (secs < 60) return secs + 's'; if (secs < 60) return secs + 's';
@ -1964,16 +1939,14 @@ window.marked = marked;
try { try {
const resp = await fetch('/api/schedules'); const resp = await fetch('/api/schedules');
if (!resp.ok) { if (!resp.ok) {
paintAtomic(listRoot, (root) => { listRoot.innerHTML = '';
root.append(el('p', { class: 'empty' }, 'schedules unavailable: http ' + resp.status)); listRoot.append(el('p', { class: 'empty' }, 'schedules unavailable: http ' + resp.status));
});
return; return;
} }
schedulesState = await resp.json(); schedulesState = await resp.json();
} catch (err) { } catch (err) {
paintAtomic(listRoot, (root) => { listRoot.innerHTML = '';
root.append(el('p', { class: 'empty' }, 'schedules fetch failed: ' + err)); listRoot.append(el('p', { class: 'empty' }, 'schedules fetch failed: ' + err));
});
return; return;
} }
renderScheduleNewForm(); renderScheduleNewForm();
@ -2090,15 +2063,13 @@ window.marked = marked;
} }
function renderScheduleNewForm() { function renderScheduleNewForm() {
const liveRoot = $('schedule-new-section'); const root = $('schedule-new-section');
if (!liveRoot) return; if (!root) return;
// Preserve any field the operator was mid-typing in by reading // Preserve any field the operator was mid-typing in by reading
// current values before we re-render. Only `body` + `description` // current values before we re-render. Only `body` + `description`
// are big enough to feel — the others are toggle/datetime/number. // are big enough to feel — the others are toggle/datetime/number.
// Read from the live root BEFORE the swap so we see current const carry = readScheduleFormCarry(root);
// values (the fragment we build into is fresh). root.innerHTML = '';
const carry = readScheduleFormCarry(liveRoot);
paintAtomic(liveRoot, (root) => {
// Targets multi-select pulls from the live containers list. The // Targets multi-select pulls from the live containers list. The
// backend accepts any string (forward-compat for richer recipients), // backend accepts any string (forward-compat for richer recipients),
@ -2194,7 +2165,6 @@ window.marked = marked;
form_.append(actions); form_.append(actions);
root.append(form_); root.append(form_);
}); // paintAtomic
} }
function readScheduleFormCarry(root) { function readScheduleFormCarry(root) {
return { return {
@ -2279,9 +2249,9 @@ window.marked = marked;
} }
} }
function renderSchedulesList() { function renderSchedulesList() {
const liveRoot = $('schedules-section'); const root = $('schedules-section');
if (!liveRoot) return; if (!root) return;
paintAtomic(liveRoot, (root) => { root.innerHTML = '';
if (!schedulesState.length) { if (!schedulesState.length) {
root.append(el('p', { class: 'empty' }, 'no schedules queued')); root.append(el('p', { class: 'empty' }, 'no schedules queued'));
return; return;
@ -2296,7 +2266,6 @@ window.marked = marked;
}); });
for (const s of sorted) ul.append(renderScheduleCard(s)); for (const s of sorted) ul.append(renderScheduleCard(s));
root.append(ul); root.append(ul);
});
} }
function renderScheduleCard(s) { function renderScheduleCard(s) {
const cancelled = !!s.cancelled_at_unix; const cancelled = !!s.cancelled_at_unix;