Throttled or debounced callbacks (in watch_* or *_fn) no longer are called after the containing scope was cleaned up.

Closes #63
This commit is contained in:
Maccesch 2024-01-10 23:50:52 +00:00
parent 84396b7675
commit 752cf1f832
7 changed files with 17 additions and 2 deletions

View file

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `UseMouseReturn` signals `x`, `y`, and `source_type` are now of type `Signal<f64>` instead of `ReadSignal<f64>`.
- You can now convert `leptos::html::HtmlElement<T>` into `Element(s)MaybeSignal`. This should make functions a lot easier to use in directives.
- There's now a chapter in the book especially for `Element(s)MaybeSignal`.
- Throttled or debounced callbacks (in watch_* or *_fn) no longer are called after the containing scope was cleaned up.
## [0.9.0] - 2023-12-06

View file

@ -33,6 +33,8 @@ use std::rc::Rc;
/// # }
/// ```
///
/// Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.
///
/// 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).
///

View file

@ -36,6 +36,8 @@ pub use crate::utils::ThrottleOptions;
/// # }
/// ```
///
/// Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.
///
/// You can provide options when you use [`use_throttle_fn_with_options`].
///
/// ```

View file

@ -3,7 +3,7 @@
use cfg_if::cfg_if;
use default_struct_builder::DefaultBuilder;
use leptos::leptos_dom::helpers::TimeoutHandle;
use leptos::{set_timeout_with_handle, MaybeSignal, SignalGetUntracked};
use leptos::{on_cleanup, set_timeout_with_handle, MaybeSignal, SignalGetUntracked};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::time::Duration;
@ -34,6 +34,10 @@ where
}
};
on_cleanup(move || {
clear_timeout(&timer);
});
let ms = ms.into();
let max_wait_signal = options.max_wait;

View file

@ -4,7 +4,7 @@ use crate::core::now;
use cfg_if::cfg_if;
use default_struct_builder::DefaultBuilder;
use leptos::leptos_dom::helpers::TimeoutHandle;
use leptos::{set_timeout_with_handle, MaybeSignal, SignalGetUntracked};
use leptos::{on_cleanup, set_timeout_with_handle, MaybeSignal, SignalGetUntracked};
use std::cell::{Cell, RefCell};
use std::cmp::max;
use std::rc::Rc;
@ -47,6 +47,8 @@ where
}
};
on_cleanup(clear.clone());
let ms = ms.into();
move |mut _invoke: Rc<dyn Fn() -> R>| {

View file

@ -32,6 +32,8 @@ use leptos::*;
///
/// This really is only shorthand shorthand for `watch_with_options(deps, callback, WatchOptions::default().debounce(ms))`.
///
/// Please note that if the current component is cleaned up before the debounced callback is called, the debounced callback will not be called.
///
/// There's also `watch_debounced_with_options` where you can specify the other watch options (except `filter`).
///
/// ```

View file

@ -31,6 +31,8 @@ use default_struct_builder::DefaultBuilder;
///
/// This really is only shorthand shorthand for `watch_with_options(deps, callback, WatchOptions::default().throttle(ms))`.
///
/// Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.
///
/// There's also `watch_throttled_with_options` where you can specify the other watch options (except `filter`).
///
/// ```