refactor: separate earthquake source and display ownership

This commit is contained in:
Sameh Khamis committed 2026-09-13 12:14:44 -07:00
1 parent 965a44860f
commit d1d938babe
11 files changed
+540 -339

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
import { normalizeEarthquakeSnapshot } from './model.js';
const API_URL =
'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson';
/** Request and validate a complete USGS snapshot before it can replace displayed events. */
export function createUsgsEarthquakeSource({
fetchImpl = (...args) => globalThis.fetch(...args),
} = {}) {
return {
async getSnapshot({ signal } = {}) {
signal?.throwIfAborted();
const response = await fetchImpl(API_URL, { signal });
if (!response.ok) throw new Error(`USGS HTTP ${response.status}`);
const payload = await response.json();
signal?.throwIfAborted();
const rows = normalizeEarthquakeSnapshot(payload);
if (!rows) throw new Error('Malformed USGS response');
return rows;
},
};
}