mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-18 01:02:23 +00:00
Main brings the reviewed one-time-control and timeline-mark changes. They keep this branch's renderer work: earthquakes draw through the draw port and register their strong quakes as marks; the weather clock keeps `follows` beside `world`; the weather layer measures ages and delays from the timeline time; the Visual panel keeps its sky and weather groups under Effects. Tests that moved time through the weather clock now move it on the world clock.
This commit is contained in:
commit
0ecbfbecd0
123 files changed
+5917
-1471
No files matched your search
@@ -119,6 +119,7 @@ test('at the live edge a layer with a timeline only follows and registers', asyn
|
||||
start: WALL - 3_600_000,
|
||||
end: WALL,
|
||||
marks: [],
|
||||
markDetails: [],
|
||||
});
|
||||
assert.equal(h.follower.active(), false);
|
||||
assert.deepEqual(h.calls, []);
|
||||
|
||||
+128
-12
@@ -14,11 +14,85 @@
|
||||
* src/time/snapshotTimeline.js); otherwise they 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.
|
||||
* it: a driver steps the clock itself and moves it 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. Layers that load frames
|
||||
* (weather) do not drive it: they gate continuous play at their next frame.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A timeline mark as a layer gives it: its time in epoch ms, and optionally
|
||||
* a short label to show for it, the layer's own id for it and its kind.
|
||||
* @typedef {{time: number, label?: string, id?: string, kind?: string}} WorldClockMarkInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* A timeline mark as `coverage().markDetails` returns it: a finite time, and
|
||||
* only those of `label`, `id` and `kind` that are non-empty strings.
|
||||
* @typedef {Readonly<{time: number, label?: string, id?: string, kind?: string}>} WorldClockMark
|
||||
*/
|
||||
|
||||
const MARK_TEXT = ['label', 'id', 'kind'];
|
||||
/**
|
||||
* The longest label a mark carries, ellipsis and all, in characters as a
|
||||
* reader sees them (graphemes where the runtime can tell them, otherwise code
|
||||
* points), so an emoji or an accented letter is never split.
|
||||
*/
|
||||
export const WORLD_CLOCK_MARK_LABEL_LENGTH = 60;
|
||||
|
||||
const graphemes =
|
||||
typeof Intl?.Segmenter === 'function'
|
||||
? (() => {
|
||||
const segmenter = new Intl.Segmenter(undefined, {
|
||||
granularity: 'grapheme',
|
||||
});
|
||||
return (text) =>
|
||||
Array.from(segmenter.segment(text), ({ segment }) => segment);
|
||||
})()
|
||||
: (text) => Array.from(text);
|
||||
|
||||
/** `mark` with its label cut to WORLD_CLOCK_MARK_LABEL_LENGTH, ending in `…`. */
|
||||
function shownMark(mark) {
|
||||
if (!mark.label || mark.label.length <= WORLD_CLOCK_MARK_LABEL_LENGTH)
|
||||
return mark;
|
||||
const parts = graphemes(mark.label);
|
||||
if (parts.length <= WORLD_CLOCK_MARK_LABEL_LENGTH) return mark;
|
||||
const label = `${parts
|
||||
.slice(0, WORLD_CLOCK_MARK_LABEL_LENGTH - 1)
|
||||
.join('')
|
||||
.trimEnd()}…`;
|
||||
return Object.freeze({ ...mark, label });
|
||||
}
|
||||
|
||||
/**
|
||||
* A mark (a number, or `{time, label?, id?, kind?}`) in its one shape, or
|
||||
* null for one with no finite time.
|
||||
* @param {unknown} input
|
||||
* @returns {WorldClockMark | null}
|
||||
*/
|
||||
function readMark(input) {
|
||||
if (typeof input === 'number')
|
||||
return Number.isFinite(input) ? Object.freeze({ time: input }) : null;
|
||||
if (!input || typeof input !== 'object') return null;
|
||||
const { time } = input;
|
||||
if (typeof time !== 'number' || !Number.isFinite(time)) return null;
|
||||
const mark = { time };
|
||||
for (const key of MARK_TEXT) {
|
||||
const value = input[key];
|
||||
if (typeof value === 'string' && value.trim()) mark[key] = value.trim();
|
||||
}
|
||||
return Object.freeze(mark);
|
||||
}
|
||||
|
||||
// Marks in time order; at one time, by kind, label and id, so the order
|
||||
// never depends on which source registered first.
|
||||
const byText = (a = '', b = '') => (a < b ? -1 : a > b ? 1 : 0);
|
||||
const byMark = (a, b) =>
|
||||
a.time - b.time ||
|
||||
byText(a.kind, b.kind) ||
|
||||
byText(a.label, b.label) ||
|
||||
byText(a.id, b.id);
|
||||
|
||||
/** The span a JS Date can hold, either side of 1970. */
|
||||
const MAX_DATE_MS = 8.64e15;
|
||||
|
||||
@@ -191,15 +265,17 @@ export function createWorldClock({
|
||||
jumps += 1;
|
||||
changed();
|
||||
},
|
||||
/** Back to the wall clock; playback stops and its rate goes back to 1. */
|
||||
/**
|
||||
* Back to the wall clock; playback stops. The rate stays as chosen, so
|
||||
* the next play, from wherever it starts, runs at it.
|
||||
*/
|
||||
goLive() {
|
||||
if (live && !playing && rate === 1) return;
|
||||
if (live && !playing) return;
|
||||
const stepping = active;
|
||||
live = true;
|
||||
cursorMs = null;
|
||||
playing = false;
|
||||
active = null;
|
||||
rate = 1;
|
||||
jumps += 1;
|
||||
// Its driven(false) finds nothing left to stop.
|
||||
stepping?.driver.pause();
|
||||
@@ -270,7 +346,10 @@ export function createWorldClock({
|
||||
if (ms === null || ms === undefined) gates.delete(id);
|
||||
else if (isWorldTime(ms)) gates.set(id, ms);
|
||||
},
|
||||
/** Simulated ms per real ms for continuous playback. */
|
||||
/**
|
||||
* Simulated ms per real ms for continuous playback. It holds while paused
|
||||
* and live, so any play (`play()` with no rate) runs at the one chosen.
|
||||
*/
|
||||
setRate(nextRate) {
|
||||
if (!Number.isFinite(nextRate) || nextRate <= 0)
|
||||
throw new RangeError('World clock rate must be positive');
|
||||
@@ -371,7 +450,16 @@ export function createWorldClock({
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* `marks` are the moments in that span worth going to (a launch, a strong
|
||||
* earthquake), drawn as ticks the timeline's arrow keys step to. Each is
|
||||
* an epoch ms number, or `{time, label?, id?, kind?}`: `label` is short
|
||||
* text the timeline shows for it (a name, `M6.4 · a place`; one longer
|
||||
* than WORLD_CLOCK_MARK_LABEL_LENGTH is cut to it, ending in `…`), `id` the
|
||||
* layer's own key for the event and `kind` what sort of event it is.
|
||||
* Numbers and objects may be mixed; a mark with no finite time is
|
||||
* ignored, as are a label, id or kind that is not a non-empty string.
|
||||
* @param {{id: string, coverage: () => ({start: number, end: number, marks?: Array<number | WorldClockMarkInput>} | null)}} source
|
||||
*/
|
||||
register(source) {
|
||||
// Each registration is its own, even under a shared id.
|
||||
@@ -386,20 +474,48 @@ export function createWorldClock({
|
||||
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. */
|
||||
/**
|
||||
* The union of the registered sources' coverage, or null with none:
|
||||
* `{start, end, marks, markDetails}`. `marks` is every mark's time, once
|
||||
* each, in order, as numbers. `markDetails` is the marks themselves, in
|
||||
* time order, each `{time, label?, id?, kind?}` (frozen): a mark several
|
||||
* sources give alike is there once, and a bare time is dropped where a
|
||||
* mark with more to say shares it.
|
||||
* @returns {{start: number, end: number, marks: number[], markDetails: WorldClockMark[]} | null}
|
||||
*/
|
||||
coverage() {
|
||||
let start = Infinity;
|
||||
let end = -Infinity;
|
||||
const marks = new Set();
|
||||
const details = new Map();
|
||||
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);
|
||||
for (const input of span.marks ?? []) {
|
||||
const mark = readMark(input);
|
||||
if (!mark) continue;
|
||||
const key = JSON.stringify([
|
||||
mark.time,
|
||||
mark.label,
|
||||
mark.id,
|
||||
mark.kind,
|
||||
]);
|
||||
if (!details.has(key)) details.set(key, mark);
|
||||
}
|
||||
}
|
||||
if (start > end) return null;
|
||||
return { start, end, marks: [...marks].sort((a, b) => a - b) };
|
||||
const told = new Set();
|
||||
for (const mark of details.values())
|
||||
if (Object.keys(mark).length > 1) told.add(mark.time);
|
||||
// Marks merge on their whole label, and are cut for showing after, so
|
||||
// two whose labels only differ past the cut stay two.
|
||||
const markDetails = [...details.values()]
|
||||
.filter((mark) => Object.keys(mark).length > 1 || !told.has(mark.time))
|
||||
.sort(byMark)
|
||||
.map(shownMark);
|
||||
const marks = [...new Set(markDetails.map((mark) => mark.time))];
|
||||
return { start, end, marks, markDetails };
|
||||
},
|
||||
|
||||
getState,
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createWorldClock, WORLD_CLOCK_TICK_MS } from './worldClock.js';
|
||||
import {
|
||||
createWorldClock,
|
||||
WORLD_CLOCK_MARK_LABEL_LENGTH,
|
||||
WORLD_CLOCK_TICK_MS,
|
||||
} from './worldClock.js';
|
||||
|
||||
function fakeTime() {
|
||||
const t = {
|
||||
@@ -253,9 +257,60 @@ test('coverage is the union of the registered sources', () => {
|
||||
start: 50,
|
||||
end: 200,
|
||||
marks: [100, 150, 200],
|
||||
markDetails: [{ time: 100 }, { time: 150 }, { time: 200 }],
|
||||
});
|
||||
off();
|
||||
assert.deepEqual(clock.coverage(), { start: 50, end: 180, marks: [] });
|
||||
assert.deepEqual(clock.coverage(), {
|
||||
start: 50,
|
||||
end: 180,
|
||||
marks: [],
|
||||
markDetails: [],
|
||||
});
|
||||
});
|
||||
|
||||
test('marks may carry a label, an id and a kind, and merge in one order', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
clock.register({
|
||||
id: 'launches',
|
||||
coverage: () => ({
|
||||
start: 100,
|
||||
end: 300,
|
||||
marks: [
|
||||
{ time: 300, label: ' ARTEMIS II ', id: 'a2', kind: 'launch' },
|
||||
100,
|
||||
{ time: 200, label: 'M6.4 · Fiji', kind: 'earthquake', extra: 1 },
|
||||
],
|
||||
}),
|
||||
});
|
||||
clock.register({
|
||||
id: 'others',
|
||||
coverage: () => ({
|
||||
start: 100,
|
||||
end: 300,
|
||||
marks: [
|
||||
200,
|
||||
{ time: 200, label: 'M6.4 · Fiji', kind: 'earthquake' },
|
||||
{ time: 200, label: 'ALPHA', kind: 'launch' },
|
||||
{ time: 250, label: '', id: 7, kind: null },
|
||||
{ time: Number.NaN, label: 'never' },
|
||||
Number.POSITIVE_INFINITY,
|
||||
null,
|
||||
'soon',
|
||||
],
|
||||
}),
|
||||
});
|
||||
const covered = clock.coverage();
|
||||
assert.deepEqual(covered.marks, [100, 200, 250, 300], 'the times, once each');
|
||||
assert.deepEqual(covered.markDetails, [
|
||||
{ time: 100 },
|
||||
// A bare 200 says nothing a labelled mark there does not; alike marks
|
||||
// from two sources are one.
|
||||
{ time: 200, label: 'M6.4 · Fiji', kind: 'earthquake' },
|
||||
{ time: 200, label: 'ALPHA', kind: 'launch' },
|
||||
{ time: 250 },
|
||||
{ time: 300, label: 'ARTEMIS II', id: 'a2', kind: 'launch' },
|
||||
]);
|
||||
assert.ok(covered.markDetails.every(Object.isFrozen));
|
||||
});
|
||||
|
||||
test('seek refuses anything but epoch ms and rate must be positive', () => {
|
||||
@@ -306,7 +361,7 @@ test('a coverage that throws counts as none', (t) => {
|
||||
assert.equal(clock.coverage(), null);
|
||||
});
|
||||
|
||||
test('continuous play runs past every driver, and live resets the rate', () => {
|
||||
test('continuous play runs past every driver, and live keeps the rate chosen', () => {
|
||||
const t = fakeTime();
|
||||
const clock = createWorldClock(t.options);
|
||||
const calls = [];
|
||||
@@ -317,7 +372,13 @@ test('continuous play runs past every driver, and live resets the rate', () => {
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), t.wall - 61_000 + 30_000);
|
||||
clock.goLive();
|
||||
assert.equal(clock.getRate(), 1);
|
||||
assert.equal(clock.getRate(), 30);
|
||||
assert.equal(clock.now(), t.wall, 'live is the wall clock, whatever the rate');
|
||||
// Play with no rate given runs at the one chosen.
|
||||
clock.seek(t.wall - 60_000);
|
||||
clock.play({ continuous: true });
|
||||
t.advance(1_000);
|
||||
assert.equal(clock.now(), t.wall - 61_000 + 30_000);
|
||||
});
|
||||
|
||||
test('continuous play holds while a layer waits, and goes on from there', () => {
|
||||
@@ -408,3 +469,68 @@ test('a seek or a return to live counts as a jump; play does not', () => {
|
||||
clock.goLive();
|
||||
assert.equal(clock.getState().jumps, 2, 'already live is no jump');
|
||||
});
|
||||
|
||||
test('a mark label longer than the timeline shows is cut, ending in an ellipsis', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
const long = `${'A'.repeat(58)} BEYOND THE CUT`;
|
||||
clock.register({
|
||||
id: 'long',
|
||||
coverage: () => ({
|
||||
start: 1,
|
||||
end: 2,
|
||||
marks: [
|
||||
{ time: 1, label: long },
|
||||
{ time: 2, label: 'B'.repeat(WORLD_CLOCK_MARK_LABEL_LENGTH) },
|
||||
],
|
||||
}),
|
||||
});
|
||||
const [cut, whole] = clock.coverage().markDetails;
|
||||
assert.equal(cut.label, `${'A'.repeat(58)}…`);
|
||||
assert.ok(cut.label.length <= WORLD_CLOCK_MARK_LABEL_LENGTH);
|
||||
assert.equal(whole.label, 'B'.repeat(WORLD_CLOCK_MARK_LABEL_LENGTH));
|
||||
});
|
||||
|
||||
test('a cut label never splits an emoji, and counts it as one character', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
const a = 'A'.repeat(58);
|
||||
clock.register({
|
||||
id: 'emoji',
|
||||
coverage: () => ({
|
||||
start: 1,
|
||||
end: 3,
|
||||
marks: [
|
||||
{ time: 1, label: `${a}🚀 LIFTOFF` },
|
||||
{ time: 2, label: `${a}👩🚀 CREW` },
|
||||
// Sixty characters, though more UTF-16 units: whole.
|
||||
{ time: 3, label: `${'A'.repeat(59)}🚀` },
|
||||
],
|
||||
}),
|
||||
});
|
||||
const [rocket, crew, whole] = clock.coverage().markDetails;
|
||||
assert.equal(rocket.label, `${a}🚀…`);
|
||||
assert.equal(crew.label, `${a}👩🚀…`);
|
||||
assert.equal(whole.label, `${'A'.repeat(59)}🚀`);
|
||||
for (const { label } of [rocket, crew, whole])
|
||||
assert.equal(label.isWellFormed(), true, 'no lone surrogate');
|
||||
});
|
||||
|
||||
test('marks merge on their whole label, so labels alike up to the cut stay two', () => {
|
||||
const clock = createWorldClock(fakeTime().options);
|
||||
const stem = 'C'.repeat(WORLD_CLOCK_MARK_LABEL_LENGTH - 1);
|
||||
for (const id of ['one', 'two'])
|
||||
clock.register({
|
||||
id,
|
||||
coverage: () => ({
|
||||
start: 5,
|
||||
end: 5,
|
||||
marks: [{ time: 5, label: `${stem}${id === 'one' ? ' FIRST' : ' SECOND'}` }],
|
||||
}),
|
||||
});
|
||||
const covered = clock.coverage();
|
||||
assert.deepEqual(covered.marks, [5]);
|
||||
assert.equal(covered.markDetails.length, 2);
|
||||
for (const mark of covered.markDetails) {
|
||||
assert.equal(mark.label, `${stem}…`);
|
||||
assert.equal(Object.isFrozen(mark), true);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user