mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-06 11:24:00 +00:00
refactor(perimeters): share getSnapshotDetail without this
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
f3faf373f0
commit
f6244e8fef
2 files changed
+33
-27
No files matched your search
@@ -8,36 +8,35 @@ const msg = (text) => text;
|
|||||||
export function createWfigsPerimeterSource({
|
export function createWfigsPerimeterSource({
|
||||||
fetchImpl = (...args) => globalThis.fetch(...args),
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
||||||
} = {}) {
|
} = {}) {
|
||||||
|
/**
|
||||||
|
* The rows with how fresh they are: the proxy answers from its last
|
||||||
|
* snapshot, marked stale, while WFIGS is unreachable.
|
||||||
|
* @returns {Promise<{rows: object[], stale: boolean, fetchedAt: number|null}>}
|
||||||
|
*/
|
||||||
|
async function getSnapshotDetail({ signal } = {}) {
|
||||||
|
signal?.throwIfAborted();
|
||||||
|
const response = await fetchImpl('/api/fire-perimeters', { signal });
|
||||||
|
if (!response.ok) throw new Error(`WFIGS HTTP ${response.status}`);
|
||||||
|
const payload = await readResponseJsonCapped(
|
||||||
|
response,
|
||||||
|
80 * 1024 * 1024,
|
||||||
|
signal,
|
||||||
|
);
|
||||||
|
signal?.throwIfAborted();
|
||||||
|
if (!Array.isArray(payload?.rows))
|
||||||
|
throw new Error(msg('Malformed perimeter snapshot'));
|
||||||
|
return {
|
||||||
|
rows: payload.rows,
|
||||||
|
stale: payload.stale === true,
|
||||||
|
fetchedAt: Number.isFinite(payload.fetchedAt) ? payload.fetchedAt : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
/** The perimeter rows (see getSnapshotDetail for staleness). */
|
/** The perimeter rows (see getSnapshotDetail for staleness). */
|
||||||
async getSnapshot(options = {}) {
|
async getSnapshot(options = {}) {
|
||||||
return (await this.getSnapshotDetail(options)).rows;
|
return (await getSnapshotDetail(options)).rows;
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The rows with how fresh they are: the proxy answers from its last
|
|
||||||
* snapshot, marked stale, while WFIGS is unreachable.
|
|
||||||
* @returns {Promise<{rows: object[], stale: boolean, fetchedAt: number|null}>}
|
|
||||||
*/
|
|
||||||
async getSnapshotDetail({ signal } = {}) {
|
|
||||||
signal?.throwIfAborted();
|
|
||||||
const response = await fetchImpl('/api/fire-perimeters', { signal });
|
|
||||||
if (!response.ok) throw new Error(`WFIGS HTTP ${response.status}`);
|
|
||||||
const payload = await readResponseJsonCapped(
|
|
||||||
response,
|
|
||||||
80 * 1024 * 1024,
|
|
||||||
signal,
|
|
||||||
);
|
|
||||||
signal?.throwIfAborted();
|
|
||||||
if (!Array.isArray(payload?.rows))
|
|
||||||
throw new Error(msg('Malformed perimeter snapshot'));
|
|
||||||
return {
|
|
||||||
rows: payload.rows,
|
|
||||||
stale: payload.stale === true,
|
|
||||||
fetchedAt: Number.isFinite(payload.fetchedAt)
|
|
||||||
? payload.fetchedAt
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
|
getSnapshotDetail,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -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(), []);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user