mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
The main timeline is the only time control; the Weather panel shows its frame time
The Weather panel kept a second time transport of its own: a slider, step buttons, Latest and Play, backed by a replay in the weather clock that stepped the observed frames every two seconds and went round from the last to the first. The weather clock also attached itself to the world clock as a driver, so the timeline's Play became that replay whenever weather was on. The panel now shows the frame time only: the newest frame and its age while live, the timeline time alone at a past time. The weather clock no longer has setTarget, step, latest, play, pause or togglePlay, its state no longer has playing, and it no longer drives the world clock. It still follows the world time, registers each product's frames as marks the timeline's arrow keys step to, holds its products, and gates the world's continuous play at each next frame until it loads. weatherClock.world names the world clock it follows. A weather row's play, latest and step params move the world clock as the timeline does: play toggles continuous play (from the start of the covered time when live), latest goes live, and step goes to the previous or next mark. The rail timeline component, which only the panel used, is removed, along with its unused messages in every locale.
This commit is contained in:
1 parent
4ddce7f38b
commit
5150279ccf
35 files changed
+331
-1040
No files matched your search
+22
-146
@@ -59,12 +59,15 @@ export const PLAY_WAIT_MS = 8_000;
|
||||
// costs play nothing.
|
||||
|
||||
let clocks = 0;
|
||||
// How many weather clocks follow each world clock.
|
||||
const followers = new WeakMap();
|
||||
|
||||
/**
|
||||
* Transient observed time. Products must honor the signal before installing a
|
||||
* frame. Its time is the world clock's: latest while the world is live,
|
||||
* history at the world's time otherwise. Replay steps the observed frames and
|
||||
* moves the world clock as it goes, as the world clock's driver.
|
||||
* history at the world's time otherwise. It never moves the world clock
|
||||
* itself; the world's own controls (its timeline) move time, and while the
|
||||
* world plays it stops at each next frame until that frame loads.
|
||||
*/
|
||||
export function createWeatherClock({
|
||||
now = Date.now,
|
||||
@@ -76,17 +79,10 @@ export function createWeatherClock({
|
||||
const listeners = new Set();
|
||||
let mode = 'latest';
|
||||
let target = null;
|
||||
let playing = false;
|
||||
// The world time last applied; while this clock moves the world, the
|
||||
// transition its move started.
|
||||
// The world time last applied.
|
||||
let seen = 'latest';
|
||||
let moving = false;
|
||||
let moved = null;
|
||||
const releases = new Map();
|
||||
// Attached at the end, once the API exists.
|
||||
let driver = null;
|
||||
let generation = 0;
|
||||
let timer = null;
|
||||
let pending = null;
|
||||
// This clock's name on the world, for its holds and its gate.
|
||||
const holdId = `weather:${++clocks}`;
|
||||
@@ -147,10 +143,9 @@ export function createWeatherClock({
|
||||
const getState = () => ({
|
||||
mode,
|
||||
target,
|
||||
playing,
|
||||
// Frames come on one after another: this clock replays them, or the world
|
||||
// plays on through them, so the next is worth loading ahead.
|
||||
advancing: playing || worldPlaysOn(),
|
||||
// Frames come on one after another as the world plays on through them,
|
||||
// so the next is worth loading ahead.
|
||||
advancing: worldPlaysOn(),
|
||||
timeline: getTimeline(),
|
||||
products: [...registry.values()].map(({ product }) => ({
|
||||
id: product.id,
|
||||
@@ -162,60 +157,12 @@ export function createWeatherClock({
|
||||
const notify = () => {
|
||||
if (!destroyed) for (const listener of listeners) listener(getState());
|
||||
};
|
||||
const cancelTimer = () => {
|
||||
if (timer !== null) clearTimeout(timer);
|
||||
timer = null;
|
||||
};
|
||||
const canPlay = () => getTimeline().length >= 2;
|
||||
const stopPlaying = () => {
|
||||
if (!playing) return;
|
||||
playing = false;
|
||||
driver?.driven(false);
|
||||
};
|
||||
// Move the world clock; the change comes back through followWorld. Returns
|
||||
// the transition the move started, if it started one.
|
||||
const move = (fn) => {
|
||||
const outer = [moving, moved];
|
||||
moving = true;
|
||||
moved = null;
|
||||
try {
|
||||
fn();
|
||||
return moved;
|
||||
} finally {
|
||||
[moving, moved] = outer;
|
||||
}
|
||||
};
|
||||
const selections = () =>
|
||||
[...registry.values()].map(({ product }) => selectedFor(product)).join();
|
||||
let applied = null;
|
||||
function schedule() {
|
||||
cancelTimer();
|
||||
if (!playing || destroyed || pending) return;
|
||||
if (!canPlay()) {
|
||||
stopPlaying();
|
||||
notify();
|
||||
return;
|
||||
}
|
||||
const owner = generation;
|
||||
timer = setTimeout(() => {
|
||||
timer = null;
|
||||
if (destroyed || !playing || owner !== generation) return;
|
||||
const timeline = getTimeline();
|
||||
if (timeline.length < 2) {
|
||||
api.pause();
|
||||
return;
|
||||
}
|
||||
const next = timeline.find(
|
||||
(time) => Date.parse(time) > Date.parse(target),
|
||||
);
|
||||
void api.setTarget(next ?? timeline[0]);
|
||||
}, 2000);
|
||||
}
|
||||
function transition() {
|
||||
if (destroyed) return Promise.resolve(false);
|
||||
cancelTimer();
|
||||
const owner = ++generation;
|
||||
if (!canPlay()) stopPlaying();
|
||||
// A scrub drops what loads and moves on; play goes on as this loads.
|
||||
applied = selections();
|
||||
deferred = false;
|
||||
@@ -235,15 +182,12 @@ export function createWeatherClock({
|
||||
if (destroyed || owner !== generation) return false;
|
||||
stopWaiting();
|
||||
pending = null;
|
||||
if (!canPlay()) stopPlaying();
|
||||
notify();
|
||||
schedule();
|
||||
// Play held at the next frame for this one goes on to it.
|
||||
if (deferred) void transition();
|
||||
return true;
|
||||
});
|
||||
pending = result;
|
||||
if (moving) moved ??= result;
|
||||
notify();
|
||||
return result;
|
||||
}
|
||||
@@ -300,81 +244,17 @@ export function createWeatherClock({
|
||||
* wind, gives that layer the timeline time rather than the live edge.
|
||||
*/
|
||||
follows: (id) => world.follows(id),
|
||||
setTarget(time) {
|
||||
if (destroyed) return Promise.resolve(false);
|
||||
if (!Number.isFinite(Date.parse(time)))
|
||||
throw new TypeError('Weather target must be a UTC time');
|
||||
const normalized = new Date(time).toISOString();
|
||||
if (mode === 'history' && target === normalized)
|
||||
return pending ?? Promise.resolve(true);
|
||||
return (
|
||||
move(() => world.seek(Date.parse(normalized))) ??
|
||||
pending ??
|
||||
Promise.resolve(true)
|
||||
);
|
||||
},
|
||||
step(direction) {
|
||||
if (destroyed || ![-1, 1].includes(direction))
|
||||
return Promise.resolve(false);
|
||||
api.pause();
|
||||
const timeline = getTimeline();
|
||||
if (!timeline.length) return Promise.resolve(false);
|
||||
const at = Date.parse(target ?? timeline.at(-1));
|
||||
const next =
|
||||
direction < 0
|
||||
? (timeline.findLast((time) => Date.parse(time) < at) ?? timeline[0])
|
||||
: (timeline.find((time) => Date.parse(time) > at) ?? timeline.at(-1));
|
||||
return api.setTarget(next);
|
||||
},
|
||||
latest() {
|
||||
if (destroyed) return Promise.resolve(false);
|
||||
stopPlaying();
|
||||
if (mode === 'latest') return transition();
|
||||
return move(() => world.goLive()) ?? pending ?? Promise.resolve(true);
|
||||
},
|
||||
play() {
|
||||
if (destroyed || playing || !canPlay())
|
||||
return pending ?? Promise.resolve(false);
|
||||
playing = true;
|
||||
if (mode === 'latest') {
|
||||
const timeline = getTimeline();
|
||||
const start =
|
||||
timeline.findLast((time) => Date.parse(time) <= now()) ?? timeline[0];
|
||||
// Playing and at the first frame in one move of the world.
|
||||
return (
|
||||
move(() => driver.driven(true, Date.parse(start))) ??
|
||||
pending ??
|
||||
Promise.resolve(true)
|
||||
);
|
||||
}
|
||||
driver.driven(true);
|
||||
notify();
|
||||
schedule();
|
||||
return pending ?? Promise.resolve(true);
|
||||
},
|
||||
pause() {
|
||||
cancelTimer();
|
||||
if (!playing) {
|
||||
// The world may be playing on its own; a pause or step here holds it.
|
||||
if (world.isPlaying()) world.pause();
|
||||
return;
|
||||
}
|
||||
stopPlaying();
|
||||
notify();
|
||||
},
|
||||
togglePlay() {
|
||||
return playing ? api.pause() : api.play();
|
||||
},
|
||||
/**
|
||||
* The world clock this clock follows. Time moves there, through the same
|
||||
* seek, play and live the world's timeline uses, never through this clock.
|
||||
*/
|
||||
world,
|
||||
refresh() {
|
||||
if (destroyed) return Promise.resolve(false);
|
||||
// New frames, or products added or dropped, and with them the next.
|
||||
world.coverageChanged();
|
||||
gated = undefined;
|
||||
gateNext(world.getState());
|
||||
if (!canPlay()) {
|
||||
stopPlaying();
|
||||
cancelTimer();
|
||||
}
|
||||
if (mode === 'history') return transition();
|
||||
notify();
|
||||
return Promise.resolve(true);
|
||||
@@ -386,15 +266,15 @@ export function createWeatherClock({
|
||||
},
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
stopPlaying();
|
||||
destroyed = true;
|
||||
++generation;
|
||||
stopWaiting();
|
||||
world.gate(holdId, null);
|
||||
cancelTimer();
|
||||
unfollowWorld();
|
||||
// The last weather clock leaves the world live, as a new one expects.
|
||||
if (driver.detach() === 0) world.goLive();
|
||||
const left = followers.get(world) - 1;
|
||||
followers.set(world, left);
|
||||
if (left === 0) world.goLive();
|
||||
for (const release of releases.values()) release();
|
||||
releases.clear();
|
||||
for (const entry of registry.values()) entry.controller?.abort();
|
||||
@@ -403,9 +283,8 @@ export function createWeatherClock({
|
||||
pending = null;
|
||||
},
|
||||
};
|
||||
// Follow the world clock. A move of this clock's own always applies; one
|
||||
// from elsewhere (a host's seek, continuous play) applies only when it
|
||||
// changes the mode or a product's frame.
|
||||
// Follow the world clock. A move (a seek, play going on) applies only when
|
||||
// it changes the mode or a product's frame.
|
||||
function followWorld(state) {
|
||||
if (destroyed) return;
|
||||
gateNext(state);
|
||||
@@ -426,7 +305,7 @@ export function createWeatherClock({
|
||||
const modeChanged = nextMode !== mode;
|
||||
mode = nextMode;
|
||||
target = nextTarget;
|
||||
if (!moving && !modeChanged && selections() === applied) {
|
||||
if (!modeChanged && selections() === applied) {
|
||||
// Back at the frame that loads, play need not hold for it.
|
||||
if (deferred) {
|
||||
deferred = false;
|
||||
@@ -439,7 +318,7 @@ export function createWeatherClock({
|
||||
}
|
||||
// Playing on into the next frame while the one before it loads, the
|
||||
// world holds here until that one shows.
|
||||
if (!moving && !modeChanged && pending && playsOn) {
|
||||
if (!modeChanged && pending && playsOn) {
|
||||
deferred = true;
|
||||
startWaiting();
|
||||
notify();
|
||||
@@ -462,10 +341,7 @@ export function createWeatherClock({
|
||||
world.gate(holdId, next);
|
||||
}
|
||||
const unfollowWorld = world.subscribe(followWorld);
|
||||
driver = world.attachDriver({
|
||||
play: () => (canPlay() ? void api.play() : false),
|
||||
pause: () => api.pause(),
|
||||
});
|
||||
followers.set(world, (followers.get(world) ?? 0) + 1);
|
||||
followWorld(world.getState());
|
||||
return api;
|
||||
}
|
||||
@@ -158,13 +158,16 @@ test('a product’s gap is at least one and a half of its feed’s frame spacing
|
||||
assert.equal(clock.selectFor('clouds', time(211)), null);
|
||||
});
|
||||
|
||||
test('history applies per-product selections, including null; latest restores unequal newest times', async (t) => {
|
||||
const { clock } = harness(t);
|
||||
const at = (minutes) => Date.parse(time(minutes));
|
||||
|
||||
test('history applies per-product selections, including null; live restores unequal newest times', async (t) => {
|
||||
const { clock, world } = harness(t);
|
||||
const radar = product('radar', [0, 10, 20]);
|
||||
const satellite = product('satellite', [5, 15]);
|
||||
const lightning = product('lightning', [15]);
|
||||
for (const p of [radar, satellite, lightning]) clock.register(p);
|
||||
await clock.setTarget(time(10));
|
||||
world.seek(at(10));
|
||||
await flush();
|
||||
assert.deepEqual(
|
||||
[radar.shown, satellite.shown, lightning.shown],
|
||||
[time(10), time(5), null],
|
||||
@@ -173,18 +176,19 @@ test('history applies per-product selections, including null; latest restores un
|
||||
clock.getState().products.map(({ selected }) => selected),
|
||||
[time(10), time(5), null],
|
||||
);
|
||||
await clock.latest();
|
||||
world.goLive();
|
||||
await flush();
|
||||
assert.deepEqual(
|
||||
[radar.shown, satellite.shown, lightning.shown],
|
||||
[time(20), time(15), time(15)],
|
||||
);
|
||||
assert.equal(clock.getState().mode, 'latest');
|
||||
assert.equal(clock.getState().target, null);
|
||||
assert.equal(clock.getState().playing, false);
|
||||
assert.equal(clock.getState().advancing, false);
|
||||
});
|
||||
|
||||
test('superseded completions cannot commit, notify or schedule a new playback advance', async (t) => {
|
||||
const { clock, timers } = harness(t);
|
||||
test('superseded completions cannot commit or notify', async (t) => {
|
||||
const { clock, world, timers } = harness(t);
|
||||
const p = product('radar', [0, 10, 20]);
|
||||
const loads = [];
|
||||
p.apply = (selected, { signal }) =>
|
||||
@@ -201,113 +205,44 @@ test('superseded completions cannot commit, notify or schedule a new playback ad
|
||||
clock.register(p);
|
||||
let notifications = 0;
|
||||
clock.subscribe(() => notifications++);
|
||||
const old = clock.setTarget(time(0));
|
||||
world.seek(at(0));
|
||||
await flush();
|
||||
const current = clock.setTarget(time(10));
|
||||
world.seek(at(10));
|
||||
await flush();
|
||||
assert.equal(loads[0].signal.aborted, true);
|
||||
loads[1].finish();
|
||||
assert.equal(await current, true);
|
||||
await flush();
|
||||
const count = notifications;
|
||||
loads[0].finish();
|
||||
assert.equal(await old, false);
|
||||
await flush();
|
||||
assert.equal(notifications, count);
|
||||
assert.equal(p.shown, time(10));
|
||||
assert.equal(clock.getState().target, time(10));
|
||||
assert.equal(timers.size, 0);
|
||||
});
|
||||
|
||||
test('steps use strict union neighbours from arbitrary targets and clamp at endpoints', async (t) => {
|
||||
const { clock } = harness(t);
|
||||
clock.register(product('radar', [0, 10, 20]));
|
||||
clock.register(product('satellite', [5, 15]));
|
||||
await clock.step(-1);
|
||||
assert.equal(clock.getState().target, time(15));
|
||||
await clock.setTarget(time(12));
|
||||
await clock.step(-1);
|
||||
assert.equal(clock.getState().target, time(10));
|
||||
await clock.setTarget(time(12));
|
||||
await clock.step(1);
|
||||
assert.equal(clock.getState().target, time(15));
|
||||
await clock.setTarget(time(0));
|
||||
await clock.step(-1);
|
||||
assert.equal(clock.getState().target, time(0));
|
||||
await clock.setTarget(time(20));
|
||||
await clock.step(1);
|
||||
assert.equal(clock.getState().target, time(20));
|
||||
});
|
||||
|
||||
test('playback waits for every product to settle, including rejection, then waits 2000 ms and wraps', async (t) => {
|
||||
const { clock, tick, timers } = harness(t);
|
||||
const a = product('radar', [0, 10]);
|
||||
const b = product('satellite', [5]);
|
||||
const loads = [];
|
||||
b.apply = () =>
|
||||
new Promise((resolve, reject) => loads.push({ resolve, reject }));
|
||||
clock.register(a);
|
||||
clock.register(b);
|
||||
const start = clock.play();
|
||||
await flush();
|
||||
assert.equal(clock.getState().target, time(10));
|
||||
tick(10_000);
|
||||
assert.equal(timers.size, 0);
|
||||
assert.equal(a.calls.length, 1);
|
||||
loads[0].reject(new Error('unavailable'));
|
||||
await start;
|
||||
assert.equal(timers.size, 1);
|
||||
tick(1999);
|
||||
assert.equal(clock.getState().target, time(10));
|
||||
tick(1);
|
||||
await flush();
|
||||
assert.equal(clock.getState().target, time(0));
|
||||
assert.equal(timers.size, 0);
|
||||
tick(10_000);
|
||||
assert.equal(clock.getState().target, time(0));
|
||||
loads[1].resolve(true);
|
||||
await flush();
|
||||
tick(2000);
|
||||
await flush();
|
||||
assert.equal(clock.getState().target, time(5));
|
||||
clock.pause();
|
||||
loads[2].resolve(false);
|
||||
await flush();
|
||||
assert.equal(timers.size, 0, 'pausing during a load prevents rescheduling');
|
||||
});
|
||||
|
||||
test('pause cancels a pending advance; playback stops with one time or all products suspended', async (t) => {
|
||||
const { clock, tick, timers } = harness(t);
|
||||
const p = product('radar', [0, 10]);
|
||||
clock.register(p);
|
||||
await clock.play();
|
||||
await clock.play();
|
||||
assert.equal(timers.size, 1);
|
||||
clock.pause();
|
||||
tick(2000);
|
||||
assert.equal(clock.getState().target, time(10));
|
||||
assert.equal(timers.size, 0);
|
||||
await clock.togglePlay();
|
||||
p.times = [time(0)];
|
||||
await clock.refresh();
|
||||
assert.equal(clock.getState().playing, false);
|
||||
await clock.play();
|
||||
assert.equal(clock.getState().playing, false);
|
||||
p.times = [time(0), time(10)];
|
||||
await clock.refresh();
|
||||
await clock.play();
|
||||
p.suspended = true;
|
||||
await clock.refresh();
|
||||
assert.equal(clock.getState().playing, false);
|
||||
assert.deepEqual(clock.getTimeline(), []);
|
||||
assert.equal(timers.size, 0);
|
||||
test('the clock has no time controls of its own; the world clock moves time', (t) => {
|
||||
const { clock, world } = harness(t);
|
||||
for (const name of [
|
||||
'setTarget',
|
||||
'step',
|
||||
'latest',
|
||||
'play',
|
||||
'pause',
|
||||
'togglePlay',
|
||||
])
|
||||
assert.equal(clock[name], undefined, name);
|
||||
assert.equal(clock.world, world);
|
||||
});
|
||||
|
||||
test('refresh does not apply in latest; history retains its target as expired frames disappear', async (t) => {
|
||||
const { clock } = harness(t);
|
||||
const { clock, world } = harness(t);
|
||||
const p = product('radar', [0, 10]);
|
||||
clock.register(p);
|
||||
await clock.refresh();
|
||||
assert.equal(p.calls.length, 0);
|
||||
await clock.setTarget(time(10));
|
||||
world.seek(at(10));
|
||||
await flush();
|
||||
p.times = [time(0), time(20)];
|
||||
await clock.refresh();
|
||||
assert.equal(p.shown, time(0));
|
||||
@@ -318,15 +253,16 @@ test('refresh does not apply in latest; history retains its target as expired fr
|
||||
});
|
||||
|
||||
test('registration joins history; unregister and destroy abort work and release subscriptions/timers', async (t) => {
|
||||
const { clock, timers, tick } = harness(t);
|
||||
const { clock, world, timers } = harness(t);
|
||||
const p = product('radar', [0, 10]);
|
||||
const unregister = clock.register(p);
|
||||
await clock.setTarget(time(0));
|
||||
world.seek(at(0));
|
||||
await flush();
|
||||
const b = product('satellite', [0, 5]);
|
||||
clock.register(b);
|
||||
await flush();
|
||||
assert.equal(b.shown, time(0));
|
||||
await clock.play();
|
||||
world.seek(at(5));
|
||||
unregister();
|
||||
unregister();
|
||||
await flush();
|
||||
@@ -337,31 +273,27 @@ test('registration joins history; unregister and destroy abort work and release
|
||||
);
|
||||
let notifications = 0;
|
||||
const off = clock.subscribe(() => notifications++);
|
||||
clock.pause();
|
||||
off();
|
||||
const before = notifications;
|
||||
await clock.play();
|
||||
assert.equal(notifications, before);
|
||||
world.seek(at(0));
|
||||
await flush();
|
||||
assert.equal(notifications, 0);
|
||||
world.seek(at(5));
|
||||
clock.destroy();
|
||||
clock.destroy();
|
||||
tick(2000);
|
||||
await flush();
|
||||
assert.equal(timers.size, 0);
|
||||
assert.equal(b.calls.at(-1).signal.aborted, true);
|
||||
assert.equal(clock.getState().playing, false);
|
||||
assert.deepEqual(clock.getTimeline(), []);
|
||||
assert.equal(await clock.setTarget(time(5)), false);
|
||||
});
|
||||
|
||||
test('the weather time is the world time, both ways', async (t) => {
|
||||
test('the weather time is the world time', async (t) => {
|
||||
const { clock, world } = harness(t);
|
||||
const radar = product('radar', [0, 10, 20]);
|
||||
clock.register(radar);
|
||||
assert.equal(world.follows('radar'), true);
|
||||
await clock.setTarget(time(10));
|
||||
assert.equal(world.isLive(), false);
|
||||
assert.equal(world.now(), Date.parse(time(10)));
|
||||
world.seek(Date.parse(time(3)));
|
||||
world.seek(at(3));
|
||||
await flush();
|
||||
assert.equal(clock.getState().mode, 'history');
|
||||
assert.equal(clock.getState().target, time(3));
|
||||
assert.equal(radar.shown, time(0));
|
||||
world.goLive();
|
||||
@@ -370,30 +302,22 @@ test('the weather time is the world time, both ways', async (t) => {
|
||||
assert.equal(radar.shown, time(20));
|
||||
});
|
||||
|
||||
test('world play and pause drive the weather replay', async (t) => {
|
||||
test('world play runs the world on its own at its rate; weather does not drive it', async (t) => {
|
||||
const { clock, world, tick } = harness(t);
|
||||
const radar = product('radar', [0, 10, 20]);
|
||||
clock.register(radar);
|
||||
world.seek(Date.parse(time(0)));
|
||||
world.seek(at(0));
|
||||
await flush();
|
||||
world.play();
|
||||
assert.equal(clock.getState().playing, true);
|
||||
assert.equal(world.isPlaying(), true);
|
||||
tick(2000);
|
||||
world.play({ rate: 60 });
|
||||
assert.equal(world.getState().stepping, false);
|
||||
assert.equal(clock.getState().advancing, true);
|
||||
tick(5_000);
|
||||
assert.equal(world.now(), at(5), 'five minutes on at a minute a second');
|
||||
tick(10_000);
|
||||
await flush();
|
||||
assert.equal(radar.shown, time(10));
|
||||
assert.equal(world.now(), Date.parse(time(10)));
|
||||
assert.equal(world.now(), at(10), 'held at the next frame');
|
||||
world.pause();
|
||||
assert.equal(clock.getState().playing, false);
|
||||
clock.play();
|
||||
assert.equal(world.isPlaying(), true);
|
||||
clock.pause();
|
||||
assert.equal(world.isPlaying(), false);
|
||||
clock.play();
|
||||
world.goLive();
|
||||
await flush();
|
||||
assert.equal(clock.getState().playing, false);
|
||||
assert.equal(clock.getState().mode, 'latest');
|
||||
assert.equal(clock.getState().advancing, false);
|
||||
});
|
||||
|
||||
test('a moving world re-applies frames only when a product changes frame', async (t) => {
|
||||
@@ -416,15 +340,6 @@ test('a moving world re-applies frames only when a product changes frame', async
|
||||
assert.equal(radar.shown, time(10));
|
||||
});
|
||||
|
||||
test('a world without frames to step plays on its own', async (t) => {
|
||||
const { clock, world, tick } = harness(t);
|
||||
world.seek(Date.parse(time(0)));
|
||||
world.play();
|
||||
assert.equal(clock.getState().playing, false);
|
||||
tick(1000);
|
||||
assert.equal(world.now(), Date.parse(time(0)) + 1000);
|
||||
});
|
||||
|
||||
function worldAt(at) {
|
||||
return createWorldClock({
|
||||
now: () => at,
|
||||
@@ -434,73 +349,24 @@ function worldAt(at) {
|
||||
});
|
||||
}
|
||||
|
||||
test('weather clocks sharing a world hand play over, and the last leaves it live', async () => {
|
||||
test('weather clocks sharing a world: the last one destroyed leaves it live', () => {
|
||||
const world = worldAt(Date.parse(time(20)));
|
||||
const a = createWeatherClock({ world, now: () => Date.parse(time(20)) });
|
||||
const b = createWeatherClock({ world, now: () => Date.parse(time(20)) });
|
||||
const radarA = product('radar', [0, 10, 20]);
|
||||
const radarB = product('radar', [0, 10, 20]);
|
||||
a.register(radarA);
|
||||
b.register(radarB);
|
||||
await a.play();
|
||||
assert.equal(world.isPlaying(), true);
|
||||
world.pause();
|
||||
assert.equal(a.getState().playing, false);
|
||||
assert.equal(world.isPlaying(), false);
|
||||
await a.play();
|
||||
world.goLive();
|
||||
assert.equal(a.getState().playing, false);
|
||||
b.destroy();
|
||||
assert.equal(world.follows('radar'), true);
|
||||
a.register(product('radar', [0, 10, 20]));
|
||||
b.register(product('radar', [0, 10, 20]));
|
||||
world.seek(Date.parse(time(0)));
|
||||
world.play();
|
||||
assert.equal(a.getState().playing, true);
|
||||
b.destroy();
|
||||
assert.equal(world.follows('radar'), true);
|
||||
assert.equal(world.isLive(), false);
|
||||
assert.equal(world.isPlaying(), true);
|
||||
a.destroy();
|
||||
assert.equal(world.follows('radar'), false);
|
||||
assert.equal(world.isLive(), true);
|
||||
assert.equal(world.isPlaying(), false);
|
||||
});
|
||||
|
||||
test('pausing from inside the first playing notice stops the world too', async (t) => {
|
||||
const { clock, world } = harness(t);
|
||||
clock.register(product('radar', [0, 10, 20]));
|
||||
const off = clock.subscribe((state) => {
|
||||
if (state.playing) {
|
||||
off();
|
||||
clock.pause();
|
||||
}
|
||||
});
|
||||
await clock.play();
|
||||
assert.equal(clock.getState().playing, false);
|
||||
assert.equal(world.isPlaying(), false);
|
||||
});
|
||||
|
||||
test('a step holds a world that was playing on its own', async (t) => {
|
||||
const { clock, world } = harness(t);
|
||||
world.seek(Date.parse(time(0)));
|
||||
world.play();
|
||||
clock.register(product('radar', [0, 10, 20]));
|
||||
await clock.step(1);
|
||||
assert.equal(world.isPlaying(), false);
|
||||
assert.equal(world.now(), Date.parse(time(10)));
|
||||
});
|
||||
|
||||
test('a target superseded from a listener resolves false', async (t) => {
|
||||
const { clock } = harness(t);
|
||||
const radar = product('radar', [0, 10, 20]);
|
||||
clock.register(radar);
|
||||
await flush();
|
||||
let redirected = false;
|
||||
clock.subscribe((state) => {
|
||||
if (state.target === time(0) && !redirected) {
|
||||
redirected = true;
|
||||
void clock.setTarget(time(10));
|
||||
}
|
||||
});
|
||||
assert.equal(await clock.setTarget(time(0)), false);
|
||||
assert.equal(radar.shown, time(10));
|
||||
});
|
||||
|
||||
test('each product’s frames are world clock coverage, and support the timeline while shown', (t) => {
|
||||
const { clock, world } = harness(t);
|
||||
assert.equal(world.coverage(), null);
|
||||
|
||||
@@ -60,6 +60,37 @@ const utc = (value) =>
|
||||
? `${formatUtcDayMonth(value)} ${value.slice(11, 16)} UTC`
|
||||
: t('Unavailable');
|
||||
|
||||
/**
|
||||
* Move `world`, the world clock, as its timeline does: `play` toggles play
|
||||
* (from the start of the covered time when live), `latest` goes live, and
|
||||
* `step` (-1 or 1) goes to the previous or next mark, a weather frame among
|
||||
* them, as the timeline's arrow keys do.
|
||||
*/
|
||||
function moveWorldTime(world, { play, latest, step }) {
|
||||
if (!world) return;
|
||||
if (play === true) {
|
||||
const state = world.getState();
|
||||
if (state.playing) world.pause();
|
||||
else {
|
||||
const covered = world.coverage();
|
||||
if (state.live && covered) world.seek(covered.start);
|
||||
world.play({ continuous: true });
|
||||
}
|
||||
}
|
||||
if (latest === true) world.goLive();
|
||||
if (step === -1 || step === 1) {
|
||||
const at = world.getState().timeMs;
|
||||
const marks = world.coverage()?.marks ?? [];
|
||||
const to =
|
||||
step < 0
|
||||
? marks.findLast((mark) => mark < at)
|
||||
: marks.find((mark) => mark > at);
|
||||
if (to === undefined) return;
|
||||
world.pause();
|
||||
world.seek(to);
|
||||
}
|
||||
}
|
||||
|
||||
/** A per-application observation layer, using the existing layer lifecycle and
|
||||
* row controls. Its time is the world clock's, so a link made away from live
|
||||
* opens its imagery at that time; one made live opens the latest. */
|
||||
@@ -251,7 +282,10 @@ export function createWeatherLayer({
|
||||
)
|
||||
return;
|
||||
const times = manifest?.times ?? [];
|
||||
const next = times[(times.indexOf(time) + 1) % times.length];
|
||||
// The world plays on past the newest frame; this layer's own replay
|
||||
// (without a clock) goes round to the first.
|
||||
const after = times.indexOf(time) + 1;
|
||||
const next = clock ? times[after] : times[after % times.length];
|
||||
if (!next || next === time) return;
|
||||
try {
|
||||
await rendering.prefetch?.(manifest, next, { infrared });
|
||||
@@ -528,11 +562,9 @@ export function createWeatherLayer({
|
||||
);
|
||||
});
|
||||
}
|
||||
if (clock && enabled) {
|
||||
if (params.play === true) void clock.togglePlay();
|
||||
if (params.latest === true) void clock.latest();
|
||||
if ([-1, 1].includes(params.step)) void clock.step(params.step);
|
||||
}
|
||||
// With a clock, time is the world's: these move the world clock as its
|
||||
// timeline does, and every layer that follows it moves along.
|
||||
if (clock && enabled) moveWorldTime(clock.world, params);
|
||||
if (
|
||||
!clock &&
|
||||
params.play === true &&
|
||||
@@ -892,14 +924,14 @@ export function createWeatherLayer({
|
||||
const shared = clock?.getState();
|
||||
return {
|
||||
...rendering?.getDiagnostics(),
|
||||
playing: shared ? shared.playing : playing && !hostHidden,
|
||||
playing: shared ? shared.advancing : playing && !hostHidden,
|
||||
followLatest: isLatest(),
|
||||
...(shared
|
||||
? {
|
||||
clock: {
|
||||
mode: shared.mode,
|
||||
target: shared.target,
|
||||
playing: shared.playing,
|
||||
playing: shared.advancing,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
|
||||
@@ -41,6 +41,16 @@ const deferred = () => {
|
||||
const flush = async () => {
|
||||
for (let i = 0; i < 30; i++) await Promise.resolve();
|
||||
};
|
||||
// Time moves on the world clock the weather clock follows, as the timeline
|
||||
// moves it.
|
||||
const seekTo = (clock, time) => {
|
||||
clock.world.seek(Date.parse(time));
|
||||
return flush();
|
||||
};
|
||||
const goLive = (clock) => {
|
||||
clock.world.goLive();
|
||||
return flush();
|
||||
};
|
||||
function event() {
|
||||
const listeners = new Set();
|
||||
return {
|
||||
@@ -1802,7 +1812,7 @@ test('a delayed installation failure retains the replacement and does not throw
|
||||
h.rendering.clear();
|
||||
});
|
||||
|
||||
test('one row steps every registered observation; missing frames hide and latest restores each product', async (t) => {
|
||||
test('a row steps the world clock through every registered observation; missing frames hide and latest goes live', async (t) => {
|
||||
const clock = createWeatherClock();
|
||||
const radar = layerHarness({ clock });
|
||||
const satellite = layerHarness({
|
||||
@@ -1835,6 +1845,14 @@ test('one row steps every registered observation; missing frames hide and latest
|
||||
.chips.filter(({ id }) =>
|
||||
['previous', 'play', 'next', 'latest'].includes(id),
|
||||
);
|
||||
// As the timeline's arrow keys do, a step back from live goes to the
|
||||
// newest frame, and the next one back to the frame before it.
|
||||
radar.layer.setParams({ step: -1 });
|
||||
await flush();
|
||||
assert.equal(clock.getState().target, times[2]);
|
||||
assert.equal(clock.world.isPlaying(), false);
|
||||
for (const h of [radar, satellite]) h.stages.at(-1)?.finish();
|
||||
await flush();
|
||||
radar.layer.setParams({ step: -1 });
|
||||
await flush();
|
||||
assert.equal(clock.getState().target, times[1]);
|
||||
@@ -1886,6 +1904,33 @@ test('one row steps every registered observation; missing frames hide and latest
|
||||
assert.equal(clock.getState().products.length, 1);
|
||||
});
|
||||
|
||||
test('a row’s play and latest move the world clock as its timeline does', async (t) => {
|
||||
const clock = createWeatherClock();
|
||||
const { world } = clock;
|
||||
const h = layerHarness({ clock });
|
||||
t.after(() => {
|
||||
h.layer.destroy();
|
||||
clock.destroy();
|
||||
});
|
||||
const update = h.layer.update();
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
await update;
|
||||
h.layer.setParams({ play: true });
|
||||
assert.equal(world.isPlaying(), true);
|
||||
assert.equal(world.getState().stepping, false, 'the world plays on its own');
|
||||
assert.ok(
|
||||
world.now() - Date.parse(times[0]) < 1_000,
|
||||
'from the start when live',
|
||||
);
|
||||
h.layer.setParams({ play: true });
|
||||
assert.equal(world.isPlaying(), false);
|
||||
h.layer.setParams({ latest: true });
|
||||
assert.equal(world.isLive(), true);
|
||||
await flush();
|
||||
assert.equal(clock.getState().mode, 'latest');
|
||||
});
|
||||
|
||||
test('history manifest expiry hides imagery without jumping to latest; host resume keeps the target', async (t) => {
|
||||
const clock = createWeatherClock();
|
||||
let value = snapshot;
|
||||
@@ -1898,7 +1943,7 @@ test('history manifest expiry hides imagery without jumping to latest; host resu
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
await update;
|
||||
const history = clock.setTarget(times[0]);
|
||||
const history = seekTo(clock, times[0]);
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
await history;
|
||||
@@ -1967,7 +2012,7 @@ test('satellite global selection uses the three-hour gap and preserves the share
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
await update;
|
||||
await clock.setTarget(times[0]);
|
||||
await seekTo(clock, times[0]);
|
||||
assert.equal(
|
||||
h.layer.getDiagnostics().hidden,
|
||||
true,
|
||||
@@ -2026,7 +2071,8 @@ test('satellite clouds from an hourly feed draw all through each hour and read l
|
||||
);
|
||||
|
||||
// Forty-five minutes past a frame, between it and the next, it still shows.
|
||||
const between = clock.setTarget(
|
||||
const between = seekTo(
|
||||
clock,
|
||||
new Date(Date.parse(hourly[0]) + 45 * 60_000).toISOString(),
|
||||
);
|
||||
await flush();
|
||||
@@ -2037,7 +2083,7 @@ test('satellite clouds from an hourly feed draw all through each hour and read l
|
||||
|
||||
// Live again (a past time judges delay from the timeline's time), past
|
||||
// one and a half hours it reads late.
|
||||
const live = clock.latest();
|
||||
const live = goLive(clock);
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
await live;
|
||||
@@ -2059,11 +2105,11 @@ test('switching to history cancels a still-loading latest refresh and a later ta
|
||||
const update = h.layer.update();
|
||||
await flush();
|
||||
const latest = h.stages.at(-1);
|
||||
const oldHistory = clock.setTarget(times[0]);
|
||||
const oldHistory = seekTo(clock, times[0]);
|
||||
await flush();
|
||||
assert.equal(latest.signal.aborted, true);
|
||||
const oldStage = h.stages.at(-1);
|
||||
const current = clock.setTarget(times[1]);
|
||||
const current = seekTo(clock, times[1]);
|
||||
await flush();
|
||||
assert.equal(oldStage.signal.aborted, true);
|
||||
h.stages.at(-1).finish();
|
||||
@@ -2373,8 +2419,9 @@ test('failed prefetch leaves the current frame and errors unchanged and releases
|
||||
assert.deepEqual(h.rendering.getDiagnostics(), before);
|
||||
});
|
||||
|
||||
test('layer warms the next advertised observation only after a successful playing frame, wrapping at the end', async (t) => {
|
||||
test('as the world plays, the layer warms the next advertised observation after each frame that shows', async (t) => {
|
||||
const clock = createWeatherClock();
|
||||
const { world } = clock;
|
||||
const h = layerHarness({ clock });
|
||||
const warmed = [];
|
||||
let cancels = 0;
|
||||
@@ -2392,45 +2439,45 @@ test('layer warms the next advertised observation only after a successful playin
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
await updated;
|
||||
assert.equal(warmed.length, 0);
|
||||
const play = clock.play();
|
||||
await seekTo(clock, times[0]);
|
||||
h.stages.at(-1).finish();
|
||||
await flush();
|
||||
assert.equal(warmed.length, 0, 'paused, nothing is warmed');
|
||||
world.play({ rate: 1e-9, continuous: true });
|
||||
assert.equal(clock.getState().advancing, true);
|
||||
await seekTo(clock, times[1]);
|
||||
assert.equal(warmed.length, 0, 'still staging');
|
||||
h.stages.at(-1).finish();
|
||||
await play;
|
||||
await flush();
|
||||
assert.deepEqual(
|
||||
warmed.map(({ time }) => time),
|
||||
[times[0]],
|
||||
[times[2]],
|
||||
);
|
||||
assert.deepEqual(warmed[0].options, { infrared: 100 });
|
||||
const next = clock.setTarget(times[0]);
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
await next;
|
||||
assert.deepEqual(
|
||||
warmed.map(({ time }) => time),
|
||||
[times[0], times[1]],
|
||||
);
|
||||
const failed = clock.setTarget(times[1]);
|
||||
await flush();
|
||||
await seekTo(clock, times[0]);
|
||||
h.stages.at(-1).finish(false);
|
||||
await failed;
|
||||
assert.equal(warmed.length, 2);
|
||||
const priorCancels = cancels;
|
||||
clock.pause();
|
||||
assert.ok(cancels > priorCancels);
|
||||
const paused = clock.setTarget(times[2]);
|
||||
await flush();
|
||||
assert.equal(warmed.length, 1, 'a failed frame warms nothing');
|
||||
await seekTo(clock, times[2]);
|
||||
h.stages.at(-1).finish();
|
||||
await paused;
|
||||
assert.equal(warmed.length, 2);
|
||||
await clock.play();
|
||||
await flush();
|
||||
assert.equal(warmed.length, 1, 'the world plays on past the newest frame');
|
||||
const priorCancels = cancels;
|
||||
world.pause();
|
||||
await flush();
|
||||
assert.ok(cancels > priorCancels);
|
||||
await seekTo(clock, times[1]);
|
||||
h.stages.at(-1).finish();
|
||||
await flush();
|
||||
assert.equal(warmed.length, 1);
|
||||
world.play({ rate: 1e-9, continuous: true });
|
||||
h.documentRef.hidden = true;
|
||||
h.documentRef.emit('visibilitychange');
|
||||
await flush();
|
||||
h.stages.at(-1).finish();
|
||||
h.stages.at(-1)?.finish();
|
||||
await flush();
|
||||
assert.equal(warmed.length, 2, 'hidden products do not prefetch');
|
||||
assert.equal(warmed.length, 1, 'hidden products do not prefetch');
|
||||
world.goLive();
|
||||
});
|
||||
|
||||
for (const product of ['radar', 'clouds-regional', 'lightning']) {
|
||||
@@ -2767,7 +2814,7 @@ test('at a past time a frame is aged and its source judged delayed from the time
|
||||
|
||||
// Years later, the world clock is moved back to the newest frame.
|
||||
Date.now.mock.mockImplementation(() => Date.parse('2032-01-01T00:00:00Z'));
|
||||
void clock.setTarget(times[2]);
|
||||
void seekTo(clock, times[2]);
|
||||
await flush();
|
||||
h.stages.at(-1)?.finish();
|
||||
await flush();
|
||||
@@ -2778,7 +2825,7 @@ test('at a past time a frame is aged and its source judged delayed from the time
|
||||
assert.equal(h.layer.getStats().stale, false);
|
||||
|
||||
// Ten minutes after the newest frame, the frame is ten minutes old.
|
||||
void clock.setTarget('2026-09-15T20:20:00.000Z');
|
||||
void seekTo(clock, '2026-09-15T20:20:00.000Z');
|
||||
await flush();
|
||||
h.stages.at(-1)?.finish();
|
||||
await flush();
|
||||
|
||||
Reference in new issue
Block a user