mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-18 01:02:23 +00:00
Merge main through #1634 into the renderer seam
Brings in the CCTV opening camera, the Iceland, Giroc HLS, Taiwan, Quebec, Ottawa and world camera packs, the world clock and timeline, the zoom-out limit and registered-only layer state, ported onto the renderer contract: the sky follows the world time through the frame port's setTime in preUpdate, the Quebec overlay reads the eye, projects and redraws through the camera, query and frame ports, launches hide pending missions through their draw records, and mission orbit rings turn on the scene clock.
This commit is contained in:
commit
2b60531c3c
170 files changed
+44096
-319
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import { findFrame } from '../render/rendererOf.js';
|
||||
|
||||
/**
|
||||
* Draw the sky at the world time: the sun, moon, stars, atmosphere and the
|
||||
* lighting on the tiles all read the frame port's scene time, which never
|
||||
* moved on its own, so before this the sky stayed at the moment the page
|
||||
* loaded.
|
||||
*
|
||||
* Each frame's time is set in `preUpdate`, so the sky is drawn at the same
|
||||
* moment the layers read that frame.
|
||||
* @param {object} viewer
|
||||
* @param {() => number} readTime Epoch ms the sky is drawn at.
|
||||
* @param {{subscribe?: Function, requestRender?: () => void}} [options]
|
||||
* A world clock to hear moves from, so a resting scene repaints when the
|
||||
* world time jumps.
|
||||
* @returns {() => void} Stop following.
|
||||
*/
|
||||
export function followWorldTime(
|
||||
viewer,
|
||||
readTime,
|
||||
{
|
||||
subscribe,
|
||||
requestRender = () => findFrame(viewer)?.requestRender('world-time'),
|
||||
} = {},
|
||||
) {
|
||||
const frame = findFrame(viewer);
|
||||
const off = frame?.on('preUpdate', () => frame.setTime(readTime())) ?? null;
|
||||
let last = readTime();
|
||||
const unsubscribe =
|
||||
subscribe?.(() => {
|
||||
const next = readTime();
|
||||
if (next === last) return;
|
||||
last = next;
|
||||
requestRender();
|
||||
}) ?? (() => {});
|
||||
return () => {
|
||||
off?.();
|
||||
unsubscribe();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,419 @@
|
||||
/**
|
||||
* The world's one time, in epoch ms. Every layer that draws something at a
|
||||
* moment reads it here instead of calling `Date.now()` itself, so layers can
|
||||
* never disagree about when the world is.
|
||||
*
|
||||
* Live is a mode, not a timestamp: while live the clock is the wall clock and
|
||||
* every layer behaves as it would reading `Date.now()`. Away from live the
|
||||
* clock holds a cursor, fixed or playing. Only the layers that follow the clock
|
||||
* move with the cursor; the rest keep reading the live edge through
|
||||
* `timeFor(id)`. On the page's clock the layers whose data has a time of its
|
||||
* own (the sky, satellites, launches and weather) follow it; live feeds such
|
||||
* as flights and vessels have no time to move to and stay live.
|
||||
*
|
||||
* Playback is continuous (`cursor + elapsed × rate`) unless a driver takes
|
||||
* it: weather replay steps its observed frames itself and moves the clock as
|
||||
* it goes, so play and pause hand over to it. Several drivers may be attached;
|
||||
* play goes to the newest that can play, pause to the one playing.
|
||||
*/
|
||||
|
||||
/** The span a JS Date can hold, either side of 1970. */
|
||||
const MAX_DATE_MS = 8.64e15;
|
||||
|
||||
/** Whether `ms` is a time the world clock can hold. */
|
||||
export const isWorldTime = (ms) =>
|
||||
Number.isFinite(ms) && Math.abs(ms) <= MAX_DATE_MS;
|
||||
|
||||
/**
|
||||
* How often subscribers hear of a continuously playing cursor with no
|
||||
* animation frames to go by; with them, they hear of it every frame.
|
||||
*/
|
||||
export const WORLD_CLOCK_TICK_MS = 250;
|
||||
|
||||
/**
|
||||
* @param {{now?: () => number, monotonicNow?: () => number,
|
||||
* setInterval?: typeof globalThis.setInterval,
|
||||
* clearInterval?: typeof globalThis.clearInterval,
|
||||
* requestFrame?: typeof globalThis.requestAnimationFrame | null,
|
||||
* cancelFrame?: typeof globalThis.cancelAnimationFrame | null,
|
||||
* follow?: Iterable<string>}} [options] `follow` lists the layers that
|
||||
* move with the cursor from the start. Playing, the clock tells its
|
||||
* listeners of the time each animation frame, or each tick without frames.
|
||||
*/
|
||||
export function createWorldClock({
|
||||
// Read at each call, so a stand-in for Date.now (a test's, a harness's)
|
||||
// reaches every layer as it did when they called Date.now themselves.
|
||||
now = () => Date.now(),
|
||||
monotonicNow = () => globalThis.performance.now(),
|
||||
setInterval = globalThis.setInterval,
|
||||
clearInterval = globalThis.clearInterval,
|
||||
requestFrame = globalThis.requestAnimationFrame?.bind(globalThis) ?? null,
|
||||
cancelFrame = globalThis.cancelAnimationFrame?.bind(globalThis) ?? null,
|
||||
follow = [],
|
||||
} = {}) {
|
||||
const listeners = new Set();
|
||||
// Layers a host follows, and layers a part of the app holds following.
|
||||
const following = new Set(follow);
|
||||
const holds = new Map();
|
||||
const readers = new Map();
|
||||
const sources = new Set();
|
||||
const drivers = [];
|
||||
let live = true;
|
||||
// The cursor while fixed; while playing continuously, the cursor at `anchorMono`.
|
||||
let cursorMs = null;
|
||||
let anchorMono = 0;
|
||||
let rate = 1;
|
||||
let playing = false;
|
||||
// The driver handle stepping the clock, or null.
|
||||
let active = null;
|
||||
let ticker = null;
|
||||
let notifying = 0;
|
||||
// Layers loading what the cursor needs; continuous play holds while any is.
|
||||
const waits = new Map();
|
||||
// Times continuous play may not pass yet, by layer: each one's next frame.
|
||||
const gates = new Map();
|
||||
|
||||
const continuous = () => playing && !active;
|
||||
const buffering = () => waits.size > 0;
|
||||
const isFollowed = (id) => following.has(id) || (holds.get(id) ?? 0) > 0;
|
||||
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() || buffering()) return cursorMs;
|
||||
let at = cursorMs + (monotonicNow() - anchorMono) * rate;
|
||||
for (const gate of gates.values())
|
||||
if (gate >= cursorMs) at = Math.min(at, gate);
|
||||
return at;
|
||||
};
|
||||
const getState = () =>
|
||||
Object.freeze({
|
||||
live,
|
||||
timeMs: time(),
|
||||
playing,
|
||||
rate,
|
||||
// A driver steps the clock, at its own pace rather than the rate.
|
||||
stepping: Boolean(active),
|
||||
// Playing, but held while a layer loads what the cursor needs.
|
||||
buffering: continuous() && buffering(),
|
||||
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((state ??= getState()));
|
||||
} catch (error) {
|
||||
console.error('World clock listener failed', error);
|
||||
}
|
||||
}
|
||||
};
|
||||
// Playing, the time moves on smoothly: each frame, so the timeline and the
|
||||
// globe move with it and a layer's next frame is met as it comes, or each
|
||||
// tick where there are no frames.
|
||||
const frames = Boolean(requestFrame && cancelFrame);
|
||||
const frame = () => {
|
||||
ticker = requestFrame(frame);
|
||||
notify();
|
||||
};
|
||||
const syncTicker = () => {
|
||||
if (continuous() && listeners.size) {
|
||||
if (ticker === null)
|
||||
ticker = frames
|
||||
? requestFrame(frame)
|
||||
: setInterval(notify, WORLD_CLOCK_TICK_MS);
|
||||
} else if (ticker !== null) {
|
||||
if (frames) cancelFrame(ticker);
|
||||
else clearInterval(ticker);
|
||||
ticker = null;
|
||||
}
|
||||
};
|
||||
const changed = () => {
|
||||
syncTicker();
|
||||
notify();
|
||||
};
|
||||
// Freeze a continuously playing cursor where it is.
|
||||
const settle = () => {
|
||||
if (continuous()) cursorMs = time();
|
||||
anchorMono = monotonicNow();
|
||||
};
|
||||
|
||||
const api = {
|
||||
/** The live edge: the wall clock, whatever the cursor does. */
|
||||
wallNow: () => now(),
|
||||
/** The world time: the wall clock while live, else the cursor. */
|
||||
now: time,
|
||||
isLive: () => live,
|
||||
isPlaying: () => playing,
|
||||
getRate: () => rate,
|
||||
|
||||
/** Hold the world at `ms`; a playing clock plays on from there. */
|
||||
seek(ms) {
|
||||
if (!isWorldTime(ms)) throw new TypeError('World time must be epoch ms');
|
||||
if (!live && cursorMs === ms && !continuous()) return;
|
||||
live = false;
|
||||
cursorMs = ms;
|
||||
anchorMono = monotonicNow();
|
||||
changed();
|
||||
},
|
||||
/** Back to the wall clock; playback stops and its rate goes back to 1. */
|
||||
goLive() {
|
||||
if (live && !playing && rate === 1) return;
|
||||
const stepping = active;
|
||||
live = true;
|
||||
cursorMs = null;
|
||||
playing = false;
|
||||
active = null;
|
||||
rate = 1;
|
||||
// Its driven(false) finds nothing left to stop.
|
||||
stepping?.driver.pause();
|
||||
changed();
|
||||
},
|
||||
/**
|
||||
* Play from the cursor (from now, when live). A driver steps it unless
|
||||
* `continuous`, which runs the cursor at the rate past every driver.
|
||||
*/
|
||||
play({ rate: nextRate, continuous: runOn = false } = {}) {
|
||||
if (nextRate !== undefined) api.setRate(nextRate);
|
||||
if (playing) return;
|
||||
for (let index = drivers.length - 1; !runOn && index >= 0; index -= 1) {
|
||||
if (drivers[index].driver.play() !== false) return;
|
||||
}
|
||||
if (live) {
|
||||
live = false;
|
||||
cursorMs = now();
|
||||
}
|
||||
playing = true;
|
||||
anchorMono = monotonicNow();
|
||||
changed();
|
||||
},
|
||||
pause() {
|
||||
if (!playing) return;
|
||||
if (active) {
|
||||
active.driver.pause();
|
||||
return;
|
||||
}
|
||||
settle();
|
||||
playing = false;
|
||||
changed();
|
||||
},
|
||||
togglePlay() {
|
||||
return playing ? api.pause() : api.play();
|
||||
},
|
||||
/**
|
||||
* Hold continuous play where it is while layer `id` loads what the cursor
|
||||
* needs, as a video waits on its buffer; play goes on once every hold is
|
||||
* released. A seek still moves the cursor. Returns the release. Neither
|
||||
* tells listeners (play's own ticks do), so a layer can take and release a
|
||||
* hold mid-change without anyone moving the clock under it.
|
||||
*/
|
||||
wait(id) {
|
||||
settle();
|
||||
waits.set(id, (waits.get(id) ?? 0) + 1);
|
||||
let held = true;
|
||||
return () => {
|
||||
if (!held) return;
|
||||
held = false;
|
||||
const count = waits.get(id) - 1;
|
||||
if (count > 0) {
|
||||
waits.set(id, count);
|
||||
return;
|
||||
}
|
||||
waits.delete(id);
|
||||
// Play goes on from where it held, not from where it would have been.
|
||||
if (!buffering()) anchorMono = monotonicNow();
|
||||
};
|
||||
},
|
||||
/**
|
||||
* Keep continuous play from passing `ms` (null lifts it): the time layer
|
||||
* `id` shows next, which it has yet to load. Play stops there, to wait on
|
||||
* the load as it starts, rather than rush past frames between ticks.
|
||||
*/
|
||||
gate(id, ms) {
|
||||
settle();
|
||||
if (ms === null || ms === undefined) gates.delete(id);
|
||||
else if (isWorldTime(ms)) gates.set(id, ms);
|
||||
},
|
||||
/** Simulated ms per real ms for continuous playback. */
|
||||
setRate(nextRate) {
|
||||
if (!Number.isFinite(nextRate) || nextRate <= 0)
|
||||
throw new RangeError('World clock rate must be positive');
|
||||
if (nextRate === rate) return;
|
||||
settle();
|
||||
rate = nextRate;
|
||||
changed();
|
||||
},
|
||||
|
||||
/**
|
||||
* Offer a stepping player. The driver moves the clock with `seek()` and
|
||||
* reports through its handle's `driven(playing, atMs?)`; its `play()`
|
||||
* returns false when it cannot play, and the clock tries an older driver
|
||||
* or plays continuously instead.
|
||||
* @param {{play: () => (boolean|void), pause: () => void}} driver
|
||||
* @returns {{driven: (playing: boolean, atMs?: number) => void,
|
||||
* detach: () => number}} `detach()` returns how many drivers remain.
|
||||
*/
|
||||
attachDriver(driver) {
|
||||
const handle = {
|
||||
driver,
|
||||
/** This driver started stepping (at `atMs`, if given) or stopped. */
|
||||
driven(isPlaying, atMs) {
|
||||
if (!drivers.includes(handle)) return;
|
||||
if (!isPlaying) {
|
||||
if (active !== handle) return;
|
||||
active = null;
|
||||
playing = false;
|
||||
changed();
|
||||
return;
|
||||
}
|
||||
// A continuous run hands over where it is; another driver stops.
|
||||
settle();
|
||||
const previous = active;
|
||||
active = handle;
|
||||
playing = true;
|
||||
live = false;
|
||||
if (Number.isFinite(atMs)) cursorMs = atMs;
|
||||
else cursorMs ??= now();
|
||||
anchorMono = monotonicNow();
|
||||
if (previous && previous !== handle) previous.driver.pause();
|
||||
changed();
|
||||
},
|
||||
detach() {
|
||||
const index = drivers.indexOf(handle);
|
||||
if (index < 0) return drivers.length;
|
||||
handle.driven(false);
|
||||
drivers.splice(index, 1);
|
||||
return drivers.length;
|
||||
},
|
||||
};
|
||||
drivers.push(handle);
|
||||
return handle;
|
||||
},
|
||||
|
||||
/** Let layer `id` move with the cursor instead of reading the live edge. */
|
||||
follow(id, on = true) {
|
||||
if (on === following.has(id)) return;
|
||||
if (on) following.add(id);
|
||||
else following.delete(id);
|
||||
changed();
|
||||
},
|
||||
/**
|
||||
* Keep layer `id` following until the returned release is called, however
|
||||
* many parts of the app hold it.
|
||||
*/
|
||||
hold(id) {
|
||||
const was = isFollowed(id);
|
||||
holds.set(id, (holds.get(id) ?? 0) + 1);
|
||||
if (!was) changed();
|
||||
let held = true;
|
||||
return () => {
|
||||
if (!held) return;
|
||||
held = false;
|
||||
const count = holds.get(id) - 1;
|
||||
if (count > 0) holds.set(id, count);
|
||||
else holds.delete(id);
|
||||
if (!isFollowed(id)) changed();
|
||||
};
|
||||
},
|
||||
follows: isFollowed,
|
||||
/**
|
||||
* The time layer `id` draws at: the cursor when it follows the clock,
|
||||
* otherwise the live edge.
|
||||
* @returns {() => number}
|
||||
*/
|
||||
timeFor(id) {
|
||||
let reader = readers.get(id);
|
||||
if (!reader) {
|
||||
reader = () => (isFollowed(id) ? time() : now());
|
||||
readers.set(id, reader);
|
||||
}
|
||||
return reader;
|
||||
},
|
||||
|
||||
/**
|
||||
* A layer's span of time it can show, for a host's timeline; null while
|
||||
* it has none (off, or nothing loaded). Returns the unregister.
|
||||
* @param {{id: string, coverage: () => ({start: number, end: number, marks?: number[]} | null)}} source
|
||||
*/
|
||||
register(source) {
|
||||
// Each registration is its own, even under a shared id.
|
||||
const entry = { source };
|
||||
sources.add(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 = 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);
|
||||
}
|
||||
if (start > end) return null;
|
||||
return { start, end, marks: [...marks].sort((a, b) => a - b) };
|
||||
},
|
||||
|
||||
getState,
|
||||
subscribe(listener) {
|
||||
listeners.add(listener);
|
||||
syncTicker();
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
syncTicker();
|
||||
};
|
||||
},
|
||||
};
|
||||
return api;
|
||||
}
|
||||
|
||||
/**
|
||||
* The page's world clock. Layers, the sky and a host all read this one. The
|
||||
* sky and the HUD's clock show the world time; satellites (and the launched
|
||||
* missions flying on their time) and launches have a time of their own and
|
||||
* move with it; weather holds each product it shows.
|
||||
*/
|
||||
export const worldClock = createWorldClock({
|
||||
follow: ['sky', 'hud', 'satellites', 'rocket-launches'],
|
||||
});
|
||||
|
||||
/** The time layer `id` draws at on the page's clock. */
|
||||
export const worldTimeFor = (id) => worldClock.timeFor(id);
|
||||
@@ -0,0 +1,389 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createWorldClock, WORLD_CLOCK_TICK_MS } from './worldClock.js';
|
||||
|
||||
function fakeTime() {
|
||||
const t = {
|
||||
wall: Date.UTC(2026, 10, 1, 12),
|
||||
mono: 1_000,
|
||||
timers: new Map(),
|
||||
nextId: 1,
|
||||
advance(ms) {
|
||||
t.wall += ms;
|
||||
t.mono += ms;
|
||||
},
|
||||
options: {
|
||||
now: () => t.wall,
|
||||
monotonicNow: () => t.mono,
|
||||
setInterval: (fn, ms) => {
|
||||
const id = t.nextId++;
|
||||
t.timers.set(id, { fn, ms });
|
||||
return id;
|
||||
},
|
||||
clearInterval: (id) => t.timers.delete(id),
|
||||
},
|
||||
};
|
||||
return t;
|
||||
}
|
||||
|
||||
test('live is the wall clock for every layer', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
assert.equal(clock.isLive(), true);
|
||||
assert.equal(clock.now(), t.wall);
|
||||
t.advance(5_000);
|
||||
assert.equal(clock.now(), t.wall);
|
||||
assert.equal(clock.timeFor('flights')(), t.wall);
|
||||
clock.follow('flights');
|
||||
assert.equal(clock.timeFor('flights')(), t.wall);
|
||||
});
|
||||
|
||||
test('away from live only following layers move with the cursor', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const satellites = clock.timeFor('satellites');
|
||||
const flights = clock.timeFor('flights');
|
||||
clock.follow('satellites');
|
||||
const past = t.wall - 3_600_000;
|
||||
clock.seek(past);
|
||||
assert.equal(clock.isLive(), false);
|
||||
assert.equal(clock.now(), past);
|
||||
assert.equal(satellites(), past);
|
||||
assert.equal(flights(), t.wall);
|
||||
assert.equal(clock.timeFor('satellites'), satellites);
|
||||
clock.goLive();
|
||||
assert.equal(satellites(), t.wall);
|
||||
});
|
||||
|
||||
test('layers named at creation follow from the start', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock({ ...t.options, follow: ['satellites'] });
|
||||
clock.seek(t.wall - 1_000);
|
||||
assert.equal(clock.timeFor('satellites')(), t.wall - 1_000);
|
||||
assert.equal(clock.timeFor('flights')(), t.wall);
|
||||
clock.follow('satellites', false);
|
||||
assert.equal(clock.follows('satellites'), false);
|
||||
});
|
||||
|
||||
test('the page clock moves the layers with a time of their own', async () => {
|
||||
const { worldClock } = await import('./worldClock.js');
|
||||
for (const id of ['sky', 'hud', 'satellites', 'rocket-launches'])
|
||||
assert.equal(worldClock.follows(id), true, id);
|
||||
for (const id of ['flights', 'military', 'vessels', 'transit'])
|
||||
assert.equal(worldClock.follows(id), false, id);
|
||||
});
|
||||
|
||||
test('continuous play advances the cursor at the rate and pause holds it', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const start = t.wall - 60_000;
|
||||
clock.seek(start);
|
||||
clock.play({ rate: 60 });
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), start + 60_000);
|
||||
clock.setRate(120);
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), start + 180_000);
|
||||
clock.pause();
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), start + 180_000);
|
||||
assert.equal(clock.isPlaying(), false);
|
||||
});
|
||||
|
||||
test('play from live starts at now, and goLive returns to the wall clock', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const from = t.wall;
|
||||
clock.play({ rate: 2 });
|
||||
assert.equal(clock.isLive(), false);
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), from + 2_000);
|
||||
clock.goLive();
|
||||
assert.equal(clock.isPlaying(), false);
|
||||
assert.equal(clock.now(), t.wall);
|
||||
});
|
||||
|
||||
test('subscribers hear changes, and ticks only while playing continuously', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const states = [];
|
||||
const off = clock.subscribe((state) => states.push(state));
|
||||
clock.seek(t.wall - 1_000);
|
||||
assert.equal(states.length, 1);
|
||||
assert.equal(states[0].live, false);
|
||||
assert.equal(t.timers.size, 0);
|
||||
clock.play();
|
||||
assert.equal(t.timers.size, 1);
|
||||
assert.equal([...t.timers.values()][0].ms, WORLD_CLOCK_TICK_MS);
|
||||
clock.pause();
|
||||
assert.equal(t.timers.size, 0);
|
||||
clock.play();
|
||||
off();
|
||||
assert.equal(t.timers.size, 0);
|
||||
});
|
||||
|
||||
test('a driver takes play and pause, and moves the clock with seek', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const calls = [];
|
||||
const handle = clock.attachDriver({
|
||||
play() {
|
||||
calls.push('play');
|
||||
handle.driven(true, t.wall - 10_000);
|
||||
},
|
||||
pause() {
|
||||
calls.push('pause');
|
||||
handle.driven(false);
|
||||
},
|
||||
});
|
||||
clock.play();
|
||||
assert.deepEqual(calls, ['play']);
|
||||
assert.equal(clock.isPlaying(), true);
|
||||
assert.equal(clock.now(), t.wall - 10_000);
|
||||
const at = clock.now();
|
||||
t.advance(5_000);
|
||||
// Stepped, not continuous: the cursor waits for the driver.
|
||||
assert.equal(clock.now(), at);
|
||||
clock.pause();
|
||||
assert.deepEqual(calls, ['play', 'pause']);
|
||||
assert.equal(clock.isPlaying(), false);
|
||||
clock.play();
|
||||
clock.goLive();
|
||||
assert.deepEqual(calls, ['play', 'pause', 'play', 'pause']);
|
||||
assert.equal(clock.isLive(), true);
|
||||
assert.equal(clock.isPlaying(), false);
|
||||
assert.equal(handle.detach(), 0);
|
||||
clock.play();
|
||||
assert.equal(calls.length, 4);
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), t.wall);
|
||||
});
|
||||
|
||||
test('play goes to the newest driver that can play, pause to the one playing', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const calls = [];
|
||||
const driver = (name, canPlay) => {
|
||||
const handle = clock.attachDriver({
|
||||
play() {
|
||||
if (!canPlay()) return false;
|
||||
calls.push(`${name} play`);
|
||||
handle.driven(true);
|
||||
},
|
||||
pause() {
|
||||
calls.push(`${name} pause`);
|
||||
handle.driven(false);
|
||||
},
|
||||
});
|
||||
return handle;
|
||||
};
|
||||
let bCanPlay = false;
|
||||
const a = driver('a', () => true);
|
||||
const b = driver('b', () => bCanPlay);
|
||||
clock.seek(t.wall - 1_000);
|
||||
clock.play();
|
||||
assert.deepEqual(calls, ['a play']);
|
||||
// b starting takes over from a.
|
||||
bCanPlay = true;
|
||||
b.driven(true);
|
||||
assert.deepEqual(calls, ['a play', 'a pause']);
|
||||
clock.pause();
|
||||
assert.deepEqual(calls, ['a play', 'a pause', 'b pause']);
|
||||
assert.equal(clock.isPlaying(), false);
|
||||
// A detached driver stops, and the other one plays next.
|
||||
clock.play();
|
||||
assert.equal(b.detach(), 1);
|
||||
assert.equal(clock.isPlaying(), false);
|
||||
clock.play();
|
||||
assert.equal(calls.at(-1), 'a play');
|
||||
assert.equal(a.detach(), 0);
|
||||
assert.equal(clock.isPlaying(), false);
|
||||
});
|
||||
|
||||
test('a listener that changes the clock leaves nobody with a stale state', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const seen = [];
|
||||
clock.subscribe((state) => {
|
||||
if (state.timeMs === 0) clock.seek(1_000);
|
||||
});
|
||||
clock.subscribe((state) => seen.push(state.timeMs));
|
||||
clock.seek(0);
|
||||
assert.deepEqual(seen, [1_000]);
|
||||
});
|
||||
|
||||
test('holds keep a layer following until every holder releases it', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
const first = clock.hold('radar');
|
||||
const second = clock.hold('radar');
|
||||
first();
|
||||
first();
|
||||
assert.equal(clock.follows('radar'), true);
|
||||
second();
|
||||
assert.equal(clock.follows('radar'), false);
|
||||
clock.follow('radar');
|
||||
const third = clock.hold('radar');
|
||||
third();
|
||||
assert.equal(clock.follows('radar'), true);
|
||||
assert.deepEqual(clock.getState().following, ['radar']);
|
||||
});
|
||||
|
||||
test('a driver that cannot play leaves continuous playback', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
clock.attachDriver({ play: () => false, pause() {} });
|
||||
clock.seek(t.wall - 10_000);
|
||||
clock.play();
|
||||
assert.equal(clock.isPlaying(), true);
|
||||
t.advance(1_000);
|
||||
// Wall and cursor moved the same second at rate 1.
|
||||
assert.equal(clock.now(), t.wall - 10_000);
|
||||
});
|
||||
|
||||
test('coverage is the union of the registered sources', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
assert.equal(clock.coverage(), null);
|
||||
const off = clock.register({
|
||||
id: 'radar',
|
||||
coverage: () => ({ start: 100, end: 200, marks: [100, 150, 200] }),
|
||||
});
|
||||
clock.register({ id: 'satellites', coverage: () => ({ start: 50, end: 180 }) });
|
||||
clock.register({ id: 'empty', coverage: () => null });
|
||||
assert.deepEqual(clock.coverage(), {
|
||||
start: 50,
|
||||
end: 200,
|
||||
marks: [100, 150, 200],
|
||||
});
|
||||
off();
|
||||
assert.deepEqual(clock.coverage(), { start: 50, end: 180, marks: [] });
|
||||
});
|
||||
|
||||
test('seek refuses anything but epoch ms and rate must be positive', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
assert.throws(() => clock.seek(Number.NaN), TypeError);
|
||||
assert.throws(() => clock.setRate(0), RangeError);
|
||||
});
|
||||
|
||||
test('seek refuses a time no Date can hold', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
assert.throws(() => clock.seek(8.8e15), TypeError);
|
||||
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);
|
||||
});
|
||||
|
||||
test('continuous play runs past every driver, and live resets the rate', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const calls = [];
|
||||
clock.attachDriver({ play: () => calls.push('play'), pause() {} });
|
||||
clock.seek(t.wall - 60_000);
|
||||
clock.play({ rate: 30, continuous: true });
|
||||
assert.deepEqual(calls, []);
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), t.wall - 61_000 + 30_000);
|
||||
clock.goLive();
|
||||
assert.equal(clock.getRate(), 1);
|
||||
});
|
||||
|
||||
test('continuous play holds while a layer waits, and goes on from there', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const start = t.wall - 600_000;
|
||||
clock.seek(start);
|
||||
clock.play({ rate: 60, continuous: true });
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), start + 60_000);
|
||||
const release = clock.wait('radar');
|
||||
const other = clock.wait('clouds');
|
||||
assert.equal(clock.getState().buffering, true);
|
||||
t.advance(5_000);
|
||||
assert.equal(clock.now(), start + 60_000, 'held while loading');
|
||||
release();
|
||||
release();
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), start + 60_000, 'held until every layer is ready');
|
||||
other();
|
||||
assert.equal(clock.getState().buffering, false);
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), start + 120_000, 'on from where it held');
|
||||
});
|
||||
|
||||
test('a seek still moves a waiting clock', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
clock.seek(t.wall - 600_000);
|
||||
clock.play({ continuous: true });
|
||||
const release = clock.wait('radar');
|
||||
clock.seek(t.wall - 60_000);
|
||||
assert.equal(clock.now(), t.wall - 60_000);
|
||||
release();
|
||||
});
|
||||
|
||||
test('playing, listeners hear of the time every animation frame', () => {
|
||||
const t = fakeTime();
|
||||
const frames = new Map();
|
||||
let nextFrame = 1;
|
||||
const clock = createWorldClock({
|
||||
...t.options,
|
||||
requestFrame: (fn) => {
|
||||
frames.set(nextFrame, fn);
|
||||
return nextFrame++;
|
||||
},
|
||||
cancelFrame: (id) => frames.delete(id),
|
||||
});
|
||||
const heard = [];
|
||||
clock.subscribe((state) => heard.push(state.timeMs));
|
||||
const start = t.wall - 60_000;
|
||||
clock.seek(start);
|
||||
clock.play({ rate: 60 });
|
||||
assert.equal(t.timers.size, 0, 'no ticks with frames to go by');
|
||||
assert.equal(frames.size, 1);
|
||||
const runFrame = () => {
|
||||
const [[id, fn]] = frames;
|
||||
frames.delete(id);
|
||||
fn();
|
||||
};
|
||||
heard.length = 0;
|
||||
for (let i = 0; i < 3; i++) {
|
||||
t.advance(16);
|
||||
runFrame();
|
||||
}
|
||||
assert.deepEqual(heard, [start + 960, start + 1_920, start + 2_880]);
|
||||
clock.pause();
|
||||
assert.equal(frames.size, 0);
|
||||
});
|
||||
Reference in new issue
Block a user