Skip to content

Clock bridge

Every time measure of JOID reads the clock bridge: animations and tweens, tasks, videos and animated images, the press time of a drag, the repeat of a held key. The core reads it through IClockBridge, from BridgeHandler.CLOCK.

A backend rarely registers a clock. A host does when its own time can pause or slow down, so that the interface follows it. Tests register a manual clock, which moves only when they call advance(milliseconds): every animation then gives the same frame on every run.

MemberWhat it returns
IClockBridge.currentTimeMillisThe current time, in milliseconds: JOID reads it as a date, or as the phase of a cycle such as Color.RAINBOW and the blink of a text cursor.
IClockBridge.nanoTimeThe current time in nanoseconds, from any origin: JOID measures every duration as a difference of it.
ClassWhat it gives
SystemClockBridgeThe default: the time of the JVM.
ManualClockBridgeA clock that moves only with advance and time, for tests (Use a manual clock in a test).
RuleWhy
Never go backwards.A duration is the difference of two readings: a clock that goes back gives a negative one, and an animation or a scroll in progress breaks.
Advance both methods at the same rate.Durations read the nanoseconds and cycles the milliseconds: two drifting clocks put a color cycle out of step with the animations around it.
final ManualClockBridge clock = new ManualClockBridge(BridgeHandler.CLOCK.get().currentTimeMillis());
BridgeHandler.CLOCK.register(clock);
clock.advance(16L);

Start it at the current time, so that the dates and the cycles that read the milliseconds do not jump. Whatever time you give it, its nanoseconds go on from those of the clock it replaces, so a duration measured across the registration stays right.

time(milliseconds) sets the clock to an absolute time. Unregister it at the end of the test with BridgeHandler.CLOCK.unregister(clock), once its UIs are closed: the system clock, still in the registry below it, takes over, and its time is behind the manual clock by all that the test advanced beyond the real time. The snapshot runner of the testkit registers one manual clock for its whole run and sets it back to the same time before every scenario.

Report a problem with this page