Keep ALPR marker state in a layer store instead of on Cesium entities

The ALPR layer's private per-entity fields (record, clamp, canvas and display
anchors, wedge, native appearance, pick position, shown, surface check) move
from gevAlpr* expandos into alprMarkers, a store made by the new
createEntityStore(). The shared entity records are built on the same factory.
This commit is contained in:
Sameh Khamis committed 2026-11-03 08:24:33 -08:00
1 parent 65587f6c08
commit 393a145137
9 files changed
+144 -75

No files matched your search

+5 -4
View File
@@ -32,6 +32,7 @@ import {
normalizeAlprNode,
} from './alprCameras.js';
import { readEntityRecord } from './entityRecords.js';
import { alprMarkers } from '../layers/alpr/markerState.js';
test('normalizeAlprNode: maps a full OSM node to a plain record', () => {
const record = normalizeAlprNode({
@@ -1445,12 +1446,12 @@ test('map-source changes reposition all ALPR entities and saved native appearanc
await layer.update();
const entities = [...h.source.entities.values];
const saved = entities[1];
saved.gevAlprNativeAppearance = {
alprMarkers.write(saved).nativeAppearance = {
position: saved.position,
heightReference: saved.billboard.heightReference,
};
saved.position = Cesium.Cartesian3.fromDegrees(-97.7431, 30.26721, 190);
saved.gevAlprPickPosition = saved.position;
alprMarkers.write(saved).pickPosition = saved.position;
for (const next of [150, 120, null, 180]) {
floor = next;
// Includes a provider switch with the same globe.show value.
@@ -1471,10 +1472,10 @@ test('map-source changes reposition all ALPR entities and saved native appearanc
? Cesium.HeightReference.CLAMP_TO_GROUND
: Cesium.HeightReference.NONE,
);
assert.equal(entity.gevAlprCanvasPosition, null);
assert.equal(alprMarkers.read(entity)?.canvasPosition, null);
}
assert.equal(
saved.gevAlprNativeAppearance,
alprMarkers.read(saved)?.nativeAppearance,
null,
'saved appearance restores at the new floor',
);
+28 -9
View File
@@ -12,9 +12,34 @@
* - `visualPosition()`: where a 3D model is actually drawn, for the tracked card.
* - `labelModel`: the tracked card's `{ title, details, accent, … }`.
* - `selectionOrigin`: what started the selection (`'user'`, …).
*
* A layer's private per-entity state goes in its own store ({@link createEntityStore}).
*/
const records = new WeakMap();
/**
* A store of one record per renderer object, held by the app and dropped with the object.
* @returns {{ write: (entity: Object) => Object, read: (entity: unknown) => Object|null }}
* `write` makes the record empty on first use; `read` gives null when there is none.
*/
export function createEntityStore() {
const records = new WeakMap();
return {
write(entity) {
let record = records.get(entity);
if (!record) {
record = {};
records.set(entity, record);
}
return record;
},
read(entity) {
if (!entity || typeof entity !== 'object') return null;
return records.get(entity) ?? null;
},
};
}
const shared = createEntityStore();
/**
* The record for `entity`, made empty on first use. For layers writing their own state.
@@ -22,12 +47,7 @@ const records = new WeakMap();
* @returns {Object}
*/
export function entityRecord(entity) {
let record = records.get(entity);
if (!record) {
record = {};
records.set(entity, record);
}
return record;
return shared.write(entity);
}
/**
@@ -36,8 +56,7 @@ export function entityRecord(entity) {
* @returns {Object|null}
*/
export function readEntityRecord(entity) {
if (!entity || typeof entity !== 'object') return null;
return records.get(entity) ?? null;
return shared.read(entity);
}
/**
+18 -1
View File
@@ -1,6 +1,11 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { entityRecord, readEntityRecord, withEntityRecord } from './entityRecords.js';
import {
createEntityStore,
entityRecord,
readEntityRecord,
withEntityRecord,
} from './entityRecords.js';
test('an entity record is made on first write and read back by the same entity', () => {
const entity = {};
@@ -29,3 +34,15 @@ test('withEntityRecord writes fields into the record and returns the entity', ()
assert.equal(readEntityRecord(entity)?.trackedId, 'alpr:42');
assert.equal(Object.hasOwn(entity, 'trackedId'), false);
});
test('a layer store is separate from the shared records and from other stores', () => {
const entity = {};
const a = createEntityStore();
const b = createEntityStore();
a.write(entity).canvasPosition = 1;
assert.equal(a.read(entity)?.canvasPosition, 1);
assert.equal(b.read(entity), null);
assert.equal(readEntityRecord(entity), null);
assert.equal(a.read(null), null);
assert.deepEqual(Object.keys(entity), []);
});