2023-05-27 03:00:08 +01:00
pub use crate ::utils ::DebounceOptions ;
use crate ::utils ::{ create_filter_wrapper , create_filter_wrapper_with_arg , debounce_filter } ;
2023-05-26 18:09:01 +01:00
use leptos ::MaybeSignal ;
2023-05-26 19:27:42 +01:00
/// Debounce execution of a function.
///
/// > Debounce is an overloaded waiter: If you keep asking him your requests will be ignored until you stop and give him some time to think about your latest inquiry.
///
/// ## Demo
///
2023-05-27 03:00:08 +01:00
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_debounce_fn)
2023-05-26 19:27:42 +01:00
///
/// ## Usage
///
/// ```
/// # use leptos::*;
/// # use leptos::ev::resize;
/// # use leptos_use::use_debounce_fn;
/// #
/// # #[component]
/// # fn Demo(cx: Scope) -> impl IntoView {
/// let mut debounced_fn = use_debounce_fn(
/// || {
/// // do something
/// },
/// 1000.0,
/// );
///
/// window_event_listener(resize, move |_| debounced_fn());
/// # view! { cx, }
/// # }
/// ```
///
/// You can also pass options to [`use_debounce_fn_with_options`] with a maximum wait time, similar to
/// [lodash debounce](https://lodash.com/docs/#debounce).
///
/// ```
/// # use leptos::*;
/// # use leptos::ev::resize;
/// # use leptos_use::use_debounce_fn_with_options;
/// # use leptos_use::utils::DebounceOptions;
/// #
/// # #[component]
/// # fn Demo(cx: Scope) -> impl IntoView {
/// let mut debounced_fn = use_debounce_fn_with_options(
/// || {
/// // do something
/// },
/// 1000.0,
2023-05-27 15:12:05 +01:00
/// DebounceOptions::default()
/// .max_wait(Some(5000.0)),
2023-05-26 19:27:42 +01:00
/// );
///
/// window_event_listener(resize, move |_| debounced_fn());
/// # view! { cx, }
/// # }
/// ```
///
/// Currently there is no way to use a function with a return value. Please open an issue if you need this.
///
/// If you want to throttle a function that takes an argument there are also the versions
/// [`use_debounce_fn_with_args`] and [`use_debounce_fn_with_args_and_options`].
///
/// ## Recommended Reading
///
/// - [**Debounce vs Throttle**: Definitive Visual Guide](https://redd.one/blog/debounce-vs-throttle)
2023-05-29 01:52:03 +01:00
pub fn use_debounce_fn < F > ( func : F , ms : impl Into < MaybeSignal < f64 > > ) -> impl Fn ( ) + Clone
2023-05-26 18:09:01 +01:00
where
F : FnOnce ( ) + Clone + 'static ,
{
2023-05-27 15:12:05 +01:00
use_debounce_fn_with_options ( func , ms , DebounceOptions ::default ( ) )
2023-05-26 18:09:01 +01:00
}
2023-05-26 19:27:42 +01:00
/// Version of [`use_debounce_fn`] with debounce options. See the docs for [`use_debounce_fn`] for how to use.
2023-05-27 15:12:05 +01:00
pub fn use_debounce_fn_with_options < F > (
2023-05-26 18:09:01 +01:00
func : F ,
ms : impl Into < MaybeSignal < f64 > > ,
2023-05-27 15:12:05 +01:00
options : DebounceOptions ,
2023-05-29 01:52:03 +01:00
) -> impl Fn ( ) + Clone
2023-05-26 18:09:01 +01:00
where
F : FnOnce ( ) + Clone + 'static ,
{
create_filter_wrapper ( debounce_filter ( ms , options ) , func )
}
2023-05-26 19:27:42 +01:00
/// Version of [`use_debounce_fn`] with an argument for the debounced function. See the docs for [`use_debounce_fn`] for how to use.
2023-05-27 03:00:08 +01:00
pub fn use_debounce_fn_with_arg < F , Arg > (
func : F ,
ms : impl Into < MaybeSignal < f64 > > ,
) -> impl Fn ( Arg ) + Clone
2023-05-26 18:09:01 +01:00
where
F : FnOnce ( Arg ) + Clone + 'static ,
Arg : Clone + 'static ,
{
2023-05-27 15:12:05 +01:00
use_debounce_fn_with_arg_and_options ( func , ms , DebounceOptions ::default ( ) )
2023-05-26 18:09:01 +01:00
}
2023-05-27 03:00:08 +01:00
/// Version of [`use_debounce_fn_with_arg`] with debounce options.
2023-05-27 15:12:05 +01:00
pub fn use_debounce_fn_with_arg_and_options < F , Arg > (
2023-05-26 18:09:01 +01:00
func : F ,
ms : impl Into < MaybeSignal < f64 > > ,
2023-05-27 15:12:05 +01:00
options : DebounceOptions ,
2023-05-27 03:00:08 +01:00
) -> impl Fn ( Arg ) + Clone
2023-05-26 18:09:01 +01:00
where
F : FnOnce ( Arg ) + Clone + 'static ,
Arg : Clone + 'static ,
{
create_filter_wrapper_with_arg ( debounce_filter ( ms , options ) , func )
}