diff --git a/examples/timing/src/main.rs b/examples/timing/src/main.rs index e111ccc..732761c 100644 --- a/examples/timing/src/main.rs +++ b/examples/timing/src/main.rs @@ -17,14 +17,17 @@ fn app() -> Element { count += 1; }); - let mut debounce = use_debounce(Duration::from_millis(2000), move || count.set(0)); + let mut debounce = use_debounce(Duration::from_millis(2000), move |text| { + println!("{text}"); + count.set(0); + }); rsx! { p { "{count}" }, button { onclick: move |_| { // Reset the counter after 2 seconds pass since the last click. - debounce.action(); + debounce.action("button was clicked"); }, "Reset the counter! (2 second debounce)" } diff --git a/sdk/src/utils/timing/debounce.rs b/sdk/src/utils/timing/debounce.rs index 84dc7ad..d00fae9 100644 --- a/sdk/src/utils/timing/debounce.rs +++ b/sdk/src/utils/timing/debounce.rs @@ -9,14 +9,14 @@ use std::time::Duration; /// /// See [`use_debounce`] for more information. #[derive(Clone, Copy, PartialEq)] -pub struct UseDebounce { - sender: Signal>, +pub struct UseDebounce { + sender: Signal>, } -impl UseDebounce { +impl UseDebounce { /// Will start the debounce countdown, resetting it if already started. - pub fn action(&mut self) { - self.sender.write().unbounded_send(true).ok(); + pub fn action(&mut self, data: T) { + self.sender.write().unbounded_send(data).ok(); } } @@ -33,19 +33,19 @@ impl UseDebounce { /// use std::time::Duration; /// /// fn App() -> Element { -/// let mut debounce = use_debounce(Duration::from_millis(2000), || println!("ran")); +/// let mut debounce = use_debounce(Duration::from_millis(2000), |_| println!("ran")); /// /// rsx! { /// button { /// onclick: move |_| { -/// debounce.action(); +/// debounce.action(()); /// }, /// "Click!" /// } /// } /// } /// ``` -pub fn use_debounce(time: Duration, cb: impl FnOnce() + Copy + 'static) -> UseDebounce { +pub fn use_debounce(time: Duration, cb: impl FnOnce(T) + Copy + 'static) -> UseDebounce { use_hook(|| { let (sender, mut receiver) = mpsc::unbounded(); let debouncer = UseDebounce { @@ -56,14 +56,14 @@ pub fn use_debounce(time: Duration, cb: impl FnOnce() + Copy + 'static) -> UseDe let mut current_task: Option = None; loop { - if receiver.next().await.is_some() { - if let Some(task) = current_task { + if let Some(data) = receiver.next().await { + if let Some(task) = current_task.take() { task.cancel(); } current_task = Some(spawn(async move { tokio::time::sleep(time).await; - cb(); + cb(data); })); } }