Skip to content

Commit 3fd4d38

Browse files
committed
feat: sleep fn for time crate
1 parent 16dd11c commit 3fd4d38

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

packages/time/src/debounce.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ impl<T> PartialEq for UseDebounce<T> {
3939
/// Once the [`UseDebounce::action`] method is called, a timer will start counting down until
4040
/// the callback is ran. If the [`UseDebounce::action`] method is called again, the timer will restart.
4141
///
42-
/// # Example
42+
/// # Examples
4343
///
4444
/// ```rust
4545
/// use dioxus::prelude::*;
4646
/// use dioxus_time::use_debounce;
4747
/// use std::time::Duration;
4848
///
49+
/// #[component]
4950
/// fn App() -> Element {
50-
/// let mut debounce = use_debounce(Duration::from_millis(2000), |_| println!("ran"));
51+
/// let mut debounce = use_debounce(Duration::from_secs(2), |_| println!("ran"));
5152
///
5253
/// rsx! {
5354
/// button {

packages/time/src/interval.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use dioxus::prelude::{use_hook, Callback, Writable};
22
use std::time::Duration;
33

4+
/// A handle to an interval.
5+
///
6+
/// This handle allows you to cancel an interval.
47
#[derive(Clone, PartialEq, Copy)]
58
pub struct UseInterval {
69
inner: dioxus::prelude::Signal<InnerUseInterval>,
@@ -28,6 +31,25 @@ impl UseInterval {
2831
}
2932

3033
/// Repeatedly call a function at a specific interval.
34+
///
35+
/// Intervals are cancelable with the [`UseInterval::cancel`] method.
36+
///
37+
/// # Examples
38+
/// ```rust
39+
/// use dioxus::prelude::*;
40+
/// use dioxus_time::use_interval;
41+
/// use std::time::Duration;
42+
///
43+
/// #[component]
44+
/// fn App() -> Element {
45+
/// let mut time_elapsed = use_signal(|| 0);
46+
/// use_interval(Duration::from_secs(1), move || *time_elapsed.write() += 1);
47+
///
48+
/// rsx! {
49+
/// "It has been {time_elapsed} since the app started."
50+
/// }
51+
/// }
52+
/// ```
3153
pub fn use_interval(period: Duration, mut action: impl FnMut() + 'static) -> UseInterval {
3254
let inner = use_hook(|| {
3355
let callback = Callback::new(move |()| {

packages/time/src/lib.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1-
//! Dioxus time utilities.
1+
//! # Dioxus Time Utilities
2+
//!
3+
//! Cross-platform timing utilities for your Dioxus apps.
4+
//!
5+
//! We currently offer:
6+
//! - [`use_debounce`]
7+
//! - [`use_interval`]
8+
//! - and [`sleep`]
9+
#![warn(missing_docs)]
10+
11+
use std::time::Duration;
212

313
mod interval;
414
pub use interval::{use_interval, UseInterval};
515

616
mod debounce;
717
pub use debounce::{use_debounce, UseDebounce};
18+
19+
20+
/// Pause the current task for the specified duration.
21+
///
22+
/// # Examples
23+
/// ```rust
24+
/// use std::time::Duration;
25+
/// use dioxus::prelude::*;
26+
///
27+
/// #[component]
28+
/// pub fn App() -> Element {
29+
/// let mut has_slept = use_signal(|| false);
30+
///
31+
/// use_effect(move || {
32+
/// spawn(async move {
33+
/// dioxus_time::sleep(Duration::from_secs(3)).await;
34+
/// has_slept.set(true);
35+
/// });
36+
/// });
37+
///
38+
/// rsx! {
39+
/// "I have slept: {has_slept}"
40+
/// }
41+
/// }
42+
/// ```
43+
pub async fn sleep(duration: Duration) {
44+
#[cfg(not(target_family = "wasm"))]
45+
tokio::time::sleep(duration).await;
46+
47+
#[cfg(target_family = "wasm")]
48+
gloo_timers::future::sleep(duration).await;
49+
}

0 commit comments

Comments
 (0)