@@ -7,9 +7,9 @@ use futures::{channel::mpsc, SinkExt, StreamExt};
77use std:: time:: Duration ;
88
99/// The interface to a timeout.
10- ///
10+ ///
1111/// This is used to trigger the timeout with [`UseTimeout::action`].
12- ///
12+ ///
1313/// See [`use_timeout`] for more information.
1414pub struct UseTimeout < Args : ' static > {
1515 duration : Duration ,
@@ -18,12 +18,12 @@ pub struct UseTimeout<Args: 'static> {
1818
1919impl < Args > UseTimeout < Args > {
2020 /// Trigger the timeout.
21- ///
21+ ///
2222 /// If no arguments are desired, use the [`unit`] type.
2323 /// See [`use_timeout`] for more information.
2424 pub fn action ( & self , args : Args ) -> TimeoutHandle {
2525 let mut sender = ( self . sender ) ( ) ;
26- let duration = self . duration . clone ( ) ;
26+ let duration = self . duration ;
2727
2828 let handle = spawn ( async move {
2929 #[ cfg( not( target_family = "wasm" ) ) ]
@@ -42,24 +42,21 @@ impl<Args> UseTimeout<Args> {
4242
4343impl < Args > Clone for UseTimeout < Args > {
4444 fn clone ( & self ) -> Self {
45- Self {
46- duration : self . duration . clone ( ) ,
47- sender : self . sender . clone ( ) ,
48- }
45+ * self
4946 }
5047}
5148impl < Args > Copy for UseTimeout < Args > { }
5249impl < Args > PartialEq for UseTimeout < Args > {
5350 fn eq ( & self , other : & Self ) -> bool {
54- self . duration == other. duration && self . sender == self . sender
51+ self . duration == other. duration && self . sender == other . sender
5552 }
5653}
5754
5855/// A handle to a pending timeout.
59- ///
60- /// A handle to a running timeout triggered with [`UseTimeout::action`].
56+ ///
57+ /// A handle to a running timeout triggered with [`UseTimeout::action`].
6158/// This handle allows you to cancel the timeout from triggering with [`TimeoutHandle::cancel`]
62- ///
59+ ///
6360/// See [`use_timeout`] for more information.
6461#[ derive( Clone , Copy , PartialEq ) ]
6562pub struct TimeoutHandle {
@@ -74,13 +71,13 @@ impl TimeoutHandle {
7471}
7572
7673/// A hook to run a callback after a period of time.
77- ///
74+ ///
7875/// Timeouts allow you to trigger a callback that occurs after a period of time. Unlike a debounce, a timeout will not
79- /// reset it's timer when triggered again. Instead, calling a timeout while it is already running will start another instance
76+ /// reset it's timer when triggered again. Instead, calling a timeout while it is already running will start another instance
8077/// to run the callback after the provided period.
81- ///
78+ ///
8279/// This hook is similar to the web [setTimeout()](https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout) API.
83- ///
80+ ///
8481/// # Examples
8582///
8683/// Example of using a timeout:
@@ -91,22 +88,22 @@ impl TimeoutHandle {
9188///
9289/// #[component]
9390/// fn App() -> Element {
94- /// // Create a timeout for two seconds.
91+ /// // Create a timeout for two seconds.
9592/// // Once triggered, this timeout will print "timeout called" after two seconds.
9693/// let timeout = use_timeout(Duration::from_secs(2), |()| println!("timeout called"));
9794///
9895/// rsx! {
9996/// button {
10097/// onclick: move |_| {
101- /// // Trigger the timeout.
98+ /// // Trigger the timeout.
10299/// timeout.action(());
103100/// },
104101/// "Click!"
105102/// }
106103/// }
107104/// }
108105/// ```
109- ///
106+ ///
110107/// #### Cancelling Timeouts
111108/// Example of cancelling a timeout. This is the equivalent of a debounce.
112109/// ```rust
@@ -129,7 +126,7 @@ impl TimeoutHandle {
129126/// if let Some(handle) = *current_timeout.read() {
130127/// handle.cancel();
131128/// }
132- ///
129+ ///
133130/// // Trigger the timeout.
134131/// let handle = timeout.action(());
135132/// current_timeout.set(Some(handle));
@@ -139,7 +136,7 @@ impl TimeoutHandle {
139136/// }
140137/// }
141138/// ```
142- ///
139+ ///
143140/// #### Async Timeouts
144141/// Timeouts can accept an async callback:
145142/// ```rust
@@ -149,7 +146,7 @@ impl TimeoutHandle {
149146///
150147/// #[component]
151148/// fn App() -> Element {
152- /// // Create a timeout for two seconds.
149+ /// // Create a timeout for two seconds.
153150/// // We use an async sleep to wait an even longer duration after the timeout is called.
154151/// let timeout = use_timeout(Duration::from_secs(2), |()| async {
155152/// println!("Timeout after two total seconds.");
@@ -160,7 +157,7 @@ impl TimeoutHandle {
160157/// rsx! {
161158/// button {
162159/// onclick: move |_| {
163- /// // Trigger the timeout.
160+ /// // Trigger the timeout.
164161/// timeout.action(());
165162/// },
166163/// "Click!"
0 commit comments