diff --git a/app/static/roles.js b/app/static/roles.js index 669b8d7..c4a94ea 100644 --- a/app/static/roles.js +++ b/app/static/roles.js @@ -30,6 +30,9 @@ * and each company affinity. One is always picked while any is enabled: the * one in `?agent=`, else the one this browser viewed last (localStorage), * else the enabled one with the lowest id, which is the server's default too. + * The feed's first request carries the first two before the agents are in, + * and waits for them when there is neither, since the server gates nothing + * for a request that names no agent (`start`). * Picking another reloads the feed and the counts under the reader, which * stays open and is fetched again for that agent's vote. * @@ -449,12 +452,28 @@ let feedRequest = 0; + /* The feed has nothing to show, so the empty state offers a retry rather + than leaving the feed blank for good. */ + function feedFailed() { + state.feedError = true; + state.loaded = false; + renderFeed(); + } + /* A reload starts the list again; an append asks for the rows after the 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. */ + start with an offset into the list being replaced. + + Nothing is asked while no agent is named and the agents have not said + whether there is one, since the server would list every open role, + ungated (`start`); the empty state offers the retry instead. */ async function loadFeed({ append = false, keep = 0 } = {}) { + if (state.filter.agent === null && !state.agentsLoaded) { + if (!append) feedFailed(); + 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)) }; @@ -469,13 +488,8 @@ data = await api(`/api/roles?${params}`); } catch (err) { // An append failure leaves what is already on screen; a first load or - // a reload has nothing to show, so the empty state offers a retry - // rather than leaving the feed blank for good. - if (!append && request === feedRequest) { - state.feedError = true; - state.loaded = false; - renderFeed(); - } + // a reload has nothing to show (`feedFailed`). + if (!append && request === feedRequest) feedFailed(); return; } finally { if (request === feedRequest) state.loading = false; @@ -1394,8 +1408,10 @@ empty.innerHTML = `

Could not load the feed

The server did not answer. .

`; + // Without the agents the feed may not know whose it is, so they are + // asked again with it. const retry = empty.querySelector("[data-retry]"); - if (retry) retry.onclick = () => loadFeed(); + if (retry) retry.onclick = () => (state.agentsLoaded ? loadFeed() : start()); } else { const filtered = state.q || filtersOn(); empty.innerHTML = filtered @@ -2220,6 +2236,60 @@ }); } + /* Whose feed it is, then the feed. The server keeps to a fetch gate only + for an agent it is told of, and without one lists every open role, so + the first request carries the agent whenever one is known before the + agents are in: the address's, else the one this browser viewed last + (`storedAgent`). `chooseAgent` checks it against the enabled ones when + they land, and the feed reloads if it picks another. With neither, the + feed waits for the agents: with none enabled the list of every open + role is the right one, and when they cannot be had the feed's error + state offers to try again (`loadFeed`), rather than every open role + passing for the agent's list. + + The rest fills in when it arrives and the feed does not wait on it: the + companies, the tag counts, the saved filters. Each one drawn before is + greyed or missing, which is what "no agent" (renderAgent), an unjoined + company (paintRow) and the tag row being empty already mean, so nothing + here is a new state to handle. `keep` and `y` put the list back as it + was (`kept`). Run again by the error state's retry when the agents did + not come. */ + async function start({ keep = 0, y = null } = {}) { + if (state.filter.agent === null) state.filter.agent = storedAgent(); + const agentsIn = loadAgents().then(() => { chooseAgent(); return true; }, () => false); + const trackedIn = loadTracked().catch(() => {}); + // What the reader's fetch and then the feed's first request go out with, + // so the agents landing later know whether either is to be asked again. + const routed = state.filter.agent; + route(); + if (state.filter.agent === null) await agentsIn; + const asked = state.filter.agent; + const sortAsked = sortShown(); + const enhance = (async () => { + const [ready] = await Promise.all([agentsIn, trackedIn]); + if (!ready) return; + await loadCompanies(state.filter.agent).catch(() => {}); + renderRail(); + renderActive(); + loadFilters().catch(() => {}); + loadSaved().catch(() => {}); + // Reload for the score and votes when the agent chosen is not the one + // asked for (a remembered one disabled since), and either way bring + // the reader's affinity control and judge buttons up to date, since + // `companies` was still empty when it first painted. What fits me + // asked of an agent with no CV is What I'm looking for. + if (state.filter.agent !== asked || sortShown() !== sortAsked) reload(); + else renderFeed(); + if (state.readingUid) { + if (state.filter.agent !== routed) refreshReader(state.readingUid); + else renderReader(); + } + })(); + await loadFeed({ keep }); + if (y !== null && !state.readingUid) window.scrollTo(0, y); + await enhance; + } + document.addEventListener("DOMContentLoaded", async () => { readUrl(); const was = kept(); @@ -2230,44 +2300,6 @@ // Named before the first request, which a token the server does not // know would turn into a 422 rather than a list. await labelPlaces(); - - // The feed is the page; agents and companies are enhancements that fill - // in when they arrive, not something it waits on — a slow or failed - // /api/agents or /api/companies must not leave it blank. Each one drawn - // here is greyed or missing until `enhance` lands, which is what "no - // agent" (renderAgent), an unjoined company (paintRow) and the tag row - // being empty already mean, so nothing here is a new state to handle. - const before = state.filter.agent; - const sortBefore = sortShown(); - const enhance = (async () => { - try { - await Promise.all([loadTracked(), loadAgents()]); - chooseAgent(); - await loadCompanies(state.filter.agent); - } catch (err) { - return; - } - renderRail(); - renderActive(); - loadFilters().catch(() => {}); - loadSaved().catch(() => {}); - // The feed's first request went out with whatever agent `readUrl` had - // (often none): reload for the score and votes once the real one is - // known, and either way bring the reader's affinity control and judge - // buttons up to date, since `companies` was still empty when it first - // painted. - // What fits me asked of an agent with no CV is What I'm looking for. - if (state.filter.agent !== before || sortShown() !== sortBefore) reload(); - else renderFeed(); - if (state.readingUid) { - if (state.filter.agent !== before) refreshReader(state.readingUid); - else renderReader(); - } - })(); - - route(); - await loadFeed({ keep: was ? was.count : 0 }); - if (was && !state.readingUid) window.scrollTo(0, was.y || 0); - await enhance; + await start(was ? { keep: was.count, y: was.y || 0 } : {}); }); })();