diff --git a/web/deferredredraw.js b/web/deferredredraw.js deleted file mode 100644 index a00b1742..00000000 --- a/web/deferredredraw.js +++ /dev/null @@ -1,42 +0,0 @@ -// deferWhile(isBlocked) holds redraws back while something on screen would be -// destroyed by them, and runs them once that thing is gone. -// -// The case it exists for: a name resolves asynchronously and the map redraws to -// show it, but every redraw path starts by clearing its layer — and removing a -// marker closes its popup. A user who clicks a point right as it appears can -// watch the popup vanish under the cursor because a background lookup finished -// (#271). Only automatic redraws need holding; a redraw the user asked for -// (filter change, pan, layer toggle) closing a popup is expected. -// -// One held redraw PER KIND, not one in total. The point layer and each CS -// observer layer redraw independently, and the observer layers are redrawn -// only by explicit triggers — never by a pan or a filter change — so a -// coalesced-away observer redraw would leave raw hex ids on screen for the -// rest of the session. Within a kind the latest wins: several senders can -// resolve while one popup is open, and only the last redraw matters. -// -// flush() re-checks isBlocked and keeps holding if it is still true. Leaflet -// removes the previous popup before adding the next one, so popupclose fires -// while the next popup is already opening; flushing there would clear the -// layer out from under the arriving popup, which is this module's own bug one -// interaction later. -export function deferWhile(isBlocked) { - const held = new Map() - return { - // run(kind, fn) → true if it ran now, false if it was held for later. - run(kind, fn) { - if (isBlocked()) { held.set(kind, fn); return false } - fn() - return true - }, - // flush() → true if anything ran. Safe to call whenever the blocker may - // have cleared; it decides for itself whether it actually has. - flush() { - if (isBlocked() || held.size === 0) return false - const fns = [...held.values()] - held.clear() - for (const fn of fns) fn() - return true - }, - } -} diff --git a/web/deferredredraw.test.js b/web/deferredredraw.test.js deleted file mode 100644 index f70242d5..00000000 --- a/web/deferredredraw.test.js +++ /dev/null @@ -1,85 +0,0 @@ -import { describe, it, expect, vi } from 'vitest' -import { deferWhile } from './deferredredraw.js' - -describe('deferWhile — hold a redraw while something is open', () => { - it('runs immediately when nothing is blocking', () => { - const fn = vi.fn() - const d = deferWhile(() => false) - expect(d.run('points', fn)).toBe(true) - expect(fn).toHaveBeenCalledTimes(1) - }) - it('holds the redraw while blocked', () => { - const fn = vi.fn() - const d = deferWhile(() => true) - expect(d.run('points', fn)).toBe(false) - expect(fn).not.toHaveBeenCalled() - }) - it('runs the held redraw on flush', () => { - const fn = vi.fn() - let blocked = true - const d = deferWhile(() => blocked) - d.run('points', fn) - blocked = false - expect(d.flush()).toBe(true) - expect(fn).toHaveBeenCalledTimes(1) - }) - // Several senders can resolve while one popup is open; the map only needs - // the last redraw of that kind, not one per resolution. - it('coalesces within one kind: only the most recent held redraw runs', () => { - const first = vi.fn(), second = vi.fn() - let blocked = true - const d = deferWhile(() => blocked) - d.run('points', first) - d.run('points', second) - blocked = false - d.flush() - expect(first).not.toHaveBeenCalled() - expect(second).toHaveBeenCalledTimes(1) - }) - // ...but different kinds are different updates. Coalescing them would drop - // one silently: the CS advert and CS relay layers redraw independently, and - // neither is redrawn by pan or filter changes, so a dropped one stays stale - // for the rest of the session. - it('keeps one held redraw per kind', () => { - const advert = vi.fn(), relay = vi.fn(), points = vi.fn() - let blocked = true - const d = deferWhile(() => blocked) - d.run('cs:advert', advert) - d.run('cs:rxlog', relay) - d.run('points', points) - blocked = false - d.flush() - expect(advert).toHaveBeenCalledTimes(1) - expect(relay).toHaveBeenCalledTimes(1) - expect(points).toHaveBeenCalledTimes(1) - }) - // Leaflet removes the previous popup before adding the next one, so a - // popupclose fires while the next popup is already opening. Flushing there - // would clear the layer out from under the popup that is arriving — the very - // bug this module exists to prevent, one interaction later. - it('keeps holding when flush is called while still blocked', () => { - const fn = vi.fn() - let blocked = true - const d = deferWhile(() => blocked) - d.run('points', fn) - expect(d.flush()).toBe(false) - expect(fn).not.toHaveBeenCalled() - blocked = false - expect(d.flush()).toBe(true) - expect(fn).toHaveBeenCalledTimes(1) - }) - it('flush is a no-op when nothing was held', () => { - const d = deferWhile(() => false) - expect(d.flush()).toBe(false) - }) - it('does not run the same held redraw twice', () => { - const fn = vi.fn() - let blocked = true - const d = deferWhile(() => blocked) - d.run('points', fn) - blocked = false - d.flush() - d.flush() - expect(fn).toHaveBeenCalledTimes(1) - }) -}) diff --git a/web/e2e/ui.spec.js b/web/e2e/ui.spec.js index c33b681d..9f60d3db 100644 --- a/web/e2e/ui.spec.js +++ b/web/e2e/ui.spec.js @@ -496,54 +496,58 @@ test('the site is installable: manifest is linked and valid, no service worker ( expect(regs).toHaveLength(0) }) -// #271: every redraw path clears its layer, and removing a marker closes its -// popup — so a name resolving in the background used to shut a popup the user -// had just opened. The redraw is held until the popup closes. -test('an open popup survives a name-resolution redraw, and the redraw still happens after', async ({ page }) => { - await page.route('**/api/observer-points*', (route) => { - const src = new URL(route.request().url()).searchParams.get('src') - const pts = src === 'rxlog' - ? [{ lat: 51, lon: 4, rssi: -100, snr: -5, heard_key: '1d6f', src: 'rxlog', observer: 'Erwin Mobile', rx_at: '2026-06-30T15:00:00Z' }] - : [] - route.fulfill({ json: { points: pts } }) - }) +// #271 held name-resolution redraws while a popup was open, because on +// Leaflet removing a marker closed its popup. On MapLibre the popup is not tied +// to a feature and a redraw only replaces source data, so the hold went (#763). +// What is left to guard: the redraw runs while the popup is open, and the +// popup stays. +test('a name-resolution redraw runs under an open popup and leaves it open', async ({ page }) => { + const sender = 'ab'.repeat(32) + await page.route('**/api/points*', (r) => r.fulfill({ json: { points: [{ + lat: 51, lon: 4, rssi: -90, snr: -8, sender_id: sender, sender_kind: 'advert_pubkey', sender_label: '', + hunter_pubkey: 'h1', hunter_name: 'Hunter 1', channel_name: '', packet_type: 'Advert', hops: 0, + rx_at: new Date(Date.now() - 3600000).toISOString(), + }] } })) + // Map draws page /api/points by 5000; the ticker asks with its own limit. + // A range that has already ended is not live, so neither the range refresh + // nor the ticker poll runs: a map draw after the name lands is that redraw + // and no other. + let mapDraws = 0 + page.on('request', (r) => { if (r.url().includes('/api/points') && r.url().includes('limit=5000')) mapDraws++ }) // The resolver answers only when the test lets it (#761), so the popup is - // open and waiting when the name lands. A fixed delay raced everything the - // test does before the click, and lost under load. + // open before the name lands. let release const gate = new Promise((res) => { release = res }) await page.route('**/api/resolve*', async (r) => { await gate - r.fulfill({ json: { name: 'BE-HSS-DinX', ambiguous: false } }) + r.fulfill({ json: { name: 'Named Node', ambiguous: false } }) }) - await page.goto('/') - await setNodePos(page, 'positions') - await expect.poll(() => page.evaluate(() => window.__featureCount('observer-rxlog')), { timeout: 10000 }).toBe(1) + const local = (d) => new Date(d.getTime() - d.getTimezoneOffset() * 60000).toISOString().slice(0, 16) + const from = local(new Date(Date.now() - 86400000)), to = local(new Date(Date.now() - 120000)) + await page.goto(`/?mode=points&lat=51&lon=4&z=15&from=${from}&to=${to}`) + await expect.poll(() => page.evaluate(() => window.__featureCount('points')), { timeout: 10000 }).toBe(1) await mapSettled(page) - // Open the popup while the lookup is still in flight. Pinned as "not yet - // resolved", not merely "contains the id": the id line is present either - // way, so on a slow machine the redraw could already have happened and every - // later assertion would pass for the wrong reason. + const popup = page.locator('.maplibregl-popup:not(.ch-hover)') await expect(async () => { await clickMapAt(page, 51, 4) - await expect(page.locator('.maplibregl-popup-content')).toContainText('relay 1d6f', { timeout: 1000 }) + await expect(popup).toBeVisible({ timeout: 1000 }) }).toPass() - await expect(page.locator('.maplibregl-popup-content')).not.toContainText('BE-HSS-DinX') + await expect(popup).not.toContainText('Named Node') - // Let the name land. The popup is still there afterwards: this is the - // regression. - const answered = page.waitForResponse((r) => r.url().includes('/api/resolve')) + // The name lands and the layer redraws: a fresh map draw proves the redraw + // ran, rather than waiting a fixed time and hoping it did. + const before = mapDraws release() - await answered - await page.waitForTimeout(600) - await expect(page.locator('.maplibregl-popup-content')).toBeVisible() + await expect.poll(() => mapDraws, { timeout: 10000 }).toBeGreaterThan(before) + await expect(popup).toBeVisible() - // ...and the held redraw runs on close, so the name is not lost either. + // The name is there for the next popup (read from the resolver cache when + // it opens; the redraw is proved by the draw counter above). await page.locator('.maplibregl-popup-close-button').click() await expect(async () => { await clickMapAt(page, 51, 4) - await expect(page.locator('.maplibregl-popup-content')).toContainText('BE-HSS-DinX', { timeout: 1000 }) + await expect(popup).toContainText('Named Node', { timeout: 1000 }) }).toPass({ timeout: 10000 }) }) diff --git a/web/map.js b/web/map.js index 1d3aa263..79326407 100644 --- a/web/map.js +++ b/web/map.js @@ -21,7 +21,6 @@ import { unclutteredLabels, createLabelMeasurer, screenObstacles } from './nodel import { fetchPointsPaged } from './pagedpoints.js' import { windowKey, createWindowCache } from './windowpoints.js' import { latestWins } from './latestwins.js' -import { deferWhile } from './deferredredraw.js' import * as urlstate from './urlstate.js' import { startBarWatch, onBarChange } from './barwatch.js' import { initAuthBar } from './login.js' @@ -134,22 +133,6 @@ const csRelayLayer = 'observer-rxlog' // this covers what is drawn from the current state rather than a fetch. wm.onOverlaysReady(() => { refresh() }) -// A name-resolution redraw clears its layer, and removing a marker closes its -// popup — so a popup opened just before a background lookup finished would -// vanish under the cursor (#271). Hold those redraws while a popup is open and -// run the last one when it closes. Only automatic redraws go through this: a -// redraw the user asked for (filter, pan, layer toggle) closing a popup is -// expected behaviour. -let popupOpen = false -const nameRedraw = deferWhile(() => popupOpen) -wm.onPopup((open) => { - popupOpen = open - // Deferred a tick, and flush() re-checks: opening a popup from another - // closes the first one before the next is open, so a synchronous flush on - // close would clear the layer out from under it — this bug, one - // interaction later. - if (!open) setTimeout(() => nameRedraw.flush(), 0) -}) // Target-list picker (#223), created near the end of this file once its DOM // exists; refreshPickerCandidates() (called from refresh()) feeds it on every // redraw in all modes, guarded since it's still null during the handful of @@ -413,10 +396,11 @@ async function drawPoints() { const cover = { truncated: capped, coversFrom: oldestRxAt(points) } setStatus(coverageLabel(points.length, 'points', cover) + ignoreSuffix(), coverageTitle(POINTS_CAP, cover)) // Look up unknown full-pubkey senders once each; redraw if any resolved to a - // name — but not out from under an open popup (#271). + // name. An open popup stays open: since MapLibre (#763) a redraw replaces + // source data and never closes it. if (unresolved.size) { Promise.all([...unresolved].map((k) => resolveName(k))).then((names) => { - if (names.some((n) => n)) nameRedraw.run('points', () => refresh()) + if (names.some((n) => n)) refresh() }) } } @@ -773,7 +757,7 @@ function renderPickerRows(points) { targetPicker.render(points, Date.now()) if (!misses.size) return Promise.all([...misses].map((k) => resolveName(k))).then((names) => { - if (names.some((n) => n)) nameRedraw.run('picker', () => renderPickerRows(points)) + if (names.some((n) => n)) renderPickerRows(points) }) } @@ -1306,16 +1290,9 @@ async function drawObserverPoints(src, layer, ring) { if (unresolved.size) { Promise.all([...unresolved].map((k) => resolveName(k))).then((names) => { // Same guard as above: don't redraw for a layer that's been switched off - // (or gone into Locate focus) while the names were resolving. The guard - // moved inside the callback because the redraw can now be held until a - // popup closes (#271) — by then, either may have changed. + // (or gone into Locate focus) while the names were resolving. if (!names.some((n) => n)) return - // Keyed per source: the advert and relay layers redraw independently and - // are never redrawn by a pan or a filter change, so sharing one slot - // would drop one of them silently rather than defer it. - nameRedraw.run(`cs:${src}`, () => { - if (csOn.checked && !locateActive) drawObserverPoints(src, layer, ring) - }) + if (csOn.checked && !locateActive) drawObserverPoints(src, layer, ring) }) } } @@ -1427,12 +1404,11 @@ function nodePosPopup(name, id, p, est, { reachOn = false, selected = false, hea // trigger one). Without this the later pass clears the layer and both then add their // markers, leaving duplicates behind. let nodePosGen = 0 -// Signature of what is currently drawn. Rebuilding the layer destroys every -// marker, which silently closes any popup the user has open — and this layer -// redraws on each refresh (pan, zoom, filter change, the name-resolution -// pass), so an unguarded rebuild can yank a popup away mid-read. Skip the -// rebuild when the rendered content is identical, mirroring targetlist.js's -// _lastSig guard in the app. +// Signature of what is currently drawn. This layer redraws on each refresh +// (pan, zoom, filter change, the name-resolution pass), so the rebuild is +// skipped when the rendered content is identical, mirroring targetlist.js's +// _lastSig guard in the app. It no longer guards an open popup: on MapLibre a +// redraw leaves it open (#763). let nodePosSig = null // The repeater whose popup comes back after a selecting press or tap (#623). // A selection redraws the node layer, so the popup is reopened with the diff --git a/web/mapcore.js b/web/mapcore.js index 7ad95733..9a04be21 100644 --- a/web/mapcore.js +++ b/web/mapcore.js @@ -419,19 +419,17 @@ export function createWebMap(containerId, { center, zoom, theme = 'dark', mode = } // ---- popups ---- - // One open popup at a time, as on Leaflet: opening another closes it, and - // the 'open'/'close' events reach map.js so a name-resolution redraw can be - // held while a popup is under the cursor (#271). + // One open popup at a time, as on Leaflet: opening another closes it. It is + // not tied to a feature, so a redraw (setData) leaves it open (#763). let popup = null - const popupCbs = [] function closePopup() { if (popup) { const p = popup; popup = null; p.remove() } } function openPopup(lngLat, html, { closeButton = true, closeOnClick = true, className = '' } = {}) { closePopup() - popup = new maplibregl.Popup({ closeButton, closeOnClick, maxWidth: '280px', className }) + const p = new maplibregl.Popup({ closeButton, closeOnClick, maxWidth: '280px', className }) .setLngLat(lngLat).setHTML(html).addTo(map) - for (const cb of popupCbs) cb(true) - popup.on('close', () => { if (popup) { popup = null; for (const cb of popupCbs) cb(false) } }) - return popup + popup = p + p.on('close', () => { if (popup === p) popup = null }) + return p } // A hover line for the hex cells: MapLibre has no tooltip, so it is a small // closeless popup that follows the pointer and goes on mouseleave. @@ -552,7 +550,7 @@ export function createWebMap(containerId, { center, zoom, theme = 'dark', mode = onOverlaysReady(cb) { readyCbs.push(cb); if (overlaysReady) cb() }, isReady() { return overlaysReady }, setData, setHeat, syncSize, setHexSizes, - openPopup, closePopup, onPopup(cb) { popupCbs.push(cb) }, hoverText, onLayerClick, onEmptyClick, onGlyphTap, + openPopup, closePopup, hoverText, onLayerClick, onEmptyClick, onGlyphTap, // Test hook (#632): where a glyph paints, in page px, for a real click on it. glyphPagePoint(key, kind) { const src = kind === 'advert' ? NODE_GLYPH_SOURCE : NODE_DOT_SOURCE