2023-06-08 01:19:36 +01:00
use crate ::utils ::{ create_filter_wrapper , create_filter_wrapper_with_arg , throttle_filter } ;
2023-05-19 00:58:48 +01:00
use leptos ::MaybeSignal ;
use std ::cell ::RefCell ;
use std ::rc ::Rc ;
pub use crate ::utils ::ThrottleOptions ;
/// Throttle execution of a function.
/// Especially useful for rate limiting execution of handlers on events like resize and scroll.
///
2023-05-19 20:43:03 +01:00
/// > Throttle is a spring that throws balls: After a ball flies out it needs some time to shrink back, so it cannot throw any more balls until it's ready.
2023-05-19 00:58:48 +01:00
///
/// ## Demo
///
2023-05-27 03:00:08 +01:00
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_throttle_fn)
2023-05-19 00:58:48 +01:00
///
/// ## Usage
///
/// ```
2023-05-26 19:27:42 +01:00
/// # use leptos::*;
/// # use leptos_use::use_throttle_fn;
/// #
/// # #[component]
/// # fn Demo(cx: Scope) -> impl IntoView {
/// let mut throttled_fn = use_throttle_fn(
/// || {
/// // do something, it will be called at most 1 time per second
/// },
/// 1000.0,
/// );
/// view! { cx,
/// <button on:click=move |_| { throttled_fn(); }>
/// "Smash me!"
/// </button>
2023-05-19 00:58:48 +01:00
/// }
2023-05-26 19:27:42 +01:00
/// # }
2023-05-19 00:58:48 +01:00
/// ```
///
/// You can provide options when you use [`use_throttle_fn_with_options`].
///
/// ```
/// # use leptos::*;
/// # use leptos_use::{ThrottleOptions, use_throttle_fn_with_options};
/// # #[component]
/// # fn Demo(cx: Scope) -> impl IntoView {
2023-05-26 18:09:01 +01:00
/// let throttled_fn = use_throttle_fn_with_options(
2023-05-19 00:58:48 +01:00
/// || {
/// // do something, it will be called at most 1 time per second
/// },
/// 1000.0,
2023-05-27 15:12:05 +01:00
/// ThrottleOptions::default()
/// .leading(true)
/// .trailing(true),
2023-05-19 00:58:48 +01:00
/// );
/// # view! { cx, }
/// # }
/// ```
///
2023-05-26 18:09:01 +01:00
/// If you want to throttle a function that takes an argument there are also the versions
2023-05-19 00:58:48 +01:00
/// [`use_throttle_fn_with_args`] and [`use_throttle_fn_with_args_and_options`].
///
/// ## Recommended Reading
///
/// - [**Debounce vs Throttle**: Definitive Visual Guide](https://redd.one/blog/debounce-vs-throttle)
2023-06-09 23:10:33 +01:00
/// - [Debouncing and Throttling Explained Through Examples](https://css-tricks.com/debouncing-throttling-explained-examples/)
2023-05-19 00:58:48 +01:00
pub fn use_throttle_fn < F , R > (
func : F ,
2023-06-08 01:19:36 +01:00
ms : impl Into < MaybeSignal < f64 > > + 'static ,
2023-05-28 17:42:16 +01:00
) -> impl Fn ( ) -> Rc < RefCell < Option < R > > > + Clone
2023-05-19 00:58:48 +01:00
where
2023-05-26 18:09:01 +01:00
F : FnOnce ( ) -> R + Clone + 'static ,
2023-05-19 00:58:48 +01:00
R : 'static ,
{
use_throttle_fn_with_options ( func , ms , Default ::default ( ) )
}
/// Version of [`use_throttle_fn`] with throttle options. See the docs for [`use_throttle_fn`] for how to use.
pub fn use_throttle_fn_with_options < F , R > (
func : F ,
2023-06-08 01:19:36 +01:00
ms : impl Into < MaybeSignal < f64 > > + 'static ,
2023-05-19 00:58:48 +01:00
options : ThrottleOptions ,
2023-05-28 17:42:16 +01:00
) -> impl Fn ( ) -> Rc < RefCell < Option < R > > > + Clone
2023-05-19 00:58:48 +01:00
where
2023-05-26 18:09:01 +01:00
F : FnOnce ( ) -> R + Clone + 'static ,
2023-05-19 00:58:48 +01:00
R : 'static ,
{
2023-06-08 01:19:36 +01:00
create_filter_wrapper ( Box ::new ( throttle_filter ( ms , options ) ) , func )
2023-05-19 00:58:48 +01:00
}
/// Version of [`use_throttle_fn`] with an argument for the throttled function. See the docs for [`use_throttle_fn`] for how to use.
pub fn use_throttle_fn_with_arg < F , Arg , R > (
func : F ,
2023-06-08 01:19:36 +01:00
ms : impl Into < MaybeSignal < f64 > > + 'static ,
2023-05-28 17:42:16 +01:00
) -> impl Fn ( Arg ) -> Rc < RefCell < Option < R > > > + Clone
2023-05-19 00:58:48 +01:00
where
2023-05-26 18:09:01 +01:00
F : FnOnce ( Arg ) -> R + Clone + 'static ,
Arg : Clone + 'static ,
2023-05-19 00:58:48 +01:00
R : 'static ,
{
use_throttle_fn_with_arg_and_options ( func , ms , Default ::default ( ) )
}
/// Version of [`use_throttle_fn_with_arg`] with throttle options. See the docs for [`use_throttle_fn`] for how to use.
pub fn use_throttle_fn_with_arg_and_options < F , Arg , R > (
func : F ,
2023-06-08 01:19:36 +01:00
ms : impl Into < MaybeSignal < f64 > > + 'static ,
2023-05-19 00:58:48 +01:00
options : ThrottleOptions ,
2023-05-28 17:42:16 +01:00
) -> impl Fn ( Arg ) -> Rc < RefCell < Option < R > > > + Clone
2023-05-19 00:58:48 +01:00
where
2023-05-26 18:09:01 +01:00
F : FnOnce ( Arg ) -> R + Clone + 'static ,
Arg : Clone + 'static ,
2023-05-19 00:58:48 +01:00
R : 'static ,
{
2023-06-08 01:19:36 +01:00
create_filter_wrapper_with_arg ( Box ::new ( throttle_filter ( ms , options ) ) , func )
2023-05-19 00:58:48 +01:00
}