The first player to finish a race is not the same as the room being finished. A newcomer should not keep an old race open, a disconnected participant should not block everyone forever, and the last progress packet should not race with a stale React render. Typestrix addresses those cases with explicit participant and progress snapshots.
A roster changes; a race has participants
When a start event arrives, the room captures its participant IDs. Newer messages provide that snapshot from the server; older messages fall back to the currently known roster. The room resets progress, clears the previous result, and begins its countdown.
The participant set is separate from the visible members list. Someone joining afterward can appear in the room without becoming a new condition for finishing the already-started race.
That separation is a small but important model choice. Membership answers “who is here now?” Participation answers “whose completion counts for this round?”
Read the latest progress synchronously
React state drives the roster UI, but a ref-backed map records the latest progress immediately. On a progress message, the map is updated before the completion check runs.
Without that order, checking whether everyone finished immediately after setting state could read the previous render's 99% value. The map gives event handling a current snapshot while React catches up visually.
const checkAllFinished = useCallback(() => {
const participants = raceParticipantsRef.current;
if (participants.size === 0) return;
const allDone = [...participants].every((id) => (progressMapRef.current.get(id)?.progress ?? 0) >= 100);
if (allDone) handleRaceEnd();
}, [handleRaceEnd]);
Updates that decrease known progress are dropped. Once a player has reached 100%, an older packet cannot move that player's visible state backward.
Your finish does not wait for its echo
When the local typing engine completes, the room saves the local result and optimistically marks the player at 100% in both the progress map and members state. It can then check completion immediately instead of waiting for the server to broadcast that progress back.
That produces a responsive waiting/results transition. It is a local UI decision, not proof that the server accepted the finishing update. The server still has separate progress and elapsed-time checks.
Until everyone is done, the UI can show your own result with a waiting message. When the participant set is complete, it switches to the final room view.
Departure changes the finish condition
A member-left event removes that user's progress and participation entry before checking completion again. If every remaining participant has finished, the departure can end the race.
Participants who have not sent progress default to 0%, so silence does not accidentally count as completion. An empty participant set does not trigger the all-finished check; the timeouts below remain the fallback. handleRaceEnd treats an empty set the other way round and keeps every member in the final table, which covers a browser that joined after the start and never saw RACE_START.
A separate ended flag guards the transition to results. Both a local all-finished check and a server completion event can arrive, but only the first transition builds the final snapshot and triggers the success feedback.
Freeze the final snapshot
Final members are filtered against the captured participants and merged with the latest progress map. That makes the results view a snapshot rather than a live roster that continues changing under the reader.
The client also has a generous timeout: time mode uses the configured duration plus five seconds; word mode uses three seconds per configured word plus thirty seconds. The timeout effect does not restart for every progress packet.
The missing piece is a round identifier
The current messages do not attach a unique race ID to each progress update. Monotonic progress protects a player within the current map, but it cannot definitively classify an old packet from a previous round.
The useful pattern is still clear: current roster, round participants, latest progress, and final results are different states. Keeping them separate prevents several common completion bugs. A round-scoped protocol would strengthen that model further.