scene: a weather file that arrives after the place changed is not written

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-26 18:31:21 +01:00
commit b31a1dd4d6
2 changed files with 26 additions and 2 deletions

View file

@ -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);

View file

@ -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);
}
/**