-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: debounce * fix: fmt * revision: debounce * fix: clippy * revision: docs, compile errors * revision: debounce * fix: fmt * revision: use clone * revision: remove clone * revision: formatting * revision: manually partialeq
- Loading branch information
Showing
17 changed files
with
176 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use dioxus::prelude::*; | ||
use dioxus_sdk::utils::timing::{use_debounce, use_interval}; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
// init debug tool for WebAssembly | ||
wasm_logger::init(wasm_logger::Config::default()); | ||
console_error_panic_hook::set_once(); | ||
|
||
launch(app); | ||
} | ||
|
||
fn app() -> Element { | ||
let mut count = use_signal(|| 0); | ||
|
||
use_interval(Duration::from_millis(100), move || { | ||
count += 1; | ||
}); | ||
|
||
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("button was clicked"); | ||
}, | ||
"Reset the counter! (2 second debounce)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
mod use_clipboard; | ||
|
||
pub use use_clipboard::*; | ||
cfg_if::cfg_if! { | ||
if #[cfg(not(target_family = "wasm"))] { | ||
mod use_clipboard; | ||
pub use use_clipboard::*; | ||
} else { | ||
compile_error!("the `clipboard` feature is only available on desktop targets"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
mod use_preferred_color_scheme; | ||
|
||
pub use use_preferred_color_scheme::*; | ||
cfg_if::cfg_if! { | ||
if #[cfg(target_family = "wasm")] { | ||
mod use_preferred_color_scheme; | ||
pub use use_preferred_color_scheme::*; | ||
} else { | ||
compile_error!("the `color_scheme` feature is only available on wasm targets"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
pub mod notification; | ||
cfg_if::cfg_if! { | ||
if #[cfg(not(target_family = "wasm"))] { | ||
pub mod notification; | ||
} else { | ||
compile_error!("the `notification` feature is only available on desktop targets"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use dioxus::prelude::*; | ||
use futures::{ | ||
channel::mpsc::{self, UnboundedSender as Sender}, | ||
StreamExt, | ||
}; | ||
use std::time::Duration; | ||
|
||
/// The interface for calling a debounce. | ||
/// | ||
/// See [`use_debounce`] for more information. | ||
pub struct UseDebounce<T: 'static> { | ||
sender: Signal<Sender<T>>, | ||
} | ||
|
||
impl<T> UseDebounce<T> { | ||
/// Will start the debounce countdown, resetting it if already started. | ||
pub fn action(&mut self, data: T) { | ||
self.sender.write().unbounded_send(data).ok(); | ||
} | ||
} | ||
|
||
// Manually implement Clone, Copy, and PartialEq as #[derive] thinks that T needs to implement these (it doesn't). | ||
|
||
impl<T> Clone for UseDebounce<T> { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
impl<T> Copy for UseDebounce<T> {} | ||
|
||
impl<T> PartialEq for UseDebounce<T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.sender == other.sender | ||
} | ||
} | ||
|
||
/// A hook for allowing a function to be called only after a provided [`Duration`] has passed. | ||
/// | ||
/// Once the [`UseDebounce::action`] method is called, a timer will start counting down until | ||
/// the callback is ran. If the [`UseDebounce::action`] method is called again, the timer will restart. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use dioxus::prelude::*; | ||
/// use dioxus_sdk::utils::timing::use_debounce; | ||
/// use std::time::Duration; | ||
/// | ||
/// fn App() -> Element { | ||
/// let mut debounce = use_debounce(Duration::from_millis(2000), |_| println!("ran")); | ||
/// | ||
/// rsx! { | ||
/// button { | ||
/// onclick: move |_| { | ||
/// debounce.action(()); | ||
/// }, | ||
/// "Click!" | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn use_debounce<T>(time: Duration, cb: impl FnOnce(T) + Copy + 'static) -> UseDebounce<T> { | ||
use_hook(|| { | ||
let (sender, mut receiver) = mpsc::unbounded(); | ||
let debouncer = UseDebounce { | ||
sender: Signal::new(sender), | ||
}; | ||
|
||
spawn(async move { | ||
let mut current_task: Option<Task> = None; | ||
|
||
loop { | ||
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(data); | ||
})); | ||
} | ||
} | ||
}); | ||
|
||
debouncer | ||
}) | ||
} |
3 changes: 1 addition & 2 deletions
3
sdk/src/utils/interval/mod.rs → sdk/src/utils/timing/interval.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! Timing utilities. | ||
|
||
mod interval; | ||
pub use interval::*; | ||
|
||
mod debounce; | ||
pub use debounce::*; |