Keep the news layer inside its day and its memory bounds

From review of the news layer:
- An event's other sides older than 24 hours (or undated) are no longer
  listed, and a report dated more than 15 minutes ahead is outside the
  window, as in the regional brief.
- Picture cards are one canvas per event in a bounded cache (24), repainted
  in place when the age changes, with each card's picture dropped with it;
  the age in the key had made a new canvas every minute.
- The feed list drops reports that leave the window and is capped at 600.
- "In view" is filtered from the reports the layer already holds; it reads
  the feed only with minor news on, once per refresh, never per pan.
- Infinite scroll restarts after a filter change or a failed read.
- The two filter selects stack, so "Significant and above" is not cut off.
- The timeout tests hold the event loop until aborted, so they pass on
  Node 22 as well.
This commit is contained in:
Denis Groshev committed 2026-11-06 00:45:25 +01:00
1 parent 138cc77a11
commit e95ae7b80c
11 files changed
+560 -90

No files matched your search

+9 -1
View File
@@ -13,7 +13,8 @@ const set = (node, key, value) => {
* The News rail panel (`#news-panel`): it renders the `feed` view the news
* layer publishes through its row controls, shows itself only while the
* layer is on, and acts only through setLayerParams. Near the bottom of its
* scroller it asks for the next older page, once per page.
* scroller it asks for the next older page, once per page; other filters or a
* failed read let it ask again.
*/
export function createNewsPanel({ container, setLayerParams = () => {} } = {}) {
const document = container?.ownerDocument;
@@ -25,6 +26,7 @@ export function createNewsPanel({ container, setLayerParams = () => {} } = {}) {
let view = null;
let destroyed = false;
let requestedAt = -1;
let shownFilters = '';
function render() {
if (destroyed) return;
@@ -59,6 +61,12 @@ export function createNewsPanel({ container, setLayerParams = () => {} } = {}) {
return {
/** Show the layer's feed view, or hide the panel with null. */
update(next) {
const filters = JSON.stringify(next?.filters ?? null);
// A new list (other filters or hotspot), or a read that failed, may stop
// at the same length as the last request: let the next scroll ask.
if (filters !== shownFilters || (next?.error && !view?.error))
requestedAt = -1;
shownFilters = filters;
view = next || null;
render();
},
+57 -2
View File
@@ -37,8 +37,20 @@ const big = report('big', {
status: 'both-sides',
lastAt: '2026-10-28T09:50:00.000Z',
sides: [
{ side: 'UA', title: 'a', url: 'https://a.test/', publisher: 'A' },
{ side: 'RU', title: 'b', url: 'https://b.test/', publisher: 'B' },
{
side: 'UA',
title: 'a',
url: 'https://a.test/',
publisher: 'A',
publishedAt: '2026-10-28T09:00:00.000Z',
},
{
side: 'RU',
title: 'b',
url: 'https://b.test/',
publisher: 'B',
publishedAt: '2026-10-28T09:40:00.000Z',
},
],
image: { url: 'https://a.test/p.jpg', publisher: 'A' },
},
@@ -221,3 +233,46 @@ test('filters, In view and empty or failed reads', () => {
);
news.destroy();
});
test('scrolling to the end asks once per page, again after a filter change or a failed read', () => {
const { news, calls } = fixture();
const body = document.getElementById('news-panel-body');
// A scroller at its bottom.
Object.defineProperty(body, 'scrollHeight', {
value: 1000,
configurable: true,
});
Object.defineProperty(body, 'clientHeight', {
value: 400,
configurable: true,
});
body.scrollTop = 600;
const loadMores = () => calls.filter(([, p]) => p.loadMore === true).length;
const scroll = () => body.dispatchEvent(new Event('scroll'));
news.update(view());
scroll();
scroll();
assert.equal(loadMores(), 1);
// The page came back with nothing new (as many items): no second request.
news.update(view());
scroll();
assert.equal(loadMores(), 1);
// Another hotspot, the same number of rows: the new list can be read on.
const hotspot = normalizeFilters({ hotspot: 'ua-ru-war' }, [
{ id: 'ua-ru-war' },
]);
news.update(view({ filters: hotspot }));
scroll();
assert.equal(loadMores(), 2);
// The read fails: the next scroll tries again.
news.update(view({ filters: hotspot, error: 'News feed unavailable' }));
scroll();
assert.equal(loadMores(), 3);
// While loading, or with no older page, nothing is asked.
news.update(view({ filters: hotspot, loading: true }));
scroll();
news.update(view({ filters: hotspot, hasMore: false }));
scroll();
assert.equal(loadMores(), 3);
news.destroy();
});
+3 -1
View File
@@ -121,9 +121,11 @@
flex-wrap: wrap;
gap: 4px;
}
/* One select per row: at the rail's width two side by side cut off
"Significant and above" (and longer translations). */
.news-readout__filters {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-columns: minmax(0, 1fr);
gap: 6px;
}
.news-filter {