Scale SSE replay window by visible cities

This commit is contained in:
2569718930@qq.com
2026-05-31 20:33:04 +08:00
parent ada5f274d3
commit 8d552a9279
3 changed files with 17 additions and 2 deletions
+9 -1
View File
@@ -5,6 +5,9 @@ import { resolveBackendApiUrl } from "@/lib/backend-api";
const V1_EVENT_TYPE = "city_observation_patch.v1";
const SSE_SUBSCRIPTION_RECONNECT_DELAY_MS = 150;
const SSE_REPLAY_BASE_LIMIT = 60;
const SSE_REPLAY_EVENTS_PER_CITY = 24;
const SSE_REPLAY_MAX_LIMIT = 240;
export type CityPatch = {
type?: string;
@@ -91,12 +94,17 @@ function buildSseUrl(baseUrl: string) {
if (lastRevision > 0) {
params.set("since_revision", String(lastRevision));
}
params.set("replay_limit", "500");
params.set("replay_limit", String(resolveSseReplayLimit(cities.length)));
const query = params.toString();
return query ? `${baseUrl}?${query}` : baseUrl;
}
function resolveSseReplayLimit(cityCount: number) {
const requested = Math.max(1, cityCount) * SSE_REPLAY_EVENTS_PER_CITY;
return Math.max(SSE_REPLAY_BASE_LIMIT, Math.min(SSE_REPLAY_MAX_LIMIT, requested));
}
function currentConnectionKey() {
return `${useFallbackUrl ? "fallback" : "direct"}:${subscribedCityList().join("|")}:${lastRevision}`;
}