diff --git a/src/layers/vessels/clockFollow.test.mjs b/src/layers/vessels/clockFollow.test.mjs index 84e652bf0..e208f6ef4 100644 --- a/src/layers/vessels/clockFollow.test.mjs +++ b/src/layers/vessels/clockFollow.test.mjs @@ -175,3 +175,44 @@ test('a vessel snapshot at a time the source drops for a newer one is no failure assert.equal(probe.feed.loading, false); assert.deepEqual(probe.applied, []); }); + +test('back at live, the live poll goes out at once even while a snapshot at a time was still loading', async () => { + const queries = []; + const probe = ingestion({ + getSnapshot(query, { signal }) { + queries.push(query); + if (query.atMs === undefined) + return Promise.resolve({ + records: [{ id: '111', latitude: 1, longitude: 2, observedAtMs: 5 }], + observedAtMs: 5, + freshness: 'current', + complete: true, + transportStatus: 'live', + }); + // A snapshot at a time answers only when dropped. + return new Promise((_, reject) => + signal.addEventListener('abort', () => { + const error = new Error('aborted'); + error.name = 'AbortError'; + reject(error); + }), + ); + }, + }); + probe.state.follows = true; + const timeline = new AbortController(); + const atTime = probe.loadAt(5_000, { + signal: timeline.signal, + replace: true, + }); + // The clock goes back to live: the timeline drops what loads and resumes + // the live poll in the same turn. + probe.state.follows = false; + timeline.abort(); + const live = probe.loadLivePositions({}); + await assert.rejects(atTime, { name: 'AbortError' }); + await live; + assert.equal(queries.length, 2); + assert.equal(Object.hasOwn(queries[1], 'atMs'), false); + assert.equal(probe.applied.at(-1).rows.length, 1); +}); diff --git a/src/layers/vessels/ingestion.js b/src/layers/vessels/ingestion.js index 008507d01..c5a2ca74c 100644 --- a/src/layers/vessels/ingestion.js +++ b/src/layers/vessels/ingestion.js @@ -27,9 +27,20 @@ export function createIngestion({ async function loadLivePositions(viewer) { // Following the world clock, the layer shows the snapshots loadAt applies. if (followsClock()) return; + // Back at live, a snapshot at a time still loading no longer applies: + // it would hold the live poll back until the next refresh. + if (feed.loading && feed.loadingAtTime) dropLoad(); await loadPositions(viewer); } + /** Let go of the load in flight, so another can start now. */ + function dropLoad() { + feed.abort?.abort(); + feed.loading = false; + feed.loadingAtTime = false; + feed.abort = null; + } + /** * Apply the source's snapshot at world time `atMs` (its timeline says it * can answer one), in place of every vessel when `replace`. Resolves @@ -41,11 +52,7 @@ export function createIngestion({ async function loadAt(atMs, { signal = null, replace = false } = {}) { // Whatever polls or loads for another time no longer applies: the // timeline asks for one time at a time. - if (feed.loading) { - feed.abort?.abort(); - feed.loading = false; - feed.abort = null; - } + if (feed.loading) dropLoad(); return loadPositions(readViewer(), { atMs, replace, signal }); } @@ -53,6 +60,7 @@ export function createIngestion({ const timed = atMs !== undefined; if (!viewer || feed.loading) return false; feed.loading = true; + feed.loadingAtTime = timed; feed.loadingLabel = feed.loaded ? 'refreshing...' : 'loading...'; const requestController = new AbortController(); const requestSessionId = feed.sessionId; @@ -123,6 +131,7 @@ export function createIngestion({ feed.sessionId === requestSessionId ) { feed.loading = false; + feed.loadingAtTime = false; feed.loadingLabel = feed.firstConnectPhase === 'loading' ? AIS_FIRST_CONNECT_LABEL : ''; feed.abort = null; @@ -267,6 +276,8 @@ export function createVesselFeed() { return { enabled: false, loading: false, + /** Whether the load in flight is a snapshot at a time, not a poll. */ + loadingAtTime: false, loaded: false, stale: false, partial: false,