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:
1 parent
92ad8c2c1b
commit
9907816cc0
2 files changed
+280
-15
No files matched your search
+91
-15
@@ -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);
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
// The feed's paging, static/roles.js `pageAsk` and `pageMerge`, without a
|
||||
// browser: against a fake /api/roles that sorts afresh on every request, as
|
||||
// the store does, with a pass halving a role's score and a like lifting it a
|
||||
// fifth (`_VOTED_SCORE`), and Show read off or Judged taking a role out.
|
||||
//
|
||||
// node --test tests/js/*.test.mjs
|
||||
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import vm from "node:vm";
|
||||
|
||||
// Only what roles.js touches before DOMContentLoaded.
|
||||
const window = {
|
||||
matchMedia: () => ({ matches: false, addEventListener() {} }),
|
||||
addEventListener() {},
|
||||
};
|
||||
const document = { addEventListener() {} };
|
||||
Object.assign(globalThis, { window, document });
|
||||
vm.runInThisContext(readFileSync(new URL("../../app/static/roles.js", import.meta.url), "utf8"));
|
||||
const { pageAsk, pageMerge } = window.feedPaging;
|
||||
|
||||
const PAGE = 60;
|
||||
|
||||
// ------------------------------------------------------------ the server
|
||||
|
||||
/** `n` roles, scores falling evenly from 0.99, all unread and unjudged. */
|
||||
function server(n, { includeRead = true, judged = "" } = {}) {
|
||||
const roles = Array.from({ length: n }, (_, i) => ({
|
||||
uid: `r${String(i).padStart(3, "0")}`, score: 0.99 - (i * 0.5) / n, vote: null, read: false,
|
||||
}));
|
||||
const voted = (r) => Math.min(1, r.score * (r.vote === -1 ? 0.5 : r.vote === 1 ? 1.2 : 1));
|
||||
function list() {
|
||||
return roles
|
||||
.filter((r) => includeRead || !r.read)
|
||||
.filter((r) => judged !== "unjudged" || r.vote === null)
|
||||
.sort((a, b) => voted(b) - voted(a) || a.uid.localeCompare(b.uid));
|
||||
}
|
||||
return {
|
||||
page: ({ offset, limit }) => {
|
||||
const all = list();
|
||||
return { items: all.slice(offset, offset + limit).map((r) => ({ uid: r.uid })), total: all.length };
|
||||
},
|
||||
uids: () => list().map((r) => r.uid),
|
||||
// A vote stamps the role read, as `Store.set_vote` does.
|
||||
vote: (uid, value) => { const r = roles.find((x) => x.uid === uid); r.vote = value; r.read = true; },
|
||||
read: (uid) => { roles.find((x) => x.uid === uid).read = true; },
|
||||
};
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ the client
|
||||
|
||||
/** loadFeed's bookkeeping: the pages asked with `pageAsk`, merged with
|
||||
`pageMerge`, each change counted for two pages. */
|
||||
function client(api) {
|
||||
const c = { items: [], offset: 0, more: false, moved: 0, movedOnce: 0 };
|
||||
c.load = (append = false) => {
|
||||
const ask = append ? pageAsk(c.offset, c.moved + c.movedOnce) : { offset: 0, limit: PAGE };
|
||||
const answer = api.page(ask);
|
||||
c.movedOnce = c.moved;
|
||||
c.moved = 0;
|
||||
Object.assign(c, pageMerge(append ? c.items : [], ask, answer));
|
||||
};
|
||||
c.toEnd = () => { for (let i = 0; c.more && i < 100; i += 1) c.load(true); };
|
||||
c.uids = () => c.items.map((r) => r.uid);
|
||||
return c;
|
||||
}
|
||||
|
||||
/** Every uid once, and nothing the server lists left out. */
|
||||
function assertWhole(shown, listed) {
|
||||
assert.equal(new Set(shown).size, shown.length, "a role is shown twice");
|
||||
const missing = listed.filter((uid) => !shown.includes(uid));
|
||||
assert.deepEqual(missing, [], "a role is never shown");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ pageAsk
|
||||
|
||||
test("with nothing moved the next page starts where the last one ended", () => {
|
||||
assert.deepEqual(pageAsk(120, 0), { offset: 120, limit: PAGE });
|
||||
});
|
||||
|
||||
test("each change starts the page a row earlier and asks for a row more", () => {
|
||||
assert.deepEqual(pageAsk(120, 3), { offset: 117, limit: PAGE + 3 });
|
||||
// Never before the first row, and never more than the route allows.
|
||||
assert.deepEqual(pageAsk(2, 5), { offset: 0, limit: PAGE + 2 });
|
||||
assert.deepEqual(pageAsk(5000, 4000), { offset: 5000 - 940, limit: 1000 });
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------ pageMerge
|
||||
|
||||
test("a row already on screen is dropped and the offset counts what the server sent", () => {
|
||||
const items = [{ uid: "a" }, { uid: "b" }];
|
||||
const merged = pageMerge(items, { offset: 1, limit: 3 },
|
||||
{ items: [{ uid: "b" }, { uid: "c" }, { uid: "d" }], total: 10 });
|
||||
assert.deepEqual(merged.items.map((r) => r.uid), ["a", "b", "c", "d"]);
|
||||
assert.equal(merged.offset, 4);
|
||||
assert.equal(merged.more, true);
|
||||
});
|
||||
|
||||
test("the list ends at the total, or at a short page whatever the total says", () => {
|
||||
const at = (items, total) => pageMerge([], { offset: 0, limit: 2 }, { items, total });
|
||||
assert.equal(at([{ uid: "a" }, { uid: "b" }], 2).more, false);
|
||||
assert.equal(at([{ uid: "a" }], 5).more, false);
|
||||
assert.equal(at([{ uid: "a" }, { uid: "b" }], 5).more, true);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------ against the server
|
||||
|
||||
test("a page asked from the rows on screen skips a role after a pass (the bug)", () => {
|
||||
const api = server(200);
|
||||
let items = api.page({ offset: 0, limit: PAGE }).items;
|
||||
api.vote(items[2].uid, -1);
|
||||
while (items.length < api.page({ offset: 0, limit: 1 }).total) {
|
||||
items = items.concat(api.page({ offset: items.length, limit: PAGE }).items);
|
||||
}
|
||||
const uids = items.map((r) => r.uid);
|
||||
assert.notEqual(new Set(uids).size, uids.length);
|
||||
assert.ok(api.uids().some((uid) => !uids.includes(uid)));
|
||||
});
|
||||
|
||||
test("a pass on the first page skips nothing and shows nothing twice", () => {
|
||||
const api = server(200);
|
||||
const c = client(api);
|
||||
c.load();
|
||||
api.vote(c.items[2].uid, -1);
|
||||
c.moved += 1;
|
||||
c.toEnd();
|
||||
assertWhole(c.uids(), api.uids());
|
||||
assert.equal(c.items.length, 200);
|
||||
assert.equal(c.more, false);
|
||||
});
|
||||
|
||||
test("likes, passes and undos on several pages skip nothing", () => {
|
||||
const api = server(500);
|
||||
const c = client(api);
|
||||
c.load();
|
||||
let seed = 7;
|
||||
const pick = () => { seed = (seed * 48271) % 2147483647; return seed; };
|
||||
while (c.more) {
|
||||
for (let i = 0; i < 6; i += 1) {
|
||||
const uid = c.items[pick() % c.items.length].uid;
|
||||
api.vote(uid, [-1, 1, null][pick() % 3]);
|
||||
c.moved += 1;
|
||||
}
|
||||
c.load(true);
|
||||
}
|
||||
assertWhole(c.uids(), api.uids());
|
||||
assert.equal(c.items.length, 500);
|
||||
});
|
||||
|
||||
test("with Show read off each role opened costs no role", () => {
|
||||
const api = server(200, { includeRead: false });
|
||||
const c = client(api);
|
||||
c.load();
|
||||
for (const i of [0, 5, 30, 59]) { api.read(c.items[i].uid); c.moved += 1; }
|
||||
c.load(true);
|
||||
api.read(c.items[90].uid);
|
||||
c.moved += 1;
|
||||
c.toEnd();
|
||||
assertWhole(c.uids(), api.uids());
|
||||
// The read ones stay on screen; the server's list no longer has them.
|
||||
assert.equal(c.items.length, 200);
|
||||
assert.equal(api.uids().length, 195);
|
||||
assert.equal(c.more, false);
|
||||
});
|
||||
|
||||
test("under Judged, Unjudged a vote takes the role out and the next page makes up for it", () => {
|
||||
const api = server(150, { judged: "unjudged" });
|
||||
const c = client(api);
|
||||
c.load();
|
||||
for (const i of [1, 2, 3]) { api.vote(c.items[i].uid, 1); c.moved += 1; }
|
||||
c.toEnd();
|
||||
assertWhole(c.uids(), api.uids());
|
||||
assert.equal(c.items.length, 150);
|
||||
});
|
||||
|
||||
test("a change counts for the page after the next too, for a vote that lands late", () => {
|
||||
const api = server(200);
|
||||
const c = client(api);
|
||||
c.load();
|
||||
c.load(true);
|
||||
// Clicked before the third page was asked, landed after the server read it.
|
||||
const late = c.items[10].uid;
|
||||
c.moved += 1;
|
||||
c.load(true);
|
||||
api.vote(late, -1);
|
||||
c.toEnd();
|
||||
assertWhole(c.uids(), api.uids());
|
||||
});
|
||||
Reference in new issue
Block a user