diff options
| author | brian cully <bjc@spork.org> | 2025-12-28 13:24:27 -0500 |
|---|---|---|
| committer | brian cully <bjc@spork.org> | 2025-12-28 13:24:27 -0500 |
| commit | 6b3ecb884b38730ea20e2a44136a79e573525509 (patch) | |
| tree | 5f6d7bc0a982187d88fe1ba28505e85959bda5b2 | |
| parent | 7c26b351d207783fef5dd0f9323d74df4b650ad8 (diff) | |
| download | polyring-6b3ecb884b38730ea20e2a44136a79e573525509.tar.gz polyring-6b3ecb884b38730ea20e2a44136a79e573525509.zip | |
comments, whitespace
| -rw-r--r-- | src/render_loop.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/render_loop.rs b/src/render_loop.rs index 5dcf653..8fc0164 100644 --- a/src/render_loop.rs +++ b/src/render_loop.rs @@ -4,6 +4,8 @@ use std::rc::Rc; use log::error; use wasm_bindgen::prelude::*; +/// use `window.requestAnimationFrame()` to schedule calling a +/// function as long as the function returns true. pub struct RenderLoop { inner: Rc<RefCell<Closure<dyn FnMut(f64)>>>, } @@ -16,10 +18,16 @@ impl RenderLoop { Ok(()) } + /// `fun` takes a timestamp in the same space as the document + /// timeline and returns a flag specifying whether we should + /// schedule another frame.. pub fn new<T: FnMut(f64) -> bool + 'static>(mut fun: T) -> Self { + // use stub closure so we don't need an `Option` to flag + // whether the `RefCell` is filled or not, and otherwise we'd + // need to use `MaybeUninit` and `unsafe`. let inner = Rc::new(RefCell::new(Closure::new(|_| {}))); - let rloop = inner.clone(); + let rloop = inner.clone(); *inner.borrow_mut() = Closure::new(move |t| { if fun(t) { if let Err(e) = Self::request_animation_frame(&rloop.borrow()) { @@ -27,9 +35,11 @@ impl RenderLoop { } } }); + Self { inner } } + /// start animating. pub fn start(&self) -> Result<(), JsValue> { Self::request_animation_frame(&self.inner.borrow())?; Ok(()) |
