aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbrian cully <bjc@spork.org>2025-12-28 11:58:17 -0500
committerbrian cully <bjc@spork.org>2025-12-28 11:58:17 -0500
commit260ddcad131c1907f8eea154ce8bfb4a30dc717b (patch)
treeb45bd7f8e122a59977dfd7e5541377c0a9df3fd7 /src
parent4aa3ac264ff0a5cb102dcbd0d4e70a1df2ec96dd (diff)
downloadpolyring-260ddcad131c1907f8eea154ce8bfb4a30dc717b.tar.gz
polyring-260ddcad131c1907f8eea154ce8bfb4a30dc717b.zip
move render loop to its own module
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs42
-rw-r--r--src/render_loop.rs41
2 files changed, 45 insertions, 38 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e8e9c1a..87c68fb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,45 +1,17 @@
-use std::cell::RefCell;
-use std::sync::{Mutex};
use std::rc::Rc;
+use std::sync::Mutex;
-use log::{Level, info, debug};
+use log::{Level, debug, info};
use wasm_bindgen::prelude::*;
use state::State;
+use render_loop::RenderLoop;
mod line;
mod point;
+mod render_loop;
mod state;
-struct RenderLoop {
- inner: Rc<RefCell<Option<Closure<dyn FnMut(f64)>>>>,
-}
-
-impl RenderLoop {
- fn new<T: FnMut(f64) -> bool + 'static>(mut fun: T) -> Self {
- let inner = Rc::new(RefCell::new(None));
- let rloop = inner.clone();
-
- *inner.borrow_mut() = Some(Closure::new(move |t| {
- if fun(t) {
- request_animation_frame(rloop.borrow().as_ref().expect("can borrow rloop"));
- }
- }));
- Self { inner }
- }
-
- fn start(&self) -> Result<(), JsValue> {
- request_animation_frame(self.inner.borrow().as_ref().ok_or("closure exists")?);
- Ok(())
- }
-}
-
-impl Clone for RenderLoop {
- fn clone(&self) -> Self {
- Self { inner: self.inner.clone() }
- }
-}
-
fn window() -> web_sys::Window {
web_sys::window().expect("no window")
}
@@ -69,12 +41,6 @@ fn go() -> Result<web_sys::HtmlElement, JsValue> {
Ok(x.dyn_into::<web_sys::HtmlElement>()?)
}
-fn request_animation_frame(f: &Closure<dyn FnMut(f64)>) {
- window()
- .request_animation_frame(f.as_ref().unchecked_ref())
- .expect("should register `requestAnimationFrame` OK");
-}
-
#[wasm_bindgen(start)]
pub fn init() -> Result<(), JsValue> {
console_log::init_with_level(Level::Debug).expect("couldn't init console log");
diff --git a/src/render_loop.rs b/src/render_loop.rs
new file mode 100644
index 0000000..bc6d8a9
--- /dev/null
+++ b/src/render_loop.rs
@@ -0,0 +1,41 @@
+use std::cell::RefCell;
+use std::rc::Rc;
+
+use wasm_bindgen::prelude::*;
+
+pub struct RenderLoop {
+ inner: Rc<RefCell<Closure<dyn FnMut(f64)>>>,
+}
+
+impl RenderLoop {
+ fn request_animation_frame(f: &Closure<dyn FnMut(f64)>) {
+ web_sys::window().expect("no window")
+ .request_animation_frame(f.as_ref().unchecked_ref())
+ .expect("should register `requestAnimationFrame` OK");
+ }
+
+ pub fn new<T: FnMut(f64) -> 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) {
+ Self::request_animation_frame(&rloop.borrow());
+ }
+ });
+ 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(),
+ }
+ }
+}