Skip to content

five-on-it / apps/server/src/loop

apps/server/src/loop ​

A fixed-rate loop on top of setTimeout: it keeps an accumulator against the wall clock so the average rate is exact even though individual timer callbacks are late. Behind (a late timer, a slow step, a pause of the process), it catches up one step per callback, the next one on setImmediate: Node reads the sockets between the two, so a step runs on the inputs that arrived while the loop was behind instead of on guesses (game-server.ts runs a client's tick on a guess when its input is not in). It measures how long each step takes (the tick budget is 8 ms at 60 Hz): on average, at worst, and over the last RECENT_STEPS steps (their median, 99th percentile and worst, what the benchmark reads after its window).

The simulation never sees the wall clock; it only sees "one tick happened". The clock and the timer are injectable so the loop itself can be tested deterministically.

Interfaces ​

Variables ​

Functions ​