Files
gods-eye-view/src/app/renderer.js
T
Sameh Khamis 317c3b6589 Create the application scene through a renderer plugin
createApplicationScene takes a renderer plugin, defaulting to Cesium,
which builds today's viewer and credits exactly as before. Its renderer
is transitional: it has only the members it really has, with read-only
capabilities, and a test pins the contract members still to come. Code
not yet ported reaches the viewer through cesiumViewerOf, never through
a renderer member. A plugin whose renderer has no viewer is refused and
destroyed, since the application still runs on the viewer. The result
now includes the renderer.

Not yet checked in a browser.
2026-11-05 14:51:42 -08:00

108 lines
3.5 KiB
JavaScript

/**
* @module app/renderer
* @description The application's default renderer plugin: today's Cesium
* viewer, set up exactly as before, behind the contract's `RendererPlugin`
* (`src/render/contract/renderer.d.ts`).
*
* The renderer it makes is transitional: it has only the members it really
* has, and the contract's ports arrive one layer family at a time (phase 2
* of the renderer plan); `checkRendererShape` lists what is still missing.
* Until a family is ported, its code reaches the Cesium viewer through
* `cesiumViewerOf`, never through a renderer member, so no engine type
* crosses the contract and each call marks code still to port.
*/
import { CONTRACT_VERSION } from '../render/contract/index.js';
import { createApplicationViewer } from './viewer.js';
/**
* The members a transitional renderer has today.
* @typedef {Pick<import('../render/contract/renderer').Renderer,
* 'id' | 'contractVersion' | 'capabilities' | 'canvas' | 'resize' | 'destroy'>}
* TransitionalRenderer
*/
const viewers = new WeakMap();
/** A Set no one can change once built, so `capabilities` stays true. */
export class ReadonlySet extends Set {
#sealed;
constructor(values = []) {
// Filled here, not by Set's constructor: it would call the guarded add
// before the private field exists.
super();
for (const value of values) super.add(value);
this.#sealed = true;
}
add(value) {
if (this.#sealed) throw new TypeError('capabilities are read-only');
return super.add(value);
}
delete() {
throw new TypeError('capabilities are read-only');
}
clear() {
throw new TypeError('capabilities are read-only');
}
}
/**
* The Cesium viewer behind a renderer this plugin made, or null for any
* renderer it did not make.
* @param {object} renderer
* @returns {object|null}
*/
export const cesiumViewerOf = (renderer) => viewers.get(renderer) ?? null;
/**
* The Cesium plugin over a viewer factory; tests pass a fake one.
* @param {{createViewer?: typeof createApplicationViewer}} [options]
*/
export function createCesiumRendererPlugin({
createViewer = createApplicationViewer,
} = {}) {
return Object.freeze({
id: 'cesium',
/**
* Credentials stay with the scene, which still loads the tiles; they
* move into the renderer with the globe port.
* @param {{container: HTMLElement, signal: AbortSignal}} options
* @returns {Promise<TransitionalRenderer>}
*/
async create({ container, signal }) {
signal?.throwIfAborted();
// Provider attribution stays visible, including clean-view and recording.
const creditContainer = document.createElement('div');
creditContainer.id = 'cesium-credits';
document.body.appendChild(creditContainer);
let viewer;
try {
viewer = createViewer({ container, creditContainer });
} catch (error) {
creditContainer.remove();
throw error;
}
const renderer = Object.freeze({
id: 'cesium',
contractVersion: CONTRACT_VERSION,
capabilities: new ReadonlySet(),
canvas: viewer.canvas,
resize() {
viewer.resize();
},
destroy() {
try {
if (!viewer.isDestroyed()) viewer.destroy();
} finally {
creditContainer.remove();
}
},
});
viewers.set(renderer, viewer);
return renderer;
},
});
}
/** The default plugin. */
export const cesiumRenderer = createCesiumRendererPlugin();