Page the feed from the offset the server has read, a row earlier for each vote or read since, and drop the rows already on screen, so a pass or a role opened with Show read off no longer skips a role and shows another twice

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-10-05 09:34:50 +02:00
1 parent 92ad8c2c1b
commit 9907816cc0
2 files changed
+280 -15

No files matched your search

+91 -15
View File
@@ -91,8 +91,11 @@
* an old `?seniority=` is ignored.
*
* The list is paged: 60 rows, then 60 more each time the end of the list comes
* within 600px of the screen, with the offset the rows already shown. Back to
* this page from another tab puts the same number of rows and the scroll
* within 600px of the screen. The server sorts afresh on every request and a
* vote or a read moves a role in its list or out of it, so the next page
* starts a row earlier for each and a row already on screen is dropped
* (`pageAsk`, `pageMerge`): none is skipped and none is shown twice. Back
* to this page from another tab puts the same number of rows and the scroll
* position back, from sessionStorage.
*/
(function () {
@@ -194,6 +197,10 @@
tags: [], // [{tag, count, unread}] from /api/filters
items: [], // the rows on screen, in order
total: 0, // how many roles the filters match in all
offset: 0, // how far into the server's list the pages have read
more: false, // the server's list goes on past `offset`
moved: 0, // votes and reads no page has been asked after yet (`pageAsk`)
movedOnce: 0, // those one page has been asked after
loaded: false, // the first page has landed
loading: false, // a page is in flight, so the pager holds off
selected: 0, // which row the keyboard is on
@@ -404,18 +411,57 @@
// -------------------------------------------------------------- loading
/** The most rows one request may ask for (`limit` on /api/roles). */
const MOST = 1000;
/* Where the next page starts and how many rows it asks for. The server
sorts afresh on every request, and a vote moves a role in its order
(`_VOTED_SCORE`) or out of the list (Judged), as a read does with Show
read off. Each row that leaves the part already read lets a row from
below up into it, which a page asked from `offset` would never show, so
the page starts one row earlier for each such change and is that much
longer; the rows it repeats are dropped (`pageMerge`). `moved` holds
each change for two pages, the one asked after it and the next
(`state.moved` and `state.movedOnce`), since a vote still in flight
when a page is asked may land after the server has read that page. */
function pageAsk(offset, moved, page = PAGE) {
const back = Math.min(offset, moved, MOST - page);
return { offset: offset - back, limit: page + back };
}
/* An answer onto the rows on screen: the rows not on screen yet after
them, in the server's order, so the role a pass sank is not shown a
second time; how far into the server's list the pages have now read;
and whether it goes on. A short page is its end whatever `total` says,
since the count and the page are two queries. */
function pageMerge(items, ask, answer) {
const shown = new Set(items.map((r) => r.uid));
const offset = ask.offset + answer.items.length;
return {
items: items.concat(answer.items.filter((r) => !shown.has(r.uid))),
offset,
more: answer.items.length >= ask.limit && offset < answer.total,
};
}
// For the tests, which page through a list sorted afresh without a DOM.
window.feedPaging = { pageAsk, pageMerge };
let feedRequest = 0;
/* A reload starts the list again; an append asks for the rows after the
ones shown. `keep` asks for that many in one go, which is how the list is
put back as it was. The latch is let go only by the newest request: a
reload overtaking an append must not let a second append start with an
offset into the list being replaced. */
ones read (`pageAsk`). `keep` asks for that many in one go, which is how
the list is put back as it was. The latch is let go only by the newest
request: a reload overtaking an append must not let a second append
start with an offset into the list being replaced. */
async function loadFeed({ append = false, keep = 0 } = {}) {
if (append && (state.loading || state.items.length >= state.total)) return;
if (append && (state.loading || !state.more)) return;
const ask = append ? pageAsk(state.offset, state.moved + state.movedOnce)
: { offset: 0, limit: Math.min(MOST, Math.max(PAGE, keep)) };
const params = feedParams();
params.set("offset", String(append ? state.items.length : 0));
params.set("limit", String(Math.min(1000, Math.max(PAGE, keep))));
params.set("offset", String(ask.offset));
params.set("limit", String(ask.limit));
const moved = state.moved;
const request = ++feedRequest;
state.loading = true;
let data;
@@ -436,10 +482,17 @@
}
if (request !== feedRequest) return;
state.feedError = false;
// The changes made before this page was asked are counted once more, for
// the next; those made while it was in flight wait for theirs.
state.movedOnce = moved;
state.moved -= moved;
const held = append ? state.items[state.selected]?.uid : state.readingUid;
const page = pageMerge(append ? state.items : [], ask, data);
state.total = data.total;
state.items = append ? state.items.concat(data.items) : data.items;
state.items = page.items;
state.offset = page.offset;
state.more = page.more;
state.loaded = true;
const at = held ? state.items.findIndex((r) => r.uid === held) : -1;
state.selected = at >= 0 ? at : append ? state.selected : 0;
@@ -1359,11 +1412,13 @@
}
// The end of the list says which end it is: a list that has run out and
// one that is still loading look the same otherwise.
// one that is still loading look the same otherwise. Whether it has run
// out is the server's to say (`pageMerge`), since the rows on screen
// keep the ones a vote or a read has since taken out of its list.
const end = $("#feed-end");
const n = state.items.length;
end.hidden = !state.loaded || !n;
end.textContent = n < state.total
end.textContent = state.more
? "Loading more…"
: `${number.format(n)} ${n === 1 ? "role" : "roles"} — that is all of them.`;
}
@@ -1454,7 +1509,8 @@
rows out at once, since a hidden company is never in the feed; where the
others rank waits on the `score` it queued, whose id the affinity route
answers with (`updated.score_job`), and the feed reloads when that has
finished. */
finished. A list the company filter has come off meanwhile has its rows
asked again in the new order, as `renderAffinity` does. */
async function affinityChanged(key, company, updated) {
if (!company) await loadCompanies(state.filter.agent).catch(() => {});
if (updated?.affinity === -1) reload(); else renderFeed();
@@ -1463,6 +1519,7 @@
await scored(updated?.score_job);
if (status()) status().textContent = "";
if (state.filter.companyKeys.includes(key)) reload();
else loadFeed({ keep: state.items.length });
}
/* Wait for the `score` job `job`, the way agents.js's `rescore` waits:
@@ -1562,6 +1619,8 @@
async function markRead(uid) {
const item = state.items.find((r) => r.uid === uid);
if (item && item.read_at) return;
// With Show read off the server's list loses it, see `pageAsk`.
if (!state.filter.showRead) state.moved += 1;
if (item) { item.read_at = new Date().toISOString(); renderFeed(); }
try {
const role = await api(`/api/roles/${encodeURIComponent(uid)}/read`, { method: "POST" });
@@ -1808,14 +1867,21 @@
Companies page, and its two fetch overrides beside them. A company seen
only through a search has no row until its first change creates one, so
the list is reloaded then, and the overrides, which need a row, wait for it.
Hiding reloads the feed, which no longer holds the company's roles. */
Hiding reloads the feed, which no longer holds the company's roles. Any
other stance moves every role of the company once the `score` it queued
has run, up into the rows already read as well as down out of them,
which no page asked from an offset makes up for (`pageAsk`); so the rows
on screen are asked again then, as many, in the new order. */
function renderAffinity(slot, r) {
const company = companies.get(r.company_key);
if (!r.company_key) { slot.replaceChildren(); return; }
slot.replaceChildren(affinityControl(r.company_key, company ? company.affinity : 0, async (updated) => {
if (!company) await loadCompanies(state.filter.agent).catch(() => {});
renderAffinity(slot, r);
if (updated?.affinity === -1) reload(); else renderFeed();
if (updated?.affinity === -1) { reload(); return; }
renderFeed();
await scored(updated?.score_job);
loadFeed({ keep: state.items.length });
}, state.filter.agent));
if (company) {
slot.append(" ", overridesControl(r.company_key, company,
@@ -2125,6 +2191,16 @@
new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) loadFeed({ append: true });
}, { rootMargin: "600px" }).observe($("#feed-end"));
// ▼ ● ▲ and ★, which likes, on a row or in the reader move the role in
// the server's order, or out of its list under Judged or with Show read
// off, which the next page makes up for (`pageAsk`). The keys press the
// same buttons. Counted at the click, failed or undone ones too, since a
// page that starts a row too early only repeats one.
for (const pane of [$("#roles-list"), $("#reader")]) {
pane.addEventListener("click", (e) => {
if (e.target.closest(".judge button")) state.moved += 1;
});
}
// Kept on the way out, whichever way that is, so back finds it.
window.addEventListener("pagehide", keep);