Skip to main content

interval_precise

Function interval_precise 

Source
pub fn interval_precise(duration: Duration) -> Cell<u64, CellImmutable>
Expand description

Creates a high-precision interval cell that emits 0, 1, 2, … at the given frequency.

On native targets this uses spin-sleeping for sub-millisecond precision, suitable for high-frequency applications like 240Hz sync clocks. On wasm there is no sub-millisecond timer, so this behaves like interval.

Performance note: prefer interval_precise_source when consumers only need notification.

§Arguments

  • duration - The interval between ticks

§Example

use std::time::Duration;
use hyphae::{interval_precise, Watchable};

// 240 Hz = ~4.16ms interval
let clock = interval_precise(Duration::from_secs_f64(1.0 / 240.0));

let _guard = clock.subscribe(|signal| {
    if let hyphae::Signal::Value(tick) = signal {
        println!("Tick: {}", tick);
    }
});