Merge main through #1616 into the renderer seam

This commit is contained in:
Sameh Khamis committed 2026-11-09 23:42:15 -08:00
commit 9a2e2ecddd
82 files changed
+3594 -443

No files matched your search

+147 -21
View File
@@ -18,7 +18,15 @@ import { fromDegrees } from '../../geo/ellipsoid.js';
import { hexColor } from '../../render/color.js';
import { drawOf, findFrame } from '../../render/rendererOf.js';
import { readGhostMapsKml } from './kml.js';
import { fieldLabel, parseDescriptionFields, wrapCardText } from './records.js';
import {
fieldLabel,
GHOSTMAPS_AGE_OPTIONS,
GHOSTMAPS_DEFAULT_AGE_DAYS,
ghostMapsEventDate,
isGhostMapsEventExpired,
parseDescriptionFields,
wrapCardText,
} from './records.js';
import { inputFor as defaultInputFor } from '../../input/dispatcher.js';
export * from './catalog.js';
export * from './records.js';
@@ -29,6 +37,7 @@ export const GHOSTMAPS_LAYER_ID = 's2-ghostmaps';
export const GHOSTMAPS_OVERLAY_SOURCE_ID = 's2-ghostmaps';
const PICK_PREFIX = 'ghostmaps:';
const STORAGE_KEY = 'gev.ghostmaps.products';
const AGE_STORAGE_KEY = 'gev.ghostmaps.ageDays';
const ACCENT = '#c8ff00';
const MAX_ICON_BYTES = 512 * 1024;
const CARD_HOST_OPTIONS = Object.freeze({
@@ -85,6 +94,19 @@ function writeStoredSelection(storage, selected) {
}
}
function readStoredAge(storage) {
try {
const raw = storage?.getItem(AGE_STORAGE_KEY);
if (raw !== null && raw !== undefined) {
const value = JSON.parse(raw);
if (GHOSTMAPS_AGE_OPTIONS.includes(value)) return value;
}
} catch {
/* unavailable or invalid storage uses the default */
}
return GHOSTMAPS_DEFAULT_AGE_DAYS;
}
/** A round marker for placemarks whose icon is missing or remote. */
const markerCache = new Map();
function markerImage(color) {
@@ -163,6 +185,7 @@ export function createGhostMapsLayer({
picking = null,
openExternal = null,
storage = defaultStorage(),
now = () => Date.now(),
} = {}) {
if (typeof source?.getCatalog !== 'function')
throw new TypeError('GhostMaps layer requires a catalog source');
@@ -180,7 +203,9 @@ export function createGhostMapsLayer({
let _selectedLink = null;
let _lifetime = new AbortController();
let _selection = readStoredSelection(storage);
/** productId → {version, sets, urls, features, pins} */
let _ageDays = readStoredAge(storage);
let _expiryTimer = null;
/** productId → {version, ageDays, sets, urls, features: Map<index, record>, pins} */
const _loaded = new Map();
/** productId → in-flight version */
const _loading = new Map();
@@ -221,13 +246,36 @@ export function createGhostMapsLayer({
return (_catalog?.products || []).filter(wanted).map(({ id }) => id);
}
function unload(id) {
function unload(id, keepSelection = false) {
const item = _loaded.get(id);
if (!item) return;
_loaded.delete(id);
disposeSets(item.sets);
item.urls.forEach((url) => URL.revokeObjectURL(url));
if (_selectedKey?.startsWith(`${id}:`)) clearSelection();
if (!keepSelection && _selectedKey?.startsWith(`${id}:`)) clearSelection();
}
// Remove expired placemarks' items, rather than hiding them: horizon
// culling must never make an old pin visible again. Map keys keep
// surviving picks stable.
function expireEvents() {
const at = now();
let changed = false;
for (const [productId, item] of _loaded) {
for (const [index, feature] of item.features) {
if (!isGhostMapsEventExpired(feature.dateMs, at, _ageDays)) continue;
for (const [kind, ids] of Object.entries(feature.itemIds))
if (ids.length) item.sets?.[kind]?.remove(ids);
item.features.delete(index);
if (_selectedKey === `${productId}:${index}`) clearSelection();
changed = true;
}
item.pins = item.pins.filter((pin) => item.features.has(pin.index));
}
if (changed) {
notifyRow();
render();
}
}
function disposeSets(sets) {
@@ -274,13 +322,14 @@ export function createGhostMapsLayer({
const current = loaded?.sets
? Object.values(loaded.sets).some((set) => set?.id === hit.set)
: false;
return current && loaded.features[Number(index)]
return current && loaded.features.has(Number(index))
? `${productId}:${index}`
: null;
}
/** Build one product's draw items off-scene, then swap them in. */
async function load(product, signal) {
const ageDays = _ageDays;
const bytes = await source.getFile(product.id, product.version, { signal });
signal.throwIfAborted();
const entries = await readZipEntries(bytes);
@@ -310,8 +359,9 @@ export function createGhostMapsLayer({
const icons = [];
const lines = [];
const shapes = [];
const features = [];
const features = new Map();
const pins = [];
const at = now();
// Each distinct icon's own size, read once: icons draw at it, scaled.
const sizes = new Map();
const marker = markerImage(color);
@@ -328,8 +378,12 @@ export function createGhostMapsLayer({
for (const [image, size] of sizes) sizes.set(image, await size);
signal.throwIfAborted();
parsed.features.forEach((feature, index) => {
const dateMs = ghostMapsEventDate(feature.description);
if (isGhostMapsEventExpired(dateMs, at, ageDays)) return;
const key = `${product.id}:${index}`;
const { style } = feature;
// The items each placemark draws, by set, for expiry to take off.
const itemIds = { icons: [], lines: [], shapes: [] };
feature.geometries.forEach((geometry, part) => {
const id = `${PICK_PREFIX}${key}:${part}`;
if (geometry.type === 'point') {
@@ -355,23 +409,26 @@ export function createGhostMapsLayer({
// horizon cull hides the far side instead (see `_horizon`).
disableDepthTest: Number.POSITIVE_INFINITY,
};
const at = icons.push(item) - 1;
const slot = icons.push(item) - 1;
itemIds.icons.push(id);
// What the horizon cull turns: the icon as it draws now, in its
// edition's icon set once mounted.
pins.push({
index,
position,
set: null,
get item() {
return icons[at];
return icons[slot];
},
get show() {
return icons[at].show !== false;
return icons[slot].show !== false;
},
set show(value) {
icons[at] = { ...icons[at], show: value };
icons[slot] = { ...icons[slot], show: value };
},
});
} else if (geometry.type === 'line') {
itemIds.lines.push(id);
lines.push({
id,
positions: geometry.positions.map(([lon, lat]) =>
@@ -386,6 +443,7 @@ export function createGhostMapsLayer({
});
} else {
const ring = (points) => points.map(([lon, lat]) => ({ lon, lat }));
itemIds.shapes.push(id);
shapes.push({
id,
geometry: {
@@ -403,6 +461,7 @@ export function createGhostMapsLayer({
),
height: 'clampGround',
});
if (style.outline !== false) itemIds.lines.push(`${id}:outline`);
if (style.outline !== false)
lines.push({
id: `${id}:outline`,
@@ -418,15 +477,18 @@ export function createGhostMapsLayer({
});
}
});
features.push({
features.set(index, {
name: feature.name,
folder: feature.folder,
description: feature.description,
dateMs,
itemIds,
anchor: anchorOf(feature.geometries[0]),
});
});
return {
version: product.version,
ageDays,
items: { icons, lines, shapes },
sets: null,
urls,
@@ -443,6 +505,7 @@ export function createGhostMapsLayer({
function sync() {
const run = _queue.then(async () => {
if (!_enabled || !_viewer || !_catalog) return;
expireEvents();
const signal = _lifetime.signal;
const products = new Map(_catalog.products.map((p) => [p.id, p]));
for (const id of [..._loaded.keys()]) {
@@ -451,21 +514,33 @@ export function createGhostMapsLayer({
}
for (const product of _catalog.products) {
if (!wanted(product) || signal.aborted) continue;
if (_loaded.get(product.id)?.version === product.version) continue;
const loaded = _loaded.get(product.id);
if (loaded?.version === product.version && loaded.ageDays === _ageDays)
continue;
_loading.set(product.id, product.version);
_failed.delete(product.id);
notifyRow();
try {
const next = await load(product, signal);
if (signal.aborted || !_enabled) {
if (signal.aborted || !_enabled || next.ageDays !== _ageDays) {
next.urls.forEach((url) => URL.revokeObjectURL(url));
return;
}
// Cull before the first frame so far-side pins never flash.
_horizon.update(next.pins, { quiet: true });
mount(next);
// The new edition is on the globe before the old one leaves.
unload(product.id);
// The new edition is on the globe before the old one leaves. The card
// stays only if its index still holds the same report: the server
// may answer a pinned version with newer bytes.
const index = Number(_selectedKey?.split(':')[1]);
const before = loaded?.features.get(index);
const after = next.features.get(index);
unload(
product.id,
Boolean(before && after) &&
before.name === after.name &&
before.description === after.description,
);
_loaded.set(product.id, next);
} catch (error) {
if (signal.aborted) return;
@@ -504,12 +579,38 @@ export function createGhostMapsLayer({
void sync();
}
function setAgeDays(ageDays) {
if (!GHOSTMAPS_AGE_OPTIONS.includes(ageDays) || ageDays === _ageDays)
return;
_ageDays = ageDays;
try {
storage?.setItem(AGE_STORAGE_KEY, JSON.stringify(ageDays));
} catch {
/* private window or blocked storage: keep the session choice */
}
expireEvents();
// Narrowing only removes markers. A product loaded with a narrower window
// keeps its old ageDays, so sync() restores omitted placemarks from the
// cached KMZ even though its upstream version has not changed.
for (const [id, item] of _loaded)
if (
item.ageDays === null ||
(ageDays !== null && item.ageDays >= ageDays)
) {
item.ageDays = ageDays;
// A failed wider reload no longer matters; a stale edition retries.
_failed.delete(id);
}
notifyRow();
return sync();
}
// ---- selection card ----------------------------------------------------
function featureFor(key) {
const [productId, index] = key.split(':');
return {
product: _catalog?.products.find((p) => p.id === productId) || null,
feature: _loaded.get(productId)?.features[Number(index)] || null,
feature: _loaded.get(productId)?.features.get(Number(index)) || null,
};
}
@@ -643,7 +744,7 @@ export function createGhostMapsLayer({
function featureCount() {
let count = 0;
for (const item of _loaded.values()) count += item.features.length;
for (const item of _loaded.values()) count += item.features.size;
return count;
}
@@ -664,6 +765,8 @@ export function createGhostMapsLayer({
enable() {
_enabled = true;
_lifetime = new AbortController();
if (_expiryTimer === null)
_expiryTimer = setInterval(expireEvents, 60_000);
for (const item of _loaded.values())
for (const set of Object.values(item.sets ?? {})) set.show = true;
installClickHandler();
@@ -682,6 +785,8 @@ export function createGhostMapsLayer({
disable() {
_enabled = false;
clearInterval(_expiryTimer);
_expiryTimer = null;
_lifetime.abort();
_unsubscribe?.();
_unsubscribe = null;
@@ -695,6 +800,7 @@ export function createGhostMapsLayer({
async update(_viewerArg, { signal } = {}) {
if (!_enabled || signal?.aborted) return false;
expireEvents();
try {
const catalog = await source.getCatalog({ signal });
if (!_enabled) return false;
@@ -739,6 +845,25 @@ export function createGhostMapsLayer({
})
: t('Connecting to the S2 GhostMaps repository…');
return {
segmented: {
id: 'age',
label: t('Event age'),
options: GHOSTMAPS_AGE_OPTIONS.map((days) => ({
id: `age-${days ?? 'all'}`,
label:
days === null
? t('All', { context: 'event age filter' })
: t('{days}d', { days: formatNumber(days) }),
title:
days === null
? t('Show markers of every age')
: t('Maximum event age: {days}d; undated markers are kept', {
days: formatNumber(days),
}),
active: days === _ageDays,
onClick: () => setAgeDays(days),
})),
},
chips: products.map((product) => {
const loaded = _loaded.get(product.id);
const failed = _failed.get(product.id);
@@ -753,11 +878,11 @@ export function createGhostMapsLayer({
product.fileTitle,
loaded
? tn(
loaded.features.length,
loaded.features.size,
'{count} placemark',
'{count} placemarks',
{
count: formatNumber(loaded.features.length),
count: formatNumber(loaded.features.size),
},
)
: null,
@@ -769,7 +894,7 @@ export function createGhostMapsLayer({
};
}),
legend: [],
info,
info: `${_ageDays === null ? t('All markers') : t('Event age: {days}d · undated kept', { days: formatNumber(_ageDays) })} · ${info}`,
infoTitle: t(
'S2 Underground publishes these KMZ products to GitHub. New uploads are detected within seconds and replace the previous edition automatically. Click a marker for its report.',
),
@@ -779,6 +904,7 @@ export function createGhostMapsLayer({
getStats() {
return {
count: featureCount(),
ageDays: _ageDays,
products: [..._loaded.keys()],
commit: _catalog?.commit || null,
lastUpdate: _catalog?.committedAt || _catalog?.changedAt || null,
@@ -795,7 +921,7 @@ export function createGhostMapsLayer({
if (!_enabled) return [];
const out = [];
for (const [productId, item] of _loaded) {
for (const feature of item.features) {
for (const feature of item.features.values()) {
if (out.length >= maxCount) return out;
out.push({
product: productId,