From b31a1dd4d6c20f43c4f0d2e91f796ef86e06fb94 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 26 Nov 2026 18:31:21 +0100 Subject: [PATCH] scene: a weather file that arrives after the place changed is not written Co-Authored-By: Claude Sonnet 5.5 --- src/app/features/scene/skyForState.test.ts | 21 +++++++++++++++++++++ src/app/features/scene/skyForState.ts | 7 +++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/app/features/scene/skyForState.test.ts b/src/app/features/scene/skyForState.test.ts index 4815721c..835bb302 100644 --- a/src/app/features/scene/skyForState.test.ts +++ b/src/app/features/scene/skyForState.test.ts @@ -167,6 +167,27 @@ describe('loadWeather', () => { expect(ridgeWeather()).toBeGreaterThan(0.7); }); + it("a fetch that resolves after the place has changed writes nothing: the place the viewer is on is drawn clear, not with the old place's rain", async () => { + let release = () => undefined as void; + vi.stubGlobal( + 'fetch', + vi.fn( + () => + new Promise((resolve) => { + release = () => resolve({ ok: true, json: async () => wx(later) }); + }) + ) + ); + const later = new Date(t.getTime() + 24 * 3_600_000); + setFedWeather(undefined); + const slow = loadWeather('/w.json', VIENNA, later); // Vienna's file is on the wire + await loadWeather('/w.json', FUJI, new Date(later.getTime() + 1_000)); // the viewer is on Fuji, which has no feed: clear + expect(ridgeWeather()).toBe(CLEAR_SKY); + release(); + await slow; + expect(ridgeWeather()).toBe(CLEAR_SKY); // Vienna's rain is not Fuji's + }); + it("rejects a file whose station is not the place's feed's: a clear sky", async () => { vi.stubGlobal('fetch', async () => ({ ok: true, json: async () => ({ ...wx(t), station: 99999 }) })); await loadWeather('/w.json', VIENNA, t); diff --git a/src/app/features/scene/skyForState.ts b/src/app/features/scene/skyForState.ts index 997b958f..79110262 100644 --- a/src/app/features/scene/skyForState.ts +++ b/src/app/features/scene/skyForState.ts @@ -37,7 +37,8 @@ const WEATHER_REFRESH_MS = 10 * 60_000; let weatherAt = -Infinity; let weatherPlace = ''; /** - * Read `weather.json` (the page's own, next to config.json) into the weather the ridge sky is drawn for; at most once in ten minutes. + * Read `weather.json` (the page's own, next to config.json) into the weather the ridge sky is drawn for; at most once in ten minutes. A file that arrives after + * the place moved on is not written. * Never throws: a missing, stale or broken file is a clear sky (`currentWeather`). A place with no feed (`feedFor`) is a clear sky and the file is not * fetched; the place is remembered all the same, so going back to a place with a feed reads the file at once. */ @@ -50,7 +51,9 @@ export async function loadWeather(url: string, place: Place, now: Date = skyNow( setFedWeather(CLEAR_SKY); return; } - setFedWeather(currentWeather(await fetchWeatherFile(url, feed), place, now)); + const weather = currentWeather(await fetchWeatherFile(url, feed), place, now); + // The place the weather is for may have changed while the file was on the wire: `weatherPlace` is the one owner of it, and an answer for another place is dropped. + if (weatherPlace === place.id) setFedWeather(weather); } /**