leptos_use/
signal_throttled.rs

1use crate::utils::{signal_filtered, signal_filtered_local};
2use crate::{use_throttle_fn_with_options, ThrottleOptions};
3use leptos::prelude::*;
4use leptos::reactive::wrappers::read::Signal;
5
6signal_filtered!(
7    /// Throttle changing of a `Signal` value.
8    ///
9    /// Use `*_local` variants for values that are not `Send + Sync`.
10    ///
11    /// ## Demo
12    ///
13    /// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/signal_throttled)
14    ///
15    /// ## Usage
16    ///
17    /// ```
18    /// # use leptos::prelude::*;
19    /// # use leptos_use::{signal_throttled, signal_throttled_local};
20    /// # use std::cell::RefCell;
21    /// #
22    /// # #[component]
23    /// # fn Demo() -> impl IntoView {
24    /// let (input, set_input) = signal("");
25    /// let throttled: Signal<&'static str> = signal_throttled(input, 1000.0);
26    ///
27    /// let (input_local, set_input_local) = signal_local(RefCell::new(0));
28    /// let throttled_local: Signal<RefCell<i32>, _> = signal_throttled_local(input_local, 1000.0);
29    /// #
30    /// # view! { }
31    /// # }
32    /// ```
33    ///
34    /// ### Options
35    ///
36    /// The usual throttle options `leading` and `trailing` are available.
37    ///
38    /// ```
39    /// # use leptos::prelude::*;
40    /// # use leptos_use::{signal_throttled_with_options, signal_throttled_local_with_options, ThrottleOptions};
41    /// # use std::cell::RefCell;
42    /// #
43    /// # #[component]
44    /// # fn Demo() -> impl IntoView {
45    /// let (input, set_input) = signal("");
46    /// let throttled: Signal<&'static str> = signal_throttled_with_options(
47    ///     input,
48    ///     1000.0,
49    ///     ThrottleOptions::default().leading(false).trailing(true)
50    /// );
51    ///
52    /// let (input_local, set_input_local) = signal_local(RefCell::new(0));
53    /// let throttled_local: Signal<RefCell<i32>, _> = signal_throttled_local_with_options(
54    ///     input_local,
55    ///     1000.0,
56    ///     ThrottleOptions::default().leading(false).trailing(true)
57    /// );
58    /// #
59    /// # view! { }
60    /// # }
61    /// ```
62    ///
63    /// ## Recommended Reading
64    ///
65    /// - [**Debounce vs Throttle**: Definitive Visual Guide](https://redd.one/blog/debounce-vs-throttle)
66    /// - [Debouncing and Throttling Explained Through Examples](https://css-tricks.com/debouncing-throttling-explained-examples/)
67    ///
68    /// ## Server-Side Rendering
69    ///
70    /// Internally this uses `setTimeout` which is not supported on the server. So usually
71    /// a throttled signal on the server will simply be ignored.
72    throttle
73    /// [`signal_throttled`]
74    /// [`ThrottleOptions`]
75);
76
77signal_filtered_local!(
78    /// Throttle changing of a `Signal` value that is not `Send + Sync`.
79    ///
80    /// See ['signal_throttled`] for docs.
81    throttle
82    /// [`signal_throttled_local`]
83    /// [`ThrottledOptions`]
84);