Skip to main content

Module timer

Module timer 

Source
Expand description

Wall-clock and frame-based timers without an async runtime.

Schedule a callback for an Instant or a frame count; the platform wakes the event loop exactly then via ReposeRuntime::next_wakeup_deadline (ControlFlow::WaitUntil). No executor, no threads, no per-frame cost when idle.

Compose parallels (approximate):

ComposeHere
delay(d) half of LaunchedEffectdelay (unscoped; hold handle)
LaunchedEffect without keysscoped_delay (unmount only)
fixed-rate repetitioninterval (drift-free)
run-after-deadlinetimeout (delay alias)
flow debounce (trailing edge)Debouncer / debounced_signal
sequencing over redrawsdelay_frames

Caveats vs coroutines: handles and flags suppress only pending firings. There is no counterpart for cancelling running work (LaunchedEffect key-change/leave, withTimeout aborting in-flight work, collectLatest): a firing callback runs to completion, timeout is not withTimeout, and delay_frames counts redraw polls (no vsync timestamp, refresh-rate dependent) rather than subscribing to frames.

Rules of thumb: wall-clock waiting goes through delay/interval; sequencing after animations or redraws through delay_frames; reactive signal shaping through debounced_signal.

Structs§

Debouncer
Trailing-edge debouncer: each call reschedules the single pending firing. Cloneable (shared slot); dropping all clones cancels it.
Throttler
Leading-edge throttler with one coalesced trailing firing per period.
TimerHandle
Owner of a scheduled timer. Dropping cancels it (no-op if already fired).

Functions§

delay
Run cb once after duration. Returns a handle; dropping it cancels.
delay_frames
Run cb once after frames redraws have been polled. Counts polls, not vsync frames: no timestamp, refresh-rate dependent. While frame entries are pending poll keeps requesting frames. A 0 count fires on the next poll.
frame_count
Current redraw count (advanced by poll). Basis for delay_frames.
interval
Run cb every period, drift-free (next fire = last scheduled fire + period, so slow frames skip beats instead of bunching). Dropping the handle stops the timer. Periods below 1ms are clamped to 1ms.
interval_n
Like interval, but stops on its own after times firings. A 0 count schedules nothing and returns a disarmed handle.
next_deadline
Earliest wall-clock deadline, if any. Fed into ReposeRuntime::next_wakeup_deadline so the platform sleeps until a timer is due.
poll
Advance the frame counter and fire due timers. Called once per redraw (from ReposeRuntime::tick_overlays). Reentrant calls, e.g. from inside a timer callback, are ignored: the outer pass already collected the due timers, so a nested pass would fire them twice.
scoped_delay
delay tied to the current composition scope: if the scope disposes before the deadline, the callback is suppressed. Schedules once per mount; use scoped_delay_with_key to restart on change.
scoped_delay_with_key
Keyed scoped_delay: reschedules when key changes (cancelling the previous generation via its flag) and suppresses on unmount. Must be called inside composition; outside a scope it degrades to delay.
timeout
Run cb once after duration. A delay alias naming the run-after-deadline intent. This is not withTimeout: it cannot abort in-flight work, it only starts cb once the duration elapses.