mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-12 14:22:21 +00:00
refactor(cyclones): drop defensive shared-context state and checks
This commit is contained in:
1 parent
5e64e599e1
commit
5bab5ec26d
3 files changed
+10
-32
No files matched your search
@@ -3,9 +3,8 @@ import * as context from '../../data/contextStore.js';
|
|||||||
|
|
||||||
/** Wire cyclone selection to the application shared context. */
|
/** Wire cyclone selection to the application shared context. */
|
||||||
export function createApplicationCyclones(options) {
|
export function createApplicationCyclones(options) {
|
||||||
const browserContext = typeof window !== 'undefined' ? { context } : {};
|
|
||||||
return createCyclonesLayer({
|
return createCyclonesLayer({
|
||||||
...browserContext,
|
context: typeof window !== 'undefined' ? context : null,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -84,7 +84,6 @@ export function createCyclonesLayer({
|
|||||||
selectionIntent = 'auto',
|
selectionIntent = 'auto',
|
||||||
navigationGeneration = 0,
|
navigationGeneration = 0,
|
||||||
selectedContextStormId = null,
|
selectedContextStormId = null,
|
||||||
selectedContextEntity = null,
|
|
||||||
clickHandler = null,
|
clickHandler = null,
|
||||||
removeClickCapture = null;
|
removeClickCapture = null;
|
||||||
let enabled = false,
|
let enabled = false,
|
||||||
@@ -98,20 +97,19 @@ export function createCyclonesLayer({
|
|||||||
function clearSelectedContext({ evicted = false } = {}) {
|
function clearSelectedContext({ evicted = false } = {}) {
|
||||||
if (selectedContextStormId) {
|
if (selectedContextStormId) {
|
||||||
if (!evicted)
|
if (!evicted)
|
||||||
context?.clearSelectedEntityContextForLayer?.('weather-cyclones');
|
context.clearSelectedEntityContextForLayer('weather-cyclones');
|
||||||
context?.removeEntityContextsForLayer?.('weather-cyclones');
|
context.removeEntityContextsForLayer('weather-cyclones');
|
||||||
}
|
}
|
||||||
selectedContextStormId = null;
|
selectedContextStormId = null;
|
||||||
selectedContextEntity = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callers publish only while selectedId names a storm in the snapshot.
|
// Callers publish only while selectedId names a storm in the snapshot.
|
||||||
function publishSelectedContext({ announce = false } = {}) {
|
function publishSelectedContext({ announce = false } = {}) {
|
||||||
const storm = selected();
|
const storm = selected();
|
||||||
if (!context) return;
|
if (!context) return;
|
||||||
selectedContextEntity ||= { show: true };
|
const entity = {};
|
||||||
const contextId = `cyclone:${storm.id}`;
|
const contextId = `cyclone:${storm.id}`;
|
||||||
const record = context.registerEntityContext?.(selectedContextEntity, {
|
context.registerEntityContext(entity, {
|
||||||
id: contextId,
|
id: contextId,
|
||||||
layerId: 'weather-cyclones',
|
layerId: 'weather-cyclones',
|
||||||
layerName: 'Cyclone advisories',
|
layerName: 'Cyclone advisories',
|
||||||
@@ -145,10 +143,9 @@ export function createCyclonesLayer({
|
|||||||
: undefined,
|
: undefined,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!record) return;
|
|
||||||
selectedContextStormId = storm.id;
|
selectedContextStormId = storm.id;
|
||||||
if (announce) context.selectEntityContext?.(selectedContextEntity);
|
if (announce) context.selectEntityContext(entity);
|
||||||
context.removeEntityContextsForLayer?.('weather-cyclones', {
|
context.removeEntityContextsForLayer('weather-cyclones', {
|
||||||
retainIds: new Set([contextId]),
|
retainIds: new Set([contextId]),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -488,12 +488,7 @@ test('shared cyclone context distinguishes switches, deliberate clears and feed
|
|||||||
assert.equal(store.entities.size, 0);
|
assert.equal(store.entities.size, 0);
|
||||||
|
|
||||||
h.layer.setParams({ stormId: 'ep152026' });
|
h.layer.setParams({ stormId: 'ep152026' });
|
||||||
const record = store.entities.get('cyclone:ep152026');
|
assert.equal(store.entities.get('cyclone:ep152026').label, 'Fifteen-E');
|
||||||
assert.equal(record.layerId, 'weather-cyclones');
|
|
||||||
assert.equal(record.source, 'NOAA NHC / CPHC');
|
|
||||||
assert.equal(record.label, 'Fifteen-E');
|
|
||||||
assert.equal(record.latitude, 15.5);
|
|
||||||
assert.equal(record.longitude, -125.8);
|
|
||||||
let properties = await selectedProperties();
|
let properties = await selectedProperties();
|
||||||
assert.equal(properties.classification, 'PTC');
|
assert.equal(properties.classification, 'PTC');
|
||||||
assert.equal(properties.classificationName, 'Post-tropical cyclone');
|
assert.equal(properties.classificationName, 'Post-tropical cyclone');
|
||||||
@@ -652,24 +647,11 @@ test('cyclone clears leave another layer selection and its records alone', async
|
|||||||
'no clear is reported for a foreign selection',
|
'no clear is reported for a foreign selection',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// A cyclone clear does not report a dangling foreign selection as evicted.
|
||||||
|
store.entities.delete('flight:abc123');
|
||||||
h.layer.disable();
|
h.layer.disable();
|
||||||
assert.equal(store.selectedEntityId, 'flight:abc123');
|
assert.equal(store.selectedEntityId, 'flight:abc123');
|
||||||
assert.equal(events.length, before);
|
assert.equal(events.length, before);
|
||||||
|
|
||||||
// A cyclone feed failure does not evict a dangling foreign selection.
|
|
||||||
store.entities.delete('flight:abc123');
|
|
||||||
let fail = false;
|
|
||||||
const other = open({
|
|
||||||
getSnapshot: async () => {
|
|
||||||
if (fail) throw new Error('Network unavailable');
|
|
||||||
return snapshot();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
other.layer.enable();
|
|
||||||
await other.layer.update();
|
|
||||||
fail = true;
|
|
||||||
await other.layer.update();
|
|
||||||
assert.equal(events.length, before);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user