Preserve runway history during chart refresh

This commit is contained in:
2569718930@qq.com
2026-06-15 18:47:25 +08:00
parent 3f8ae44149
commit 05a36f857b
6 changed files with 267 additions and 9 deletions
@@ -466,6 +466,18 @@ function writeSessionCache(city: string, data: HourlyForecast) {
} catch {}
}
function rememberHourlyDetailSnapshot(
city: string,
resolution: string,
data: HourlyForecast,
) {
const cityKey = normalizeCityKey(city);
if (!cityKey || !data || !hasFullHourlyDetailPayload(data)) return;
const cacheKey = `${cityKey}:${resolution || "10m"}`;
_hourlyCache.set(cacheKey, { ts: Date.now(), data });
writeSessionCache(cacheKey, data);
}
function drainHourlyDetailRequestQueue() {
while (
_hourlyActiveDetailRequests < MAX_HOURLY_DETAIL_CONCURRENT_REQUESTS &&
@@ -1578,14 +1590,37 @@ function parseHourlyForecastFromCityDetail(json: CityDetail | null): HourlyForec
};
}
function preserveCachedRunwayHistory(cacheKey: string, data: HourlyForecast) {
if (!data) return data;
const cached = readHourlyCacheEntry(cacheKey, { allowStale: true })?.data;
if (!cached || hourlyLocalDatesConflict(cached, data, null)) return data;
const cachedHistory =
cached.runwayPlateHistory ||
((cached.amos as any)?.runway_plate_history as Record<string, Array<Record<string, unknown>>> | undefined);
const incomingHistory =
data.runwayPlateHistory ||
((data.amos as any)?.runway_plate_history as Record<string, Array<Record<string, unknown>>> | undefined);
const runwayPlateHistory = mergeRunwayPlateHistory(cachedHistory, incomingHistory);
if (!runwayPlateHistory) return data;
return {
...data,
runwayPlateHistory,
amos: {
...(data.amos || {}),
runway_plate_history: runwayPlateHistory,
} as AmosData,
};
}
function primeCityDetailCache(
city: string,
resolution: string,
detail: CityDetail | null | undefined,
): HourlyForecast {
const data = parseHourlyForecastFromCityDetail(detail || null);
let data = parseHourlyForecastFromCityDetail(detail || null);
if (!data) return null;
const cacheKey = `${city}:${resolution}`;
data = preserveCachedRunwayHistory(cacheKey, data);
_hourlyCache.set(cacheKey, { ts: Date.now(), data });
writeSessionCache(cacheKey, data);
return data;
@@ -3086,6 +3121,7 @@ export {
prefersHighFrequencyRunwayResolution,
readCityDetailBatchDiagnostics,
readSessionCache,
rememberHourlyDetailSnapshot,
selectCompactSecondaryTemp,
selectDisplayRunwayTemp,
selectInitialHourlyForRowChange,