fix(sync): a save from an older copy can no longer put a run heat back to ready (1.89.37)

This commit is contained in:
larssand committed 2026-11-15 16:35:43 +01:00
1 parent 93c590e5b8
commit cea278518e
7 files changed
+104 -6

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
// Every open browser saves its whole copy of the state. A second computer that is not the
// Race Controller Master (or a tab left in the background) can hold a copy from before a heat
// was run, and its next save put that heat back to "ready" with no laps - the heat had to be
// run again. A heat that has started only goes back through Reset (which stamps a newer
// resultsResetAt), so a save that turns a started heat back without one comes from an older
// copy: the server keeps its own version of that heat and its results.
const STAGE = { ready: 0, running: 1, paused: 1, finished: 2, stopped: 2 };
const stageOf = (session) => STAGE[String(session?.status || "ready")] ?? 0;
function keepStartedSessions(incoming, stored) {
const kept = [];
if (!incoming || !Array.isArray(incoming.sessions) || !Array.isArray(stored?.sessions)) return { state: incoming, kept };
const storedById = new Map(stored.sessions.filter((session) => session?.id).map((session) => [session.id, session]));
incoming.sessions = incoming.sessions.map((session) => {
const before = storedById.get(session?.id);
if (!before) return session;
const resetBefore = Number(before.resultsResetAt || 0);
const resetNow = Number(session.resultsResetAt || 0);
if (resetNow > resetBefore) return session; // a deliberate Reset
// Same reset (or none): only a started heat can be turned back. An older reset: a copy
// from before the latest Reset, which must not bring the old run back.
if (resetNow === resetBefore && (!before.startedAt || stageOf(session) >= stageOf(before))) return session;
kept.push(session.id);
return before;
});
kept.forEach((sessionId) => {
const result = stored.resultsBySession?.[sessionId];
if (result === undefined) return;
if (!incoming.resultsBySession || typeof incoming.resultsBySession !== "object") incoming.resultsBySession = {};
incoming.resultsBySession[sessionId] = result;
});
return { state: incoming, kept };
}
module.exports = { keepStartedSessions };