Skip to content

Commit

Permalink
feat: make Handles clonable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Feb 18, 2024
1 parent 117f6e4 commit 9d27432
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions crates/sauron-core/src/dom/raf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::dom::window;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast, JsValue};
use std::rc::Rc;

/// request animation frame handle
#[derive(Clone)]
pub struct AnimationFrameHandle {
handle: i32,
_closure: Closure<dyn FnMut()>,
_closure: Rc<Closure<dyn FnMut()>>,
}
impl Drop for AnimationFrameHandle {
fn drop(&mut self) {
Expand All @@ -24,6 +26,6 @@ where
let handle = window().request_animation_frame(closure.as_ref().unchecked_ref())?;
Ok(AnimationFrameHandle {
handle,
_closure: closure,
_closure: Rc::new(closure),
})
}
8 changes: 6 additions & 2 deletions crates/sauron-core/src/dom/ric.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::dom::{now, request_timeout_callback, window, TimeoutCallbackHandle};
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast, JsValue};
use std::rc::Rc;

/// request idle callback handle
#[derive(Clone)]
pub struct IdleCallbackHandleReal {
handle: u32,
_closure: Closure<dyn FnMut(JsValue)>,
_closure: Rc<Closure<dyn FnMut(JsValue)>>,
}

/// when dropped, cancel the idle callback
Expand All @@ -30,12 +32,13 @@ where
let handle = window().request_idle_callback(closure.as_ref().unchecked_ref())?;
Ok(IdleCallbackHandleReal {
handle,
_closure: closure,
_closure: Rc::new(closure),
})
}

/// Idle deadline interface which could be the real idle deadline if supported, otherwise the
/// polyfill
#[derive(Clone)]
pub enum IdleDeadline {
/// the web native IdleDeadline object wrap together with polyfill version
Real(web_sys::IdleDeadline),
Expand All @@ -47,6 +50,7 @@ pub enum IdleDeadline {
}

///
#[derive(Clone)]
pub enum IdleCallbackHandle {
/// wrapper to the real web native IdleCallbackHandle
Real(IdleCallbackHandleReal),
Expand Down
7 changes: 4 additions & 3 deletions crates/sauron-core/src/dom/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use js_sys::Promise;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use std::rc::Rc;

/// handle for request_idle_callback calls
#[derive(Debug)]
#[derive(Debug,Clone)]
pub struct TimeoutCallbackHandle {
handle: i32,
_closure: Closure<dyn FnMut()>,
_closure: Rc<Closure<dyn FnMut()>>,
}

impl Drop for TimeoutCallbackHandle {
Expand All @@ -29,7 +30,7 @@ where
)?;
Ok(TimeoutCallbackHandle {
handle,
_closure: closure,
_closure: Rc::new(closure),
})
}

Expand Down

0 comments on commit 9d27432

Please sign in to comment.