refactor(perimeters): share getSnapshotDetail without this

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
quanticsoul4772andClaude Opus 5.5 committed 2026-10-25 19:42:51 -07:00
1 parent f3faf373f0
commit f6244e8fef
2 files changed
+16 -10

No files matched your search

+9 -10
View File
@@ -8,18 +8,12 @@ const msg = (text) => text;
export function createWfigsPerimeterSource({ export function createWfigsPerimeterSource({
fetchImpl = (...args) => globalThis.fetch(...args), fetchImpl = (...args) => globalThis.fetch(...args),
} = {}) { } = {}) {
return {
/** The perimeter rows (see getSnapshotDetail for staleness). */
async getSnapshot(options = {}) {
return (await this.getSnapshotDetail(options)).rows;
},
/** /**
* The rows with how fresh they are: the proxy answers from its last * The rows with how fresh they are: the proxy answers from its last
* snapshot, marked stale, while WFIGS is unreachable. * snapshot, marked stale, while WFIGS is unreachable.
* @returns {Promise<{rows: object[], stale: boolean, fetchedAt: number|null}>} * @returns {Promise<{rows: object[], stale: boolean, fetchedAt: number|null}>}
*/ */
async getSnapshotDetail({ signal } = {}) { async function getSnapshotDetail({ signal } = {}) {
signal?.throwIfAborted(); signal?.throwIfAborted();
const response = await fetchImpl('/api/fire-perimeters', { signal }); const response = await fetchImpl('/api/fire-perimeters', { signal });
if (!response.ok) throw new Error(`WFIGS HTTP ${response.status}`); if (!response.ok) throw new Error(`WFIGS HTTP ${response.status}`);
@@ -34,10 +28,15 @@ export function createWfigsPerimeterSource({
return { return {
rows: payload.rows, rows: payload.rows,
stale: payload.stale === true, stale: payload.stale === true,
fetchedAt: Number.isFinite(payload.fetchedAt) fetchedAt: Number.isFinite(payload.fetchedAt) ? payload.fetchedAt : null,
? payload.fetchedAt
: null,
}; };
}
return {
/** The perimeter rows (see getSnapshotDetail for staleness). */
async getSnapshot(options = {}) {
return (await getSnapshotDetail(options)).rows;
}, },
getSnapshotDetail,
}; };
} }
+7
View File
@@ -172,3 +172,10 @@ test('the snapshot detail carries the proxy staleness and fetch time', async ()
}); });
assert.deepEqual(await source.getSnapshot(), []); assert.deepEqual(await source.getSnapshot(), []);
}); });
test('getSnapshot works when taken off its source object', async () => {
const { getSnapshot } = createWfigsPerimeterSource({
fetchImpl: async () => Response.json({ rows: [] }),
});
assert.deepEqual(await getSnapshot(), []);
});