From 6fab76c13f0359e8ef0401a41ac39dd5169ec16c Mon Sep 17 00:00:00 2001 From: Maccesch Date: Mon, 24 Jul 2023 21:16:59 +0200 Subject: [PATCH] moved to leptos::watch --- docs/book/src/SUMMARY.md | 2 +- .../watch/{watch.md => watch_with_options.md} | 2 +- src/lib.rs | 4 +- src/use_css_var.rs | 3 +- src/use_cycle_list.rs | 8 +- src/use_favicon.rs | 2 +- src/use_interval_fn.rs | 2 +- src/use_mutation_observer.rs | 5 +- src/{watch.rs => watch_with_options.rs} | 122 ++++++++---------- 9 files changed, 68 insertions(+), 82 deletions(-) rename docs/book/src/watch/{watch.md => watch_with_options.md} (73%) rename src/{watch.rs => watch_with_options.rs} (70%) diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index bea09e9..0542b5d 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -56,10 +56,10 @@ # Watch -- [watch](watch/watch.md) - [watch_debounced](watch/watch_debounced.md) - [watch_pausable](watch/watch_pausable.md) - [watch_throttled](watch/watch_throttled.md) +- [watch_with_options](watch/watch_with_options.md) - [whenever](watch/whenever.md) # Utilities diff --git a/docs/book/src/watch/watch.md b/docs/book/src/watch/watch_with_options.md similarity index 73% rename from docs/book/src/watch/watch.md rename to docs/book/src/watch/watch_with_options.md index 6087512..b45a7d3 100644 --- a/docs/book/src/watch/watch.md +++ b/docs/book/src/watch/watch_with_options.md @@ -1,3 +1,3 @@ -# watch +# watch_with_options diff --git a/src/lib.rs b/src/lib.rs index 2b33de2..f4e14c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,10 +56,10 @@ mod use_to_string; mod use_websocket; mod use_window_focus; mod use_window_scroll; -mod watch; mod watch_debounced; mod watch_pausable; mod watch_throttled; +mod watch_with_options; mod whenever; pub use is_err::*; @@ -98,8 +98,8 @@ pub use use_to_string::*; pub use use_websocket::*; pub use use_window_focus::*; pub use use_window_scroll::*; -pub use watch::*; pub use watch_debounced::*; pub use watch_pausable::*; pub use watch_throttled::*; +pub use watch_with_options::*; pub use whenever::*; diff --git a/src/use_css_var.rs b/src/use_css_var.rs index d60ca8f..254fbe5 100644 --- a/src/use_css_var.rs +++ b/src/use_css_var.rs @@ -1,7 +1,7 @@ #![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))] use crate::core::ElementMaybeSignal; -use crate::{use_mutation_observer_with_options, watch, watch_with_options, WatchOptions}; +use crate::{use_mutation_observer_with_options, watch_with_options, WatchOptions}; use cfg_if::cfg_if; use default_struct_builder::DefaultBuilder; use leptos::*; @@ -170,6 +170,7 @@ where let _ = style.set_property(&prop.get_untracked(), val); } }, + false, ); }} diff --git a/src/use_cycle_list.rs b/src/use_cycle_list.rs index 5142887..a3acc3b 100644 --- a/src/use_cycle_list.rs +++ b/src/use_cycle_list.rs @@ -1,5 +1,4 @@ use crate::core::MaybeRwSignal; -use crate::watch; use default_struct_builder::DefaultBuilder; use leptos::*; @@ -158,7 +157,12 @@ where let _ = { let set = set.clone(); - watch(cx, move || list.get(), move |_, _, _| set(index.get())) + leptos::watch( + cx, + move || list.get(), + move |_, _, _| set(index.get()), + false, + ) }; UseCycleListReturn { diff --git a/src/use_favicon.rs b/src/use_favicon.rs index 7d7aa53..93f5040 100644 --- a/src/use_favicon.rs +++ b/src/use_favicon.rs @@ -1,7 +1,6 @@ #![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))] use crate::core::MaybeRwSignal; -use crate::watch; use cfg_if::cfg_if; use default_struct_builder::DefaultBuilder; use leptos::*; @@ -104,6 +103,7 @@ pub fn use_favicon_with_options( } } }, + false, ); }} diff --git a/src/use_interval_fn.rs b/src/use_interval_fn.rs index 2c03bf8..dc8072b 100644 --- a/src/use_interval_fn.rs +++ b/src/use_interval_fn.rs @@ -1,7 +1,6 @@ #![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))] use crate::utils::Pausable; -use crate::watch; use cfg_if::cfg_if; use default_struct_builder::DefaultBuilder; use leptos::leptos_dom::helpers::IntervalHandle; @@ -127,6 +126,7 @@ where resume(); } }, + false, ); on_cleanup(cx, stop_watch); } diff --git a/src/use_mutation_observer.rs b/src/use_mutation_observer.rs index 3085f9f..f96d726 100644 --- a/src/use_mutation_observer.rs +++ b/src/use_mutation_observer.rs @@ -1,5 +1,5 @@ use crate::core::ElementsMaybeSignal; -use crate::{use_supported, watch}; +use crate::use_supported; use leptos::*; use std::cell::RefCell; use std::rc::Rc; @@ -109,7 +109,7 @@ where let stop_watch = { let cleanup = cleanup.clone(); - watch( + leptos::watch( cx, move || targets.get(), move |targets, _, _| { @@ -127,6 +127,7 @@ where observer.replace(Some(obs)); } }, + false, ) }; diff --git a/src/watch.rs b/src/watch_with_options.rs similarity index 70% rename from src/watch.rs rename to src/watch_with_options.rs index 7b017d6..8d98d95 100644 --- a/src/watch.rs +++ b/src/watch_with_options.rs @@ -5,43 +5,13 @@ use leptos::*; use std::cell::RefCell; use std::rc::Rc; -/// A version of `create_effect` that listens to any dependency that is accessed inside `deps`. -/// Also a stop handler is returned. -/// The return value of `deps` is passed into `callback` as an argument together with the previous value -/// and the previous value that the `callback` itself returned the last time it ran. -/// -/// ## Usage -/// -/// ``` -/// # use std::time::Duration; -/// # use leptos::*; -/// # use leptos_use::watch; -/// # -/// # pub fn Demo(cx: Scope) -> impl IntoView { -/// let (num, set_num) = create_signal(cx, 0); -/// -/// let stop = watch( -/// cx, -/// move || num.get(), -/// move |num, _, _| { -/// log!("Number {}", num); -/// }, -/// ); -/// -/// set_num.set(1); // > "Number 1" -/// -/// set_timeout_with_handle(move || { -/// stop(); // stop watching -/// -/// set_num.set(2); // (nothing happens) -/// }, Duration::from_millis(1000)); -/// # view! { cx, } -/// # } -/// ``` +/// A version of `leptos::watch` but with additional options. /// /// ## Immediate /// -/// If `immediate` is true, the `callback` will run immediately. +/// This is the same as for `leptos::watch`. But you don't have to specify it. +/// By default its set to `false`. +/// If `immediate` is `true`, the `callback` will run immediately (this is also true if throttled/debounced). /// If it's `false`, the `callback` will run only after /// the first change is detected of any signal that is accessed in `deps`. /// @@ -112,21 +82,12 @@ use std::rc::Rc; /// /// On the server this works just fine except if you throttle or debounce in which case the callback /// will never be called except if you set `immediate` to `true` in which case the callback will be -/// called exactly once. +/// called exactly once when `watch()` is executed. /// /// ## See also /// /// * [`watch_throttled`] /// * [`watch_debounced`] -pub fn watch(cx: Scope, deps: DFn, callback: CFn) -> impl Fn() + Clone -where - DFn: Fn() -> W + 'static, - CFn: Fn(&W, Option<&W>, Option) -> T + Clone + 'static, - W: Clone + 'static, - T: Clone + 'static, -{ - watch_with_options(cx, deps, callback, WatchOptions::default()) -} /// Version of `watch` that accepts `WatchOptions`. See [`watch`] for how to use. pub fn watch_with_options( @@ -141,8 +102,6 @@ where W: Clone + 'static, T: Clone + 'static, { - let (is_active, set_active) = create_signal(cx, true); - let cur_deps_value: Rc>> = Rc::new(RefCell::new(None)); let prev_deps_value: Rc>> = Rc::new(RefCell::new(None)); let prev_callback_value: Rc>> = Rc::new(RefCell::new(None)); @@ -167,34 +126,43 @@ where let filtered_callback = create_filter_wrapper(options.filter.filter_fn(), wrapped_callback.clone()); - create_effect(cx, move |did_run_before| { - if !is_active.get() { - return; - } + leptos::watch( + cx, + deps, + move |deps_value, previous_deps_value, did_run_before| { + cur_deps_value.replace(Some(deps_value.clone())); + prev_deps_value.replace(previous_deps_value.cloned()); - let deps_value = deps(); + let callback_value = if options.immediate && did_run_before.is_none() { + Some(wrapped_callback()) + } else { + filtered_callback().take() + }; - if !options.immediate && did_run_before.is_none() { - prev_deps_value.replace(Some(deps_value)); - return; - } + prev_callback_value.replace(callback_value); + }, + options.immediate, + ) - cur_deps_value.replace(Some(deps_value.clone())); - - let callback_value = if options.immediate && did_run_before.is_none() { - Some(wrapped_callback()) - } else { - filtered_callback().take() - }; - - prev_callback_value.replace(callback_value); - - prev_deps_value.replace(Some(deps_value)); - }); - - move || { - set_active.set(false); - } + // create_effect(cx, move |did_run_before| { + // if !is_active.get() { + // return; + // } + // + // let deps_value = deps(); + // + // if !options.immediate && did_run_before.is_none() { + // prev_deps_value.replace(Some(deps_value)); + // return; + // } + // + // cur_deps_value.replace(Some(deps_value.clone())); + // + // + // prev_deps_value.replace(Some(deps_value)); + // }); + // + // } /// Options for `watch_with_options` @@ -216,3 +184,15 @@ impl WatchOptions { filter ); } + +#[deprecated(since = "0.7.0", note = "Use `leptos::watch` instead")] +#[inline(always)] +pub fn watch(cx: Scope, deps: DFn, callback: CFn) -> impl Fn() + Clone +where + DFn: Fn() -> W + 'static, + CFn: Fn(&W, Option<&W>, Option) -> T + Clone + 'static, + W: Clone + 'static, + T: Clone + 'static, +{ + leptos::watch(cx, deps, callback, false) +}