Skip to main content

sk_runtime/
frames.rs

1//! The frame clock's wake seam: a producer that wakes the redjs pump also calls [`wake`] to get a frame.
2//!
3//! An idle host stops pumping until something asks: [`wake`] from any thread when work was queued
4//! for the pump, [`wake_at`] from the UI thread when a frame must look again at a known time.
5
6use std::cell::Cell;
7use std::sync::OnceLock;
8use std::time::Instant;
9
10static WAKER: OnceLock<fn()> = OnceLock::new();
11
12pub fn set_waker(waker: fn()) {
13    let _ = WAKER.set(waker);
14    redjs::web::set_wake_hook(wake);
15}
16
17/// Asks for a frame at the next refresh. Callable from any thread; a no-op until a host installed its waker.
18pub fn wake() {
19    if let Some(waker) = WAKER.get() {
20        waker();
21    }
22}
23
24thread_local! {
25    static WAKE_AT: Cell<Option<Instant>> = const { Cell::new(None) };
26}
27
28/// From the UI thread: a frame must run by `at` even if nothing else asks for one.
29pub fn wake_at(at: Instant) {
30    WAKE_AT.with(|w| w.set(Some(w.get().map_or(at, |t| t.min(at)))));
31}
32
33/// The earliest [`wake_at`] since the last call, for the host's idle decision.
34pub fn take_wake_at() -> Option<Instant> {
35    WAKE_AT.with(Cell::take)
36}