mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75fe5b5703 | ||
|
|
b7552fbc64 |
No files matched your search
@@ -1,5 +0,0 @@
|
||||
- Fix rendering stopping for good after a wheel zoom with the pointer on the
|
||||
centre of a whole-globe view. Cesium's zoom could ask the camera to rotate
|
||||
about a zero axis; that rotation is now ignored, and a render loop stopped by
|
||||
any error is restarted from the last drawn camera pose, up to three times in
|
||||
ten seconds (mazy1998, #1704).
|
||||
@@ -0,0 +1,3 @@
|
||||
- Shorten the hitch when the Radio directory loads from about 450 ms to about
|
||||
180 ms. A station's tags and categories are worked out once instead of for
|
||||
every category check (mazy1998, #1709).
|
||||
@@ -207,7 +207,6 @@
|
||||
"modules": [
|
||||
"src/app/viewer.js",
|
||||
"src/app/atmosphereCompat.js",
|
||||
"src/app/renderRecovery.js",
|
||||
"src/globeInView.js",
|
||||
"src/sky/starrySky.js",
|
||||
"src/sky/stars.js",
|
||||
@@ -2611,7 +2610,6 @@
|
||||
"src/sky/starrySky.js",
|
||||
"src/sky/stars.js",
|
||||
"src/app/atmosphereCompat.js",
|
||||
"src/app/renderRecovery.js",
|
||||
"src/bloom.js",
|
||||
"src/camera.js",
|
||||
"src/cameraGroundGuard.js",
|
||||
|
||||
@@ -1,155 +0,0 @@
|
||||
/**
|
||||
* Keeps one bad frame from stopping the globe for good.
|
||||
*
|
||||
* Cesium's render loop stops itself on the first error a frame throws and
|
||||
* leaves an error panel over the canvas; nothing restarts it short of a
|
||||
* reload. Two things here:
|
||||
*
|
||||
* 1. `guardDegenerateRotation` removes one such error at its cause. Zooming
|
||||
* with the wheel above 1,000 km, Cesium turns the globe from the point under
|
||||
* the screen centre towards the point under the pointer
|
||||
* (`ScreenSpaceCameraController` `handleZoom`): it normalizes both points,
|
||||
* takes their dot product, and rotates about their cross product when the
|
||||
* dot product is below 1. With the pointer on the centre pixel the two
|
||||
* points are the same, but a unit vector's dot product with itself often
|
||||
* rounds to 0.9999999999999999, so the check passes and the cross product is
|
||||
* the zero vector. `Camera.rotate` then normalizes it and throws
|
||||
* "normalized result is not a number" from `Scene.initializeFrame`, which
|
||||
* runs outside the scene's own error handling. Builds without Cesium's
|
||||
* debug checks would write NaN into the camera instead.
|
||||
*
|
||||
* 2. `installRenderRecovery` restarts the loop after any error that stops it,
|
||||
* from the last pose a frame was drawn with, a bounded number of times.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import { governorRequestRender } from '../renderGovernor.js';
|
||||
|
||||
/** Restarts allowed within RECOVERY_WINDOW_MS before the error is left to stand. */
|
||||
export const RECOVERY_LIMIT = 3;
|
||||
/** An error that keeps coming back this fast is not one a restart gets past. */
|
||||
export const RECOVERY_WINDOW_MS = 10_000;
|
||||
|
||||
/**
|
||||
* Make `camera.rotate` ignore an axis it cannot normalize (zero or not finite)
|
||||
* and an angle that is not finite. There is nothing to rotate about, so the
|
||||
* camera stays where it is and the caller carries on; Cesium's wheel zoom
|
||||
* still moves the camera in after the skipped turn.
|
||||
* @param {Cesium.Camera} camera
|
||||
* @returns {() => void} Restore the camera's own rotate.
|
||||
*/
|
||||
export function guardDegenerateRotation(camera) {
|
||||
const own = Object.hasOwn(camera, 'rotate');
|
||||
const rotate = camera.rotate;
|
||||
const guarded = function (axis, angle) {
|
||||
if (axis) {
|
||||
const magnitudeSquared = Cesium.Cartesian3.magnitudeSquared(axis);
|
||||
if (
|
||||
!(magnitudeSquared > 0) ||
|
||||
!Number.isFinite(magnitudeSquared) ||
|
||||
(angle !== undefined && !Number.isFinite(angle))
|
||||
)
|
||||
return;
|
||||
}
|
||||
return rotate.call(this, axis, angle);
|
||||
};
|
||||
camera.rotate = guarded;
|
||||
return () => {
|
||||
if (camera.rotate !== guarded) return;
|
||||
if (own) camera.rotate = rotate;
|
||||
else delete camera.rotate;
|
||||
};
|
||||
}
|
||||
|
||||
function finite(cartesian) {
|
||||
return (
|
||||
Number.isFinite(cartesian?.x) &&
|
||||
Number.isFinite(cartesian?.y) &&
|
||||
Number.isFinite(cartesian?.z)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart Cesium's render loop when an error stops it.
|
||||
*
|
||||
* Cesium reports a stopped loop only through `showErrorPanel`: errors thrown
|
||||
* while a frame is prepared (camera input, in `Scene.initializeFrame`) never
|
||||
* reach `scene.renderError`, so that event alone would miss them. The panel
|
||||
* call is the one place both kinds of stop pass through, after the loop is
|
||||
* already off.
|
||||
*
|
||||
* On a stop this puts the camera back on the last pose a frame was drawn
|
||||
* with, discards the input that was being applied (Cesium skips its own reset
|
||||
* when the frame throws, so the same gesture would otherwise run again), and
|
||||
* turns the loop back on. After RECOVERY_LIMIT restarts inside
|
||||
* RECOVERY_WINDOW_MS the error is shown as before and rendering stays stopped.
|
||||
* @param {Cesium.Viewer} viewer
|
||||
* @param {object} [options]
|
||||
* @param {() => number} [options.now]
|
||||
* @param {(reason: string) => void} [options.requestRender]
|
||||
* @returns {() => void} Idempotent disposer.
|
||||
*/
|
||||
export function installRenderRecovery(
|
||||
viewer,
|
||||
{ now = () => performance.now(), requestRender = governorRequestRender } = {},
|
||||
) {
|
||||
const widget = viewer?.cesiumWidget;
|
||||
const scene = viewer?.scene;
|
||||
const camera = viewer?.camera;
|
||||
if (!widget?.showErrorPanel || !scene?.postRender || !camera)
|
||||
throw new TypeError('A complete Cesium viewer is required');
|
||||
|
||||
// The pose of the last frame that drew: three vector copies a frame.
|
||||
const position = new Cesium.Cartesian3();
|
||||
const direction = new Cesium.Cartesian3();
|
||||
const up = new Cesium.Cartesian3();
|
||||
let hasPose = false;
|
||||
const removePostRender = scene.postRender.addEventListener(() => {
|
||||
if (!finite(camera.position) || !finite(camera.direction)) return;
|
||||
Cesium.Cartesian3.clone(camera.position, position);
|
||||
Cesium.Cartesian3.clone(camera.direction, direction);
|
||||
Cesium.Cartesian3.clone(camera.up, up);
|
||||
hasPose = true;
|
||||
});
|
||||
|
||||
const recoveredAt = [];
|
||||
const own = Object.hasOwn(widget, 'showErrorPanel');
|
||||
const showErrorPanel = widget.showErrorPanel;
|
||||
const recovering = function (title, message, error) {
|
||||
const at = now();
|
||||
while (recoveredAt.length && at - recoveredAt[0] > RECOVERY_WINDOW_MS)
|
||||
recoveredAt.shift();
|
||||
// Only a stopped loop is restarted; other panels (a failed start-up, a
|
||||
// caller's own message) are shown as they are.
|
||||
if (
|
||||
viewer.useDefaultRenderLoop !== false ||
|
||||
recoveredAt.length >= RECOVERY_LIMIT
|
||||
)
|
||||
return showErrorPanel.call(this, title, message, error);
|
||||
recoveredAt.push(at);
|
||||
console.error(
|
||||
`[viewer] render loop stopped by an error; restarting (${recoveredAt.length} of ${RECOVERY_LIMIT})`,
|
||||
error,
|
||||
);
|
||||
if (hasPose) {
|
||||
Cesium.Cartesian3.clone(position, camera.position);
|
||||
Cesium.Cartesian3.clone(direction, camera.direction);
|
||||
Cesium.Cartesian3.clone(up, camera.up);
|
||||
Cesium.Cartesian3.cross(camera.direction, camera.up, camera.right);
|
||||
}
|
||||
// Cesium has no public way to drop the input a failed frame left queued.
|
||||
scene.screenSpaceCameraController?._aggregator?.reset?.();
|
||||
viewer.useDefaultRenderLoop = true;
|
||||
requestRender('render-recovery');
|
||||
};
|
||||
widget.showErrorPanel = recovering;
|
||||
|
||||
let active = true;
|
||||
return () => {
|
||||
if (!active) return;
|
||||
active = false;
|
||||
removePostRender?.();
|
||||
if (widget.showErrorPanel !== recovering) return;
|
||||
if (own) widget.showErrorPanel = showErrorPanel;
|
||||
else delete widget.showErrorPanel;
|
||||
};
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
guardDegenerateRotation,
|
||||
installRenderRecovery,
|
||||
RECOVERY_LIMIT,
|
||||
RECOVERY_WINDOW_MS,
|
||||
} from './renderRecovery.js';
|
||||
|
||||
/** Cesium's own Camera.rotate on a camera-shaped object, with no scene behind it. */
|
||||
function cameraFixture() {
|
||||
const camera = Object.create(Cesium.Camera.prototype);
|
||||
camera.position = new Cesium.Cartesian3(28_378_137, 0, 0);
|
||||
camera.direction = new Cesium.Cartesian3(-1, 0, 0);
|
||||
camera.up = new Cesium.Cartesian3(0, 0, 1);
|
||||
camera.right = new Cesium.Cartesian3(0, 1, 0);
|
||||
camera._adjustOrthographicFrustum = () => {};
|
||||
return camera;
|
||||
}
|
||||
|
||||
const pose = (camera) =>
|
||||
[camera.position, camera.direction, camera.up, camera.right].map((v) =>
|
||||
Cesium.Cartesian3.clone(v),
|
||||
);
|
||||
|
||||
test("the wheel zoom's own arithmetic hands Camera.rotate a zero axis", () => {
|
||||
// What handleZoom computes with the pointer on the centre pixel: the same
|
||||
// picked point twice. The dot product rounds below 1, the cross product is 0.
|
||||
const picked = Cesium.Cartesian3.fromDegrees(-41.11, -13.37);
|
||||
const pickedNormal = Cesium.Cartesian3.normalize(
|
||||
picked,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const centreNormal = Cesium.Cartesian3.normalize(
|
||||
picked,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const axis = Cesium.Cartesian3.cross(
|
||||
pickedNormal,
|
||||
centreNormal,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
assert.deepEqual([axis.x, axis.y, axis.z], [0, 0, 0]);
|
||||
|
||||
const unguarded = cameraFixture();
|
||||
assert.throws(() => unguarded.rotate(axis, 0.01), /not a number/);
|
||||
|
||||
const camera = cameraFixture();
|
||||
const before = pose(camera);
|
||||
guardDegenerateRotation(camera);
|
||||
assert.doesNotThrow(() => camera.rotate(axis, 0.01));
|
||||
assert.deepEqual(pose(camera), before);
|
||||
});
|
||||
|
||||
test('an axis or angle that is not finite leaves the camera where it is', () => {
|
||||
const camera = cameraFixture();
|
||||
const before = pose(camera);
|
||||
guardDegenerateRotation(camera);
|
||||
camera.rotate(new Cesium.Cartesian3(NaN, 0, 1), 0.01);
|
||||
camera.rotate(new Cesium.Cartesian3(Infinity, 0, 1), 0.01);
|
||||
camera.rotate(Cesium.Cartesian3.UNIT_Z, NaN);
|
||||
camera.rotate(Cesium.Cartesian3.UNIT_Z, Infinity);
|
||||
assert.deepEqual(pose(camera), before);
|
||||
});
|
||||
|
||||
test('an ordinary rotation is exactly what Cesium does unguarded', () => {
|
||||
const axis = new Cesium.Cartesian3(0.2, -0.5, 0.84);
|
||||
const plain = cameraFixture();
|
||||
plain.rotate(axis, 0.37);
|
||||
plain.rotate(Cesium.Cartesian3.UNIT_Z);
|
||||
|
||||
const camera = cameraFixture();
|
||||
const restore = guardDegenerateRotation(camera);
|
||||
camera.rotate(axis, 0.37);
|
||||
// No angle: Cesium's default amount still applies.
|
||||
camera.defaultRotateAmount = plain.defaultRotateAmount;
|
||||
camera.rotate(Cesium.Cartesian3.UNIT_Z);
|
||||
assert.deepEqual(pose(camera), pose(plain));
|
||||
|
||||
restore();
|
||||
assert.equal(Object.hasOwn(camera, 'rotate'), false);
|
||||
assert.equal(camera.rotate, Cesium.Camera.prototype.rotate);
|
||||
});
|
||||
|
||||
/** Vector equality as Cesium has it: a recomputed right vector may hold -0. */
|
||||
function assertPose(camera, expected) {
|
||||
pose(camera).forEach((vector, i) =>
|
||||
assert.ok(Cesium.Cartesian3.equals(vector, expected[i]), `vector ${i}`),
|
||||
);
|
||||
}
|
||||
|
||||
function viewerFixture() {
|
||||
const postRender = new Cesium.Event();
|
||||
const panels = [];
|
||||
const resets = [];
|
||||
const camera = cameraFixture();
|
||||
const viewer = {
|
||||
camera,
|
||||
useDefaultRenderLoop: true,
|
||||
cesiumWidget: {
|
||||
showErrorPanel(title, message, error) {
|
||||
panels.push({ title, message, error });
|
||||
},
|
||||
},
|
||||
scene: {
|
||||
postRender,
|
||||
screenSpaceCameraController: {
|
||||
_aggregator: { reset: () => resets.push(true) },
|
||||
},
|
||||
},
|
||||
};
|
||||
// What Cesium's loop does when a frame throws.
|
||||
const fail = (error = new Error('frame failed')) => {
|
||||
viewer.useDefaultRenderLoop = false;
|
||||
viewer.cesiumWidget.showErrorPanel('Rendering has stopped.', undefined, error);
|
||||
};
|
||||
return { viewer, camera, postRender, panels, resets, fail };
|
||||
}
|
||||
|
||||
function quietly(run) {
|
||||
const original = console.error;
|
||||
const logged = [];
|
||||
console.error = (...args) => logged.push(args);
|
||||
try {
|
||||
run();
|
||||
} finally {
|
||||
console.error = original;
|
||||
}
|
||||
return logged;
|
||||
}
|
||||
|
||||
test('a stopped render loop restarts from the last drawn pose', () => {
|
||||
const { viewer, camera, postRender, panels, resets, fail } = viewerFixture();
|
||||
const requests = [];
|
||||
installRenderRecovery(viewer, {
|
||||
now: () => 0,
|
||||
requestRender: (reason) => requests.push(reason),
|
||||
});
|
||||
postRender.raiseEvent();
|
||||
const drawn = pose(camera);
|
||||
|
||||
// The failed frame left the camera unusable.
|
||||
camera.position.x = NaN;
|
||||
camera.direction.y = NaN;
|
||||
const error = new Error('normalized result is not a number');
|
||||
const logged = quietly(() => fail(error));
|
||||
|
||||
assert.equal(viewer.useDefaultRenderLoop, true);
|
||||
assert.deepEqual(panels, []);
|
||||
assertPose(camera, drawn);
|
||||
assert.deepEqual(resets, [true]);
|
||||
assert.deepEqual(requests, ['render-recovery']);
|
||||
assert.equal(logged.length, 1);
|
||||
assert.equal(logged[0][1], error);
|
||||
});
|
||||
|
||||
test('a frame drawn with a broken camera is not kept as the pose to return to', () => {
|
||||
const { viewer, camera, postRender, fail } = viewerFixture();
|
||||
installRenderRecovery(viewer, { now: () => 0, requestRender: () => {} });
|
||||
postRender.raiseEvent();
|
||||
const drawn = pose(camera);
|
||||
camera.position.x = NaN;
|
||||
postRender.raiseEvent();
|
||||
quietly(() => fail());
|
||||
assertPose(camera, drawn);
|
||||
});
|
||||
|
||||
test('an error that keeps returning is shown and rendering stays stopped', () => {
|
||||
const { viewer, panels, fail } = viewerFixture();
|
||||
let at = 0;
|
||||
installRenderRecovery(viewer, { now: () => at, requestRender: () => {} });
|
||||
quietly(() => {
|
||||
for (let i = 0; i < RECOVERY_LIMIT; i++) {
|
||||
at += 100;
|
||||
fail();
|
||||
assert.equal(viewer.useDefaultRenderLoop, true, `restart ${i + 1}`);
|
||||
}
|
||||
at += 100;
|
||||
fail();
|
||||
});
|
||||
assert.equal(viewer.useDefaultRenderLoop, false);
|
||||
assert.equal(panels.length, 1);
|
||||
assert.equal(panels[0].title, 'Rendering has stopped.');
|
||||
|
||||
// Once the window has passed, a later, unrelated error is recovered again.
|
||||
at += RECOVERY_WINDOW_MS + 1;
|
||||
quietly(() => fail());
|
||||
assert.equal(viewer.useDefaultRenderLoop, true);
|
||||
assert.equal(panels.length, 1);
|
||||
});
|
||||
|
||||
test('a panel shown while the loop is running is left alone', () => {
|
||||
const { viewer, panels, resets } = viewerFixture();
|
||||
installRenderRecovery(viewer, { now: () => 0, requestRender: () => {} });
|
||||
viewer.cesiumWidget.showErrorPanel('Error constructing CesiumWidget.', 'x');
|
||||
assert.equal(panels.length, 1);
|
||||
assert.deepEqual(resets, []);
|
||||
});
|
||||
|
||||
test('disposing restores the widget and stops recording poses', () => {
|
||||
const { viewer, postRender, panels, fail } = viewerFixture();
|
||||
const original = viewer.cesiumWidget.showErrorPanel;
|
||||
const dispose = installRenderRecovery(viewer, {
|
||||
now: () => 0,
|
||||
requestRender: () => {},
|
||||
});
|
||||
assert.equal(postRender.numberOfListeners, 1);
|
||||
dispose();
|
||||
dispose();
|
||||
assert.equal(postRender.numberOfListeners, 0);
|
||||
assert.equal(viewer.cesiumWidget.showErrorPanel, original);
|
||||
fail();
|
||||
assert.equal(viewer.useDefaultRenderLoop, false);
|
||||
assert.equal(panels.length, 1);
|
||||
});
|
||||
|
||||
test('an incomplete viewer is refused', () => {
|
||||
assert.throws(() => installRenderRecovery({}), TypeError);
|
||||
});
|
||||
@@ -1,9 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { applyModelAtmosphereWorkaround } from './atmosphereCompat.js';
|
||||
import {
|
||||
guardDegenerateRotation,
|
||||
installRenderRecovery,
|
||||
} from './renderRecovery.js';
|
||||
import { StarrySky } from '../sky/starrySky.js';
|
||||
import {
|
||||
FREE_LOOK_MAX_HEIGHT_METERS,
|
||||
@@ -419,11 +415,6 @@ export function createApplicationViewer({ container, creditContainer }) {
|
||||
if (globalThis.matchMedia?.('(pointer: coarse)').matches)
|
||||
viewer.scene.screenSpaceCameraController.zoomFactor *= 3;
|
||||
limitZoomOut(viewer);
|
||||
// Cesium's wheel zoom can hand the camera a zero rotation axis and stop
|
||||
// the render loop for good; and whatever else stops it, restart it rather
|
||||
// than leave the globe dead until a reload. See app/renderRecovery.js.
|
||||
guardDegenerateRotation(viewer.camera);
|
||||
installRenderRecovery(viewer);
|
||||
// Before any tile builds a draw command: Cesium's per-vertex model
|
||||
// atmosphere fails to LINK on Apple's Metal backend and kills the
|
||||
// render loop. See app/atmosphereCompat.js.
|
||||
|
||||
@@ -27,42 +27,52 @@ export function createCategories({
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
function hasTag(station, needles) {
|
||||
const tags = stationTags(station);
|
||||
return needles.some((needle) =>
|
||||
const tagsInclude = (tags, needles) =>
|
||||
needles.some((needle) =>
|
||||
tags.some((tag) => tag === needle || tag.includes(needle)),
|
||||
);
|
||||
|
||||
// A station's tags decide its categories, and neither changes while the
|
||||
// station record lives. Matching used to normalise every tag again for
|
||||
// every needle of every category of every station: about 0.9 s and a
|
||||
// 445 ms freeze per directory load of 750 stations, repeated for the active
|
||||
// filter on every horizon scan. Keyed by the record and its tags value, so
|
||||
// a record given new tags is read again.
|
||||
const membershipByStation = new WeakMap();
|
||||
|
||||
function stationMembership(station) {
|
||||
const keyed = station !== null && typeof station === 'object';
|
||||
const known = keyed ? membershipByStation.get(station) : null;
|
||||
if (known && known.source === station.tags) return known;
|
||||
const tags = stationTags(station);
|
||||
const genres = MUSIC_GENRES.filter(([genre]) =>
|
||||
tagsInclude(tags, [genre]),
|
||||
).map(([genre]) => genre);
|
||||
const categories = new Set(genres.map((genre) => `genre:${genre}`));
|
||||
for (const [id, needles] of Object.entries(CATEGORY_MATCHERS))
|
||||
if (tagsInclude(tags, needles)) categories.add(id);
|
||||
const sorted = categories.size > genres.length;
|
||||
if (genres.length > 0 || tagsInclude(tags, ['music', 'hits', 'songs']))
|
||||
categories.add('music');
|
||||
else if (!sorted) categories.add('other');
|
||||
const membership = { source: station?.tags, tags, genres, categories };
|
||||
if (keyed) membershipByStation.set(station, membership);
|
||||
return membership;
|
||||
}
|
||||
|
||||
function hasTag(station, needles) {
|
||||
return tagsInclude(stationMembership(station).tags, needles);
|
||||
}
|
||||
|
||||
function detectedGenres(station) {
|
||||
return MUSIC_GENRES.filter(([genre]) => hasTag(station, [genre])).map(
|
||||
([genre]) => genre,
|
||||
);
|
||||
return [...stationMembership(station).genres];
|
||||
}
|
||||
|
||||
/** Return whether a station belongs in a station-tag category. */
|
||||
|
||||
function stationMatchesRadioCategory(station, categoryId) {
|
||||
if (categoryId === 'all') return true;
|
||||
if (categoryId.startsWith('genre:')) {
|
||||
return detectedGenres(station).includes(
|
||||
categoryId.slice('genre:'.length),
|
||||
);
|
||||
}
|
||||
if (categoryId === 'music') {
|
||||
return (
|
||||
detectedGenres(station).length > 0 ||
|
||||
hasTag(station, ['music', 'hits', 'songs'])
|
||||
);
|
||||
}
|
||||
if (categoryId === 'other') {
|
||||
return (
|
||||
!Object.entries(CATEGORY_MATCHERS).some(([id]) =>
|
||||
stationMatchesRadioCategory(station, id),
|
||||
) && !stationMatchesRadioCategory(station, 'music')
|
||||
);
|
||||
}
|
||||
return hasTag(station, CATEGORY_MATCHERS[categoryId] || []);
|
||||
return stationMembership(station).categories.has(categoryId);
|
||||
}
|
||||
|
||||
/** Build canonical and detected-genre categories from station-level tags. */
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createCategories } from './categories.js';
|
||||
import { CATEGORY_MATCHERS, MUSIC_GENRES } from './policy.js';
|
||||
|
||||
const categories = createCategories({
|
||||
state: {},
|
||||
services: {},
|
||||
parts: { model: { radioCategoryColor: (id) => `color:${id}` } },
|
||||
source: {},
|
||||
});
|
||||
|
||||
// Category matching as it was before memberships were remembered: every call
|
||||
// normalises the station's tags again. Counts and filters must stay equal.
|
||||
const normalize = (value) =>
|
||||
String(value ?? '')
|
||||
.trim()
|
||||
.toLocaleLowerCase()
|
||||
.replace(/[_-]+/g, ' ')
|
||||
.replace(/\s+/g, ' ')
|
||||
.slice(0, 80);
|
||||
const tagsOf = (station) =>
|
||||
(Array.isArray(station?.tags)
|
||||
? station.tags
|
||||
: String(station?.tags ?? '').split(',')
|
||||
)
|
||||
.map(normalize)
|
||||
.filter(Boolean);
|
||||
const hasTag = (station, needles) => {
|
||||
const tags = tagsOf(station);
|
||||
return needles.some((needle) =>
|
||||
tags.some((tag) => tag === needle || tag.includes(needle)),
|
||||
);
|
||||
};
|
||||
const genresOf = (station) =>
|
||||
MUSIC_GENRES.filter(([genre]) => hasTag(station, [genre])).map(
|
||||
([genre]) => genre,
|
||||
);
|
||||
function matchesByRescan(station, categoryId) {
|
||||
if (categoryId === 'all') return true;
|
||||
if (categoryId.startsWith('genre:'))
|
||||
return genresOf(station).includes(categoryId.slice('genre:'.length));
|
||||
if (categoryId === 'music')
|
||||
return (
|
||||
genresOf(station).length > 0 ||
|
||||
hasTag(station, ['music', 'hits', 'songs'])
|
||||
);
|
||||
if (categoryId === 'other')
|
||||
return (
|
||||
!Object.keys(CATEGORY_MATCHERS).some((id) =>
|
||||
matchesByRescan(station, id),
|
||||
) && !matchesByRescan(station, 'music')
|
||||
);
|
||||
return hasTag(station, CATEGORY_MATCHERS[categoryId] || []);
|
||||
}
|
||||
|
||||
const CATEGORY_IDS = [
|
||||
'all',
|
||||
...Object.keys(CATEGORY_MATCHERS),
|
||||
'music',
|
||||
...MUSIC_GENRES.map(([genre]) => `genre:${genre}`),
|
||||
'other',
|
||||
'genre:not-a-genre',
|
||||
'not-a-category',
|
||||
];
|
||||
|
||||
function seeded(seed) {
|
||||
let state = seed >>> 0;
|
||||
return () => {
|
||||
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
||||
return state / 2 ** 32;
|
||||
};
|
||||
}
|
||||
|
||||
/** Stations tagged the way the directory tags them, plus the odd shapes. */
|
||||
function directory(count = 1500) {
|
||||
const random = seeded(11);
|
||||
const vocabulary = [
|
||||
...MUSIC_GENRES.map(([genre]) => genre),
|
||||
...Object.values(CATEGORY_MATCHERS).flat(),
|
||||
'Hip-Hop',
|
||||
'R&B',
|
||||
'Top_Hits',
|
||||
'songs of praise',
|
||||
'community',
|
||||
'college',
|
||||
'varied',
|
||||
'Police Scanner',
|
||||
'FIREFIGHTERS',
|
||||
'metropolitan',
|
||||
'x'.repeat(100),
|
||||
'',
|
||||
' ',
|
||||
];
|
||||
const pick = () => vocabulary[Math.floor(random() * vocabulary.length)];
|
||||
const stations = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
const tags = Array.from({ length: Math.floor(random() * 6) }, pick);
|
||||
stations.push({
|
||||
id: `station-${i}`,
|
||||
tags: i % 5 === 0 ? tags.join(',') : i % 7 === 0 ? Object.freeze(tags) : tags,
|
||||
});
|
||||
}
|
||||
stations.push(
|
||||
{ id: 'no-tags' },
|
||||
{ id: 'null-tags', tags: null },
|
||||
{ id: 'number-tags', tags: 42 },
|
||||
Object.freeze({ id: 'frozen', tags: Object.freeze(['Jazz', 'News']) }),
|
||||
null,
|
||||
undefined,
|
||||
'pop',
|
||||
);
|
||||
return stations;
|
||||
}
|
||||
|
||||
test('remembered memberships give the counts and filters of a full rescan', () => {
|
||||
const stations = directory();
|
||||
for (const id of CATEGORY_IDS) {
|
||||
const expected = stations.filter((station) => matchesByRescan(station, id));
|
||||
assert.deepEqual(
|
||||
categories.filterRadioStations(stations, id),
|
||||
expected,
|
||||
`filter ${id}`,
|
||||
);
|
||||
for (const station of stations)
|
||||
assert.equal(
|
||||
categories.stationMatchesRadioCategory(station, id),
|
||||
matchesByRescan(station, id),
|
||||
);
|
||||
}
|
||||
const built = categories.buildRadioCategories(stations);
|
||||
assert.deepEqual(
|
||||
built.map(({ id, count }) => [id, count]),
|
||||
CATEGORY_IDS.filter(
|
||||
(id) =>
|
||||
!id.startsWith('genre:') ||
|
||||
stations.some((station) => matchesByRescan(station, id)),
|
||||
)
|
||||
.filter((id) => id !== 'not-a-category')
|
||||
.map((id) => [
|
||||
id,
|
||||
stations.filter((station) => matchesByRescan(station, id)).length,
|
||||
]),
|
||||
);
|
||||
assert.ok(built.find(({ id }) => id === 'other').count > 0);
|
||||
assert.ok(built.find(({ id }) => id === 'genre:hip hop').count > 0);
|
||||
for (const station of stations) {
|
||||
assert.deepEqual(categories.detectedGenres(station), genresOf(station));
|
||||
assert.deepEqual(categories.stationTags(station), tagsOf(station));
|
||||
}
|
||||
});
|
||||
|
||||
test('a station has its tags normalised once, however often it is matched', () => {
|
||||
let reads = 0;
|
||||
const tag = (text) => ({
|
||||
toString() {
|
||||
reads++;
|
||||
return text;
|
||||
},
|
||||
});
|
||||
const stations = [
|
||||
{ id: 'a', tags: [tag('Jazz'), tag('News')] },
|
||||
{ id: 'b', tags: [tag('community')] },
|
||||
];
|
||||
categories.buildRadioCategories(stations);
|
||||
for (const id of CATEGORY_IDS) categories.filterRadioStations(stations, id);
|
||||
// A horizon scan asks once per station for the active filter.
|
||||
for (let scan = 0; scan < 50; scan++)
|
||||
for (const station of stations)
|
||||
categories.stationMatchesRadioCategory(station, 'news');
|
||||
assert.equal(reads, 3, 'one normalisation per tag');
|
||||
assert.deepEqual(
|
||||
categories.filterRadioStations(stations, 'genre:jazz').map(({ id }) => id),
|
||||
['a'],
|
||||
);
|
||||
});
|
||||
|
||||
test('a station record given new tags is matched on the new tags', () => {
|
||||
const station = { id: 'a', tags: ['jazz'] };
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'music'), true);
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'news'), false);
|
||||
station.tags = ['news'];
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'music'), false);
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'news'), true);
|
||||
assert.deepEqual(categories.detectedGenres(station), []);
|
||||
station.tags = 'community';
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'other'), true);
|
||||
});
|
||||
|
||||
test('callers cannot alter a remembered genre list', () => {
|
||||
const station = { id: 'a', tags: ['jazz', 'soul'] };
|
||||
categories.detectedGenres(station).push('rock');
|
||||
assert.deepEqual(categories.detectedGenres(station), ['jazz', 'soul']);
|
||||
assert.equal(
|
||||
categories.stationMatchesRadioCategory(station, 'genre:rock'),
|
||||
false,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user