mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Show the timeline only while a layer with time to show is on
The bar now needs more than the world clock being away from live: a layer that moves with the clock must be on with time to show. In Core that is weather radar, satellite or lightning with frames; a host adds layers by following them. Turning the last one off hides the bar; turning it back on brings it back. The world clock reports this as `supported` in its state. Weather products register their frames as coverage one by one, suspended or not, so the way back to live stays on screen with reduced motion. Weather refreshes and satellite catalog changes tell the clock their coverage may have changed. A coverage that throws counts as none, and the clock reads its state once per notification round.
This commit is contained in:
1 parent
434fc9602d
commit
690a6637e2
12 files changed
+205
-25
No files matched your search
+33
-4
@@ -61,6 +61,25 @@ export function createWorldClock({
|
||||
const followedIds = () => [
|
||||
...new Set([...following, ...[...holds.keys()].filter(isFollowed)]),
|
||||
];
|
||||
// A host's coverage that throws counts as none, so it cannot break the
|
||||
// listeners every state reaches.
|
||||
const spanOf = (source) => {
|
||||
let span;
|
||||
try {
|
||||
span = source.coverage();
|
||||
} catch (error) {
|
||||
console.error(`World time coverage failed: ${source.id}`, error);
|
||||
return null;
|
||||
}
|
||||
return span && span.end >= span.start ? span : null;
|
||||
};
|
||||
// Whether a layer that moves with the clock has time to show: only then is
|
||||
// there anything for a timeline to play.
|
||||
const isSupported = () => {
|
||||
for (const { source } of sources)
|
||||
if (isFollowed(source.id) && spanOf(source)) return true;
|
||||
return false;
|
||||
};
|
||||
const time = () => {
|
||||
if (live) return now();
|
||||
if (continuous()) return cursorMs + (monotonicNow() - anchorMono) * rate;
|
||||
@@ -73,17 +92,20 @@ export function createWorldClock({
|
||||
playing,
|
||||
rate,
|
||||
following: followedIds(),
|
||||
supported: isSupported(),
|
||||
});
|
||||
// Each listener hears the state as it is when called. A change made from
|
||||
// inside a listener notifies everyone afresh, and the outer round stops, so
|
||||
// nobody is left holding a state that has since changed.
|
||||
// A round's state is read once: any change made during it starts a new one.
|
||||
const notify = () => {
|
||||
const round = ++notifying;
|
||||
let state = null;
|
||||
for (const listener of [...listeners]) {
|
||||
if (round !== notifying) return;
|
||||
if (!listeners.has(listener)) continue;
|
||||
try {
|
||||
listener(getState());
|
||||
listener((state ??= getState()));
|
||||
} catch (error) {
|
||||
console.error('World clock listener failed', error);
|
||||
}
|
||||
@@ -270,16 +292,23 @@ export function createWorldClock({
|
||||
// Each registration is its own, even under a shared id.
|
||||
const entry = { source };
|
||||
sources.add(entry);
|
||||
return () => sources.delete(entry);
|
||||
changed();
|
||||
return () => {
|
||||
if (sources.delete(entry)) changed();
|
||||
};
|
||||
},
|
||||
/** A source's coverage changed (frames loaded, the layer paused). */
|
||||
coverageChanged: () => changed(),
|
||||
/** Whether a followed layer has time to show; see getState().supported. */
|
||||
isSupported,
|
||||
/** The union of the registered sources' coverage, or null with none. */
|
||||
coverage() {
|
||||
let start = Infinity;
|
||||
let end = -Infinity;
|
||||
const marks = new Set();
|
||||
for (const { source } of sources) {
|
||||
const span = source.coverage();
|
||||
if (!span || !(span.end >= span.start)) continue;
|
||||
const span = spanOf(source);
|
||||
if (!span) continue;
|
||||
start = Math.min(start, span.start);
|
||||
end = Math.max(end, span.end);
|
||||
for (const mark of span.marks ?? []) marks.add(mark);
|
||||
|
||||
@@ -252,3 +252,38 @@ test('seek refuses a time no Date can hold', () => {
|
||||
clock.seek(-8.64e15);
|
||||
assert.equal(clock.now(), -8.64e15);
|
||||
});
|
||||
|
||||
test('only a followed layer with time to show supports the timeline', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
let span = null;
|
||||
const states = [];
|
||||
clock.subscribe((state) => states.push(state.supported));
|
||||
const off = clock.register({ id: 'satellites', coverage: () => span });
|
||||
span = { start: 0, end: 10 };
|
||||
clock.coverageChanged();
|
||||
assert.equal(clock.isSupported(), false, 'covered but not followed');
|
||||
clock.follow('satellites');
|
||||
assert.equal(clock.isSupported(), true);
|
||||
assert.equal(states.at(-1), true);
|
||||
span = null;
|
||||
clock.coverageChanged();
|
||||
assert.equal(states.at(-1), false);
|
||||
span = { start: 0, end: 10 };
|
||||
off();
|
||||
assert.equal(clock.isSupported(), false);
|
||||
assert.equal(states.at(-1), false);
|
||||
});
|
||||
|
||||
test('a coverage that throws counts as none', (t) => {
|
||||
t.mock.method(console, 'error', () => {});
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
clock.follow('broken');
|
||||
clock.register({
|
||||
id: 'broken',
|
||||
coverage() {
|
||||
throw new Error('no');
|
||||
},
|
||||
});
|
||||
assert.equal(clock.getState().supported, false);
|
||||
assert.equal(clock.coverage(), null);
|
||||
});
|
||||
Reference in new issue
Block a user