leptos-use/src/signal_throttled.rs

83 lines
2 KiB
Rust
Raw Normal View History

2023-07-17 18:55:42 +01:00
use crate::{use_throttle_fn_with_options, ThrottleOptions};
use leptos::*;
2023-07-18 11:17:36 +01:00
/// Throttle changing of a `Signal` value.
2023-07-17 18:55:42 +01:00
///
/// ## Demo
///
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/signal_throttled)
///
/// ## Usage
///
/// ```
/// # use leptos::*;
/// # use leptos_use::signal_throttled;
/// #
/// # #[component]
/// # fn Demo(cx: Scope) -> impl IntoView {
/// let (input, set_input) = create_signal(cx, "");
/// let throttled = signal_throttled(cx, input, 1000.0);
/// #
/// # view! { cx, }
/// # }
/// ```
2023-07-18 11:17:36 +01:00
///
/// ### Options
///
/// The usual throttle options `leading` and `trailing` are available.
///
/// ```
/// # use leptos::*;
/// # use leptos_use::{signal_throttled_with_options, ThrottleOptions};
/// #
/// # #[component]
/// # fn Demo(cx: Scope) -> impl IntoView {
/// let (input, set_input) = create_signal(cx, "");
/// let throttled = signal_throttled_with_options(cx, input, 1000.0, ThrottleOptions::default().leading(false).trailing(true));
/// #
/// # view! { cx, }
/// # }
/// ```
2023-07-17 18:55:42 +01:00
pub fn signal_throttled<S, T>(
cx: Scope,
value: S,
ms: impl Into<MaybeSignal<f64>> + 'static,
) -> Signal<T>
where
S: Into<Signal<T>>,
T: Clone + 'static,
{
signal_throttled_with_options(cx, value, ms, ThrottleOptions::default())
}
/// Version of [`signal_throttled`] that takes a `SignalThrottledOptions`. See [`signal_throttled`] for how to use.
pub fn signal_throttled_with_options<S, T>(
cx: Scope,
value: S,
ms: impl Into<MaybeSignal<f64>> + 'static,
options: ThrottleOptions,
) -> Signal<T>
where
S: Into<Signal<T>>,
T: Clone + 'static,
{
let value = value.into();
let ms = ms.into();
if ms.get_untracked() <= 0.0 {
return value;
}
let (throttled, set_throttled) = create_signal(cx, value.get_untracked());
let update = use_throttle_fn_with_options(
move || set_throttled.set(value.get_untracked()),
ms,
options,
);
let _ = watch(cx, move || value.get(), move |_, _, _| update(), false);
throttled.into()
}