feat(live): flights, military and vessels follow the world clock when their source declares a timeline

A live source may now declare a timeline: it can answer a snapshot at a
given time (source.timeline = { coverage(), stepMs, subscribe? }, read by
readSnapshotTimeline). While such a layer is on it follows the world clock
and registers the coverage. At the live edge it polls exactly as before;
away from it the layer asks for getSnapshot({ ...query, atMs }) at the step
at or before the clock time and shows that snapshot instead of polling.

src/time/snapshotTimeline.js owns the following: a seek, time going
backwards or leaving live replaces every contact with the snapshot at the
new time (play waits on it, for at most 8 s); playing on keeps one
snapshot ahead and gates play on it, applying those snapshots as polls so
contacts move smoothly; outside coverage the layer is empty; back at live
it is cleared and polls at once. One load at a time, and a load the
source drops for a newer one is not a failure.

While following, aircraft are drawn at the clock time with no render
delay, records and absences are judged at the clock, track requests carry
atMs, and vessels move between fixes along the great circle. The world
clock counts jumps (seeks and returns to live) in getState().

A source with no timeline never follows the clock, and every layer stays
live, unchanged.
This commit is contained in:
Sameh Khamis committed 2026-11-10 18:12:53 -08:00
1 parent 962f7293c6
commit 57ff81f723
49 files changed
+2185 -239

No files matched your search

+57 -6
View File
@@ -21,9 +21,36 @@ export function createIngestion({
settleFirstConnect,
now,
setSourceLabel,
followsClock = () => false,
reloadAtClock = () => {},
}) {
async function loadLivePositions(viewer) {
if (!viewer || feed.loading) return;
// Following the world clock, the layer shows the snapshots loadAt applies.
if (followsClock()) return;
await loadPositions(viewer);
}
/**
* Apply the source's snapshot at world time `atMs` (its timeline says it
* can answer one), in place of every vessel when `replace`. Resolves
* whether it was applied.
* @param {number} atMs
* @param {{signal?: AbortSignal|null, replace?: boolean}} [options]
* @returns {Promise<boolean>}
*/
async function loadAt(atMs, { signal = null, replace = false } = {}) {
// Whatever polls or loads for another time no longer applies.
if (replace && feed.loading) {
feed.abort?.abort();
feed.loading = false;
feed.abort = null;
}
return loadPositions(readViewer(), { atMs, replace, signal });
}
async function loadPositions(viewer, { atMs, replace = false, signal } = {}) {
const timed = atMs !== undefined;
if (!viewer || feed.loading) return false;
feed.loading = true;
feed.loadingLabel = feed.loaded ? 'refreshing...' : 'loading...';
const requestController = new AbortController();
@@ -33,21 +60,29 @@ export function createIngestion({
try {
// Combine the layer's teardown-abort with a hard timeout so a hung upstream
// can't wedge the poll indefinitely (parity with the track fetch + flights).
const signal =
const requestSignal =
typeof AbortSignal.any === 'function'
? AbortSignal.any([
requestController.signal,
AbortSignal.timeout(10000),
...(signal ? [signal] : []),
])
: requestController.signal;
// Ask for the vessels in view; a wide view asks for every vessel.
const area = readArea(viewer);
feed.lastArea = area;
const snapshot = await readSource().getSnapshot(
{ maxRows: getRowLimit(), ...(area ? { area } : {}) },
{ signal },
{
maxRows: getRowLimit(),
...(area ? { area } : {}),
...(timed ? { atMs } : {}),
},
{ signal: requestSignal },
);
if (!ownsAisRequest(requestController, requestSessionId)) return;
if (!ownsAisRequest(requestController, requestSessionId)) return false;
if (signal?.aborted) return false;
// The clock left the live edge while this poll was out.
if (!timed && followsClock()) return false;
setSourceLabel(snapshot.source);
// Map observations into the existing display store; source fields stop here.
applyAisFeedSnapshot(viewer, {
@@ -67,8 +102,12 @@ export function createIngestion({
: new Date(snapshot.observedAtMs).toISOString(),
silentForMs: snapshot.silentForMs,
reconnectAttempt: snapshot.reconnectAttempt,
...(replace && { replace }),
});
return true;
} catch (error) {
// A snapshot at a time the source dropped for a newer one is no failure.
if (timed && error?.name === 'AbortError') throw error;
if (
ownsAisRequest(requestController, requestSessionId) &&
error?.name !== 'AbortError'
@@ -76,6 +115,7 @@ export function createIngestion({
markUnavailable(error?.message || msg('AIS live load failed'));
console.warn('[Data:ais-live-vessels]', feed.error, error);
}
return false;
} finally {
if (
feed.abort === requestController &&
@@ -113,6 +153,9 @@ export function createIngestion({
feed.acceptedRowCount = snapshot.acceptedRowCount;
feed.partial = payload?.complete === false;
// At another time, a snapshot with no vessels shows none, not the last.
if (payload?.replace && snapshot.acceptedRowCount === 0)
applyRows(viewer, [], { complete: true, replace: true });
if (snapshot.acceptedRowCount === 0 && !snapshot.emptyCoverage) {
feed.count = readCount();
feed.stale = feed.count > 0 || Boolean(payload?.refreshing);
@@ -139,6 +182,7 @@ export function createIngestion({
settleFirstConnect('ready');
applyRows(viewer, snapshot.acceptedRows, {
complete: payload?.complete !== false,
...(payload?.replace && { replace: true }),
});
feed.count = readCount();
feed.stale =
@@ -180,10 +224,16 @@ export function createIngestion({
}
/** Ask again when the view moved away from the last area asked for. */
function refreshIfMoved(viewer) {
if (!feed.enabled || feed.loading) return Promise.resolve();
const following = followsClock();
if (!feed.enabled || (feed.loading && !following)) return Promise.resolve();
const active = viewer || readViewer();
if (!viewAreaMovedEnough(feed.lastArea ?? null, readArea(active)))
return Promise.resolve();
// Following the world clock, the new area is asked for at the same time.
if (following) {
reloadAtClock();
return Promise.resolve();
}
return loadLivePositions(active);
}
@@ -196,6 +246,7 @@ export function createIngestion({
return {
loadLivePositions,
loadAt,
refreshIfMoved,
ownsAisRequest,
applyAisFeedSnapshot,