Files
gods-eye-view/src/globe/cesium/events.js
T
German Khokhlov aaa6074663 refactor(overlays): use the globe engine
The world overlay host takes its frames (frames.postRender), camera events
(camera.moveEnd), canvas size (host.size), projection matrix
(projector.viewProjection), horizon test (projector.horizonVisible) and
render requests (render.request('world-overlay')) from the globe engine.
initWorldOverlay accepts an engine or, for callers that still hold a
viewer, resolves the viewer's engine through engineFromViewer.

Contract: camera.position(result) becomes camera.livePosition, one
engine-owned object holding the current position, so per-frame readers
and paint lanes keep a live camera position without allocating; engine
events gain raiseValue for argument-list-free frame dispatch. Steady-state
frame allocation is unchanged (worker probe, 30 chunks: 2696/9536/38593
B/frame before and after; detection 473749 -> 408085).
2026-10-15 18:32:40 +04:00

60 lines
1.9 KiB
JavaScript

// Contract events in front of Cesium events. The Cesium subscription exists
// only while the contract event has listeners, so creating the engine adds
// nothing to the scene (Cesium does per-frame work for camera.changed only
// while someone listens).
import { createEngineEvent } from '../contract.js';
/**
* @param {{ addEventListener: (listener: Function) => () => void }} source A
* Cesium.Event (its addEventListener returns the remover the bridge uses).
* @param {((...args: any[]) => any[]) | null} [mapArgs] Cesium arguments →
* contract arguments.
* @param {{ unary?: (...args: any[]) => any }} [options] `unary` maps the
* Cesium arguments to the one contract argument without building an
* argument list (for per-frame events).
* @returns {{ event: import('../contract.js').EngineEvent, detach: () => void }}
*/
export function bridgeEvent(source, mapArgs, { unary } = {}) {
const event = createEngineEvent();
const forward = unary
? (a, b, c) => event.raiseValue(unary(a, b, c))
: mapArgs
? (...args) => event.raiseEvent(...mapArgs(...args))
: (...args) => event.raiseEvent(...args);
let removeForward = null;
let detached = false;
const sync = () => {
const wanted = !detached && event.numberOfListeners > 0;
if (wanted === (removeForward !== null)) return;
if (wanted) removeForward = source.addEventListener(forward);
else {
const remove = removeForward;
removeForward = null;
remove();
}
};
const add = event.addEventListener;
const remove = event.removeEventListener;
event.addEventListener = (listener) => {
const removeListener = add(listener);
sync();
return () => {
removeListener();
sync();
};
};
event.removeEventListener = (listener) => {
const removed = remove(listener);
sync();
return removed;
};
return {
event,
detach() {
detached = true;
sync();
},
};
}