diff --git a/CHANGELOG.md b/CHANGELOG.md index 8249bce..40cc0ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` instead of `ReadSignal`. - You can now convert `leptos::html::HtmlElement` 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 diff --git a/src/use_debounce_fn.rs b/src/use_debounce_fn.rs index d512720..fb99da2 100644 --- a/src/use_debounce_fn.rs +++ b/src/use_debounce_fn.rs @@ -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). /// diff --git a/src/use_throttle_fn.rs b/src/use_throttle_fn.rs index 6652248..2f9e61c 100644 --- a/src/use_throttle_fn.rs +++ b/src/use_throttle_fn.rs @@ -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`]. /// /// ``` diff --git a/src/utils/filters/debounce.rs b/src/utils/filters/debounce.rs index 6186960..be108ea 100644 --- a/src/utils/filters/debounce.rs +++ b/src/utils/filters/debounce.rs @@ -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; diff --git a/src/utils/filters/throttle.rs b/src/utils/filters/throttle.rs index ceda428..e4eb2c4 100644 --- a/src/utils/filters/throttle.rs +++ b/src/utils/filters/throttle.rs @@ -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 R>| { diff --git a/src/watch_debounced.rs b/src/watch_debounced.rs index 55d6b26..b52d2eb 100644 --- a/src/watch_debounced.rs +++ b/src/watch_debounced.rs @@ -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`). /// /// ``` diff --git a/src/watch_throttled.rs b/src/watch_throttled.rs index be45644..00497a3 100644 --- a/src/watch_throttled.rs +++ b/src/watch_throttled.rs @@ -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`). /// /// ```