use std::cell::RefCell; use std::rc::Rc; use log::error; use wasm_bindgen::prelude::*; pub struct RenderLoop { inner: Rc>>, } impl RenderLoop { fn request_animation_frame(f: &Closure) -> Result<(), JsValue> { web_sys::window() .ok_or("no window")? .request_animation_frame(f.as_ref().unchecked_ref())?; Ok(()) } pub fn new bool + 'static>(mut fun: T) -> Self { let inner = Rc::new(RefCell::new(Closure::new(|_| {}))); let rloop = inner.clone(); *inner.borrow_mut() = Closure::new(move |t| { if fun(t) { if let Err(e) = Self::request_animation_frame(&rloop.borrow()) { error!("couldn't request animation frame: {:?}", e); } } }); Self { inner } } pub fn start(&self) -> Result<(), JsValue> { Self::request_animation_frame(&self.inner.borrow())?; Ok(()) } } impl Clone for RenderLoop { fn clone(&self) -> Self { Self { inner: self.inner.clone(), } } }