leptos-use/src/signal_throttled.rs

62 lines
1.8 KiB
Rust
Raw Normal View History

2023-07-24 23:18:11 +01:00
use crate::utils::signal_filtered;
2023-07-17 18:55:42 +01:00
use crate::{use_throttle_fn_with_options, ThrottleOptions};
use leptos::*;
2023-07-24 23:18:11 +01:00
use paste::paste;
2023-07-17 18:55:42 +01:00
2023-07-24 23:18:11 +01:00
signal_filtered!(
/// Throttle changing of a `Signal` value.
///
/// ## Demo
///
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/signal_throttled)
///
/// ## Usage
///
/// ```
/// # use leptos::*;
/// # use leptos_use::signal_throttled;
/// #
/// # #[component]
2023-07-27 18:06:36 +01:00
/// # fn Demo() -> impl IntoView {
/// let (input, set_input) = create_signal("");
/// let throttled: Signal<&'static str> = signal_throttled(input, 1000.0);
2023-07-24 23:18:11 +01:00
/// #
2023-07-27 18:06:36 +01:00
/// # view! { }
2023-07-24 23:18:11 +01:00
/// # }
/// ```
///
/// ### Options
///
/// The usual throttle options `leading` and `trailing` are available.
///
/// ```
/// # use leptos::*;
/// # use leptos_use::{signal_throttled_with_options, ThrottleOptions};
/// #
/// # #[component]
2023-07-27 18:06:36 +01:00
/// # fn Demo() -> impl IntoView {
/// let (input, set_input) = create_signal("");
/// let throttled: Signal<&'static str> = signal_throttled_with_options(
2023-07-24 23:18:11 +01:00
/// input,
/// 1000.0,
/// ThrottleOptions::default().leading(false).trailing(true)
/// );
/// #
2023-07-27 18:06:36 +01:00
/// # view! { }
2023-07-24 23:18:11 +01:00
/// # }
/// ```
///
/// ## Recommended Reading
///
/// - [**Debounce vs Throttle**: Definitive Visual Guide](https://redd.one/blog/debounce-vs-throttle)
/// - [Debouncing and Throttling Explained Through Examples](https://css-tricks.com/debouncing-throttling-explained-examples/)
///
/// ## Server-Side Rendering
///
/// Internally this uses `setTimeout` which is not supported on the server. So usually
/// a throttled signal on the server will simply be ignored.
throttle
/// [`signal_throttled`]
/// [`ThrottleOptions`]
);