mirror of
https://github.com/smittix/intercept.git
synced 2026-04-24 06:40:00 -07:00
refactor(satellite): simplify telemetry abort controller management
Consolidated to a single active-request guard with cleanup in finally. The previous pattern had redundant null-checks across try and catch, and an always-false check on a controller that was already null. Cancel-on- new-request is now explicit before creating the new controller.
This commit is contained in:
@@ -1373,17 +1373,22 @@
|
|||||||
const lat = parseFloat(document.getElementById('obsLat')?.value);
|
const lat = parseFloat(document.getElementById('obsLat')?.value);
|
||||||
const lon = parseFloat(document.getElementById('obsLon')?.value);
|
const lon = parseFloat(document.getElementById('obsLon')?.value);
|
||||||
if (!Number.isFinite(lat) || !Number.isFinite(lon) || !selectedSatellite) return;
|
if (!Number.isFinite(lat) || !Number.isFinite(lon) || !selectedSatellite) return;
|
||||||
const requestKey = `telemetry:${requestedSatellite}:${lat.toFixed(3)}:${lon.toFixed(3)}`;
|
|
||||||
|
|
||||||
if (_telemetryAbortController && _activeTelemetryRequestKey === requestKey) {
|
const requestKey = `telemetry:${requestedSatellite}:${lat.toFixed(3)}:${lon.toFixed(3)}`;
|
||||||
return;
|
if (_activeTelemetryRequestKey === requestKey) return; // identical request already in flight
|
||||||
|
|
||||||
|
// Cancel any in-flight request for a different satellite/location
|
||||||
|
if (_telemetryAbortController) {
|
||||||
|
_telemetryAbortController.abort();
|
||||||
|
_telemetryAbortController = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
_telemetryAbortController = controller;
|
||||||
|
_activeTelemetryRequestKey = requestKey;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const controller = new AbortController();
|
const timeoutId = setTimeout(() => controller.abort(), TELEMETRY_FETCH_TIMEOUT_MS);
|
||||||
_telemetryAbortController = controller;
|
|
||||||
_activeTelemetryRequestKey = requestKey;
|
|
||||||
const timeout = setTimeout(() => controller.abort(), TELEMETRY_FETCH_TIMEOUT_MS);
|
|
||||||
const response = await fetch('/satellite/position', {
|
const response = await fetch('/satellite/position', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
@@ -1396,25 +1401,28 @@
|
|||||||
includeTrack: false
|
includeTrack: false
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeoutId);
|
||||||
if (_telemetryAbortController === controller) {
|
|
||||||
_telemetryAbortController = null;
|
|
||||||
}
|
|
||||||
if (_activeTelemetryRequestKey === requestKey) {
|
|
||||||
_activeTelemetryRequestKey = null;
|
|
||||||
}
|
|
||||||
if (!response.ok) return;
|
if (!response.ok) return;
|
||||||
const contentType = response.headers.get('Content-Type') || '';
|
const contentType = response.headers.get('Content-Type') || '';
|
||||||
if (!contentType.includes('application/json')) return;
|
if (!contentType.includes('application/json')) return;
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (data.status !== 'success' || !Array.isArray(data.positions)) return;
|
if (data.status !== 'success' || !Array.isArray(data.positions)) return;
|
||||||
|
|
||||||
|
// Discard if satellite or selection changed while request was in flight
|
||||||
|
if (selectionToken !== _satelliteSelectionRequestToken || requestedSatellite !== selectedSatellite) return;
|
||||||
|
|
||||||
const pos = data.positions.find(p => parseInt(p.norad_id, 10) === requestedSatellite) || null;
|
const pos = data.positions.find(p => parseInt(p.norad_id, 10) === requestedSatellite) || null;
|
||||||
if (!pos) return;
|
if (!pos) return;
|
||||||
cacheLivePosition(requestedSatellite, pos);
|
cacheLivePosition(requestedSatellite, pos);
|
||||||
if (selectionToken !== _satelliteSelectionRequestToken || requestedSatellite !== selectedSatellite) return;
|
|
||||||
handleLivePositions(data.positions, 'poll');
|
handleLivePositions(data.positions, 'poll');
|
||||||
} catch (_) {
|
|
||||||
if (_telemetryAbortController?.signal?.aborted) {
|
} catch (err) {
|
||||||
|
if (err?.name === 'AbortError') return; // expected on cancel/timeout
|
||||||
|
console.debug('Telemetry fetch error:', err);
|
||||||
|
} finally {
|
||||||
|
// Always release the controller slot so the next poll can run
|
||||||
|
if (_telemetryAbortController === controller) {
|
||||||
_telemetryAbortController = null;
|
_telemetryAbortController = null;
|
||||||
}
|
}
|
||||||
if (_activeTelemetryRequestKey === requestKey) {
|
if (_activeTelemetryRequestKey === requestKey) {
|
||||||
|
|||||||
Reference in New Issue
Block a user