From 35021d9f1019d08041be56f6bd6cadefc86502d4 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sat, 7 Nov 2026 04:27:27 -0800 Subject: [PATCH] Keep the radio layer's data source in its rendering part, and take the radio lifecycle off the Cesium list The radio lifecycle no longer makes, shows, hides or removes the stations' Cesium data source or the selected station's entity itself: the rendering part does, through installDataSource, setStationsShown, removeSelectionEntity and removeDataSource, in the same order as before. The camera factory import moves to the rendering part too, which stays on the list. The lifecycle no longer imports Cesium or drives the viewer, so it leaves the allow-list. --- scripts/format-scope.json | 1 + src/layers/radio/lifecycle.js | 26 ++----- src/layers/radio/lifecycle.test.mjs | 103 ++++++++++++++++++++++++++++ src/layers/radio/rendering.js | 25 +++++++ src/render/cesiumAllowList.json | 1 - 5 files changed, 135 insertions(+), 21 deletions(-) create mode 100644 src/layers/radio/lifecycle.test.mjs diff --git a/scripts/format-scope.json b/scripts/format-scope.json index 3e0aba992..7652b5ea2 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -635,6 +635,7 @@ "src/layers/radio/volume.js", "src/data/radio.js", "src/layers/radio/source.test.mjs", + "src/layers/radio/lifecycle.test.mjs", "src/layers/submarineCables/bundledSource.js", "src/layers/submarineCables/geometry.js", "src/layers/submarineCables/index.js", diff --git a/src/layers/radio/lifecycle.js b/src/layers/radio/lifecycle.js index 972cd2bfe..27e3a3c6f 100644 --- a/src/layers/radio/lifecycle.js +++ b/src/layers/radio/lifecycle.js @@ -1,7 +1,3 @@ -import * as Cesium from 'cesium'; -// The Cesium camera factory: given a viewer of a caller's own, with no -// renderer bound, the layer's flights and camera reads find a camera port. -import '../../render/cesium/camera.js'; import { RADIO_OVERLAY_SOURCE_ID, DEFAULT_RADIO_VOLUME, @@ -18,18 +14,14 @@ export function createLifecycle({ const { clearOverlaySource, setOverlaySourceVisible } = services.overlays; const methods = { - /** Initialize the Cesium data source and the single audio element. */ + /** Initialize the stations' rendering and the single audio element. */ init(viewer) { layerState._sessionGeneration += 1; layerState._viewer = viewer; parts.clustering.resetRadioClusterOverlayIdentities(); parts.playback.installAudio(); - if (!layerState._dataSource) { - layerState._dataSource = new Cesium.CustomDataSource('Radio stations'); - viewer.dataSources.add(layerState._dataSource); - parts.rendering.installClusterStyling(); - } - layerState._dataSource.show = false; + parts.rendering.installDataSource(viewer); + parts.rendering.setStationsShown(false); clearOverlaySource(RADIO_OVERLAY_SOURCE_ID); setOverlaySourceVisible(RADIO_OVERLAY_SOURCE_ID, false); }, @@ -58,10 +50,8 @@ export function createLifecycle({ layerState._cancelledTuningPresentationStation = null; layerState._tuningUnavailableStationId = null; parts.playback.stopRadioPlayback({ origin: 'layer-disable' }); - if (layerState._dataSource) layerState._dataSource.show = false; - if (layerState._selectedEntity && layerState._viewer) - layerState._viewer.entities.remove(layerState._selectedEntity); - layerState._selectedEntity = null; + parts.rendering.setStationsShown(false); + parts.rendering.removeSelectionEntity(); if (layerState._overlayPublishTimer) clearTimeout(layerState._overlayPublishTimer); layerState._overlayPublishTimer = null; @@ -86,11 +76,7 @@ export function createLifecycle({ if (layerState._audio) layerState._audio.volume = DEFAULT_RADIO_VOLUME; layerState._audio = null; layerState._userVolume = DEFAULT_RADIO_VOLUME; - layerState._removeClusterListener?.(); - layerState._removeClusterListener = null; - if (layerState._dataSource && layerState._viewer) - layerState._viewer.dataSources.remove(layerState._dataSource, true); - layerState._dataSource = null; + parts.rendering.removeDataSource(); clearOverlaySource(RADIO_OVERLAY_SOURCE_ID); layerState._stations = []; layerState._acceptedCatalogSnapshot = EMPTY_ACCEPTED_CATALOG_SNAPSHOT; diff --git a/src/layers/radio/lifecycle.test.mjs b/src/layers/radio/lifecycle.test.mjs new file mode 100644 index 000000000..d2e08d350 --- /dev/null +++ b/src/layers/radio/lifecycle.test.mjs @@ -0,0 +1,103 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { createLifecycle } from './lifecycle.js'; +import { createRendering } from './rendering.js'; +import { createState } from './state.js'; + +const overlays = () => ({ + clearOverlaySource() {}, + setOverlaySourceVisible() {}, + setOverlayEntries() {}, +}); + +/** A viewer whose data sources and entities record what was done to them. */ +function recordingViewer() { + const calls = []; + return { + calls, + dataSources: { + add: (source) => calls.push(['add', source]), + remove: (source, destroy) => calls.push(['remove', source, destroy]), + }, + entities: { remove: (entity) => calls.push(['removeEntity', entity]) }, + scene: { requestRender() {} }, + }; +} + +test('the stations data source is added once, clustered, and removed with its listener', () => { + const services = { + ground: { cachedGroundFloor: () => null }, + overlays: overlays(), + render: { governorRequestRender() {} }, + }; + const state = createState({ services }); + const rendering = createRendering({ state, services, parts: {} }); + const viewer = recordingViewer(); + state._viewer = viewer; + + rendering.installDataSource(viewer); + const source = state._dataSource; + assert.ok(source, 'a data source is made'); + assert.equal(source.name, 'Radio stations'); + assert.equal(source.clustering.enabled, true); + assert.equal(typeof state._removeClusterListener, 'function'); + rendering.installDataSource(viewer); + assert.deepEqual(viewer.calls, [['add', source]], 'added once'); + + rendering.setStationsShown(false); + assert.equal(source.show, false); + + rendering.removeDataSource(); + assert.deepEqual(viewer.calls.at(-1), ['remove', source, true]); + assert.equal(state._dataSource, null); + assert.equal(state._removeClusterListener, null); + assert.equal(source.clustering.clusterEvent.numberOfListeners, 0); +}); + +test('the lifecycle leaves the Cesium data source and entities to the rendering part', () => { + const services = { overlays: overlays() }; + const state = createState({ services }); + const done = []; + const record = + (name) => + (...args) => + done.push([name, ...args]); + const parts = { + clustering: { + resetRadioClusterOverlayIdentities() {}, + emptyRadioOverlayDiagnostics: () => ({}), + }, + playback: { installAudio() {}, stopRadioPlayback() {} }, + rendering: { + installDataSource: record('installDataSource'), + setStationsShown: record('setStationsShown'), + removeSelectionEntity: record('removeSelectionEntity'), + removeDataSource: record('removeDataSource'), + }, + interaction: { + syncRadioLifecyclePresentation() {}, + removeInteraction() {}, + }, + presentation: { emitState() {} }, + navigation: { invalidateRadioCameraNavigation() {} }, + tuning: { endRadioTuning() {} }, + volume: { cancelRadioVolumeTransition() {} }, + }; + const { methods } = createLifecycle({ state, services, parts }); + const viewer = recordingViewer(); + + methods.init(viewer); + assert.deepEqual(done, [ + ['installDataSource', viewer], + ['setStationsShown', false], + ]); + done.length = 0; + methods.destroy(); + assert.deepEqual(done, [ + ['setStationsShown', false], + ['removeSelectionEntity'], + ['removeDataSource'], + ]); + assert.deepEqual(viewer.calls, [], 'the lifecycle drives no viewer itself'); + assert.equal(state._viewer, null); +}); diff --git a/src/layers/radio/rendering.js b/src/layers/radio/rendering.js index 9f8613ff9..799b122de 100644 --- a/src/layers/radio/rendering.js +++ b/src/layers/radio/rendering.js @@ -1,4 +1,7 @@ import * as Cesium from 'cesium'; +// The Cesium camera factory: given a viewer of a caller's own, with no +// renderer bound, the layer's flights and camera reads find a camera port. +import '../../render/cesium/camera.js'; import { clusterPointCollection as clusterPointsOf } from '../../render/cesium/internals.js'; import { horizonOccluder } from '../../data/iconOrientation.js'; import { @@ -423,6 +426,26 @@ export function createRendering({ scheduleRadioOverlayPublish(); }); } + /** + * Add the stations' data source to the viewer, once, with its clustering. + * @param {object} viewer + */ + function installDataSource(viewer) { + if (layerState._dataSource) return; + layerState._dataSource = new Cesium.CustomDataSource('Radio stations'); + viewer.dataSources.add(layerState._dataSource); + installClusterStyling(); + } + + /** Stop styling clusters and take the data source off the viewer. */ + function removeDataSource() { + layerState._removeClusterListener?.(); + layerState._removeClusterListener = null; + if (layerState._dataSource && layerState._viewer) + layerState._viewer.dataSources.remove(layerState._dataSource, true); + layerState._dataSource = null; + } + /** Show or hide the stations' entities. */ function setStationsShown(shown) { if (layerState._dataSource) layerState._dataSource.show = shown; @@ -451,6 +474,8 @@ export function createRendering({ return { radioStationIdFromPick, + installDataSource, + removeDataSource, setStationsShown, removeSelectionEntity, reclusterStations, diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 23e3696a1..8abc20a1c 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -62,7 +62,6 @@ "src/layers/military/state.js", "src/layers/military/testing.js", "src/layers/military/tracking.js", - "src/layers/radio/lifecycle.js", "src/layers/radio/rendering.js", "src/layers/recentImagery/rendering.js", "src/layers/satellites/rendering.js",