moved to leptos::watch

This commit is contained in:
Maccesch 2023-07-24 21:16:59 +02:00
parent 8f96b10f13
commit 6fab76c13f
9 changed files with 68 additions and 82 deletions

View file

@ -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

View file

@ -1,3 +1,3 @@
# watch
# watch_with_options
<!-- cmdrun python3 ../extract_doc_comment.py watch -->

View file

@ -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::*;

View file

@ -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,
);
}}

View file

@ -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 {

View file

@ -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,
);
}}

View file

@ -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);
}

View file

@ -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,
)
};

View file

@ -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<W, T, DFn, CFn>(cx: Scope, deps: DFn, callback: CFn) -> impl Fn() + Clone
where
DFn: Fn() -> W + 'static,
CFn: Fn(&W, Option<&W>, Option<T>) -> 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<W, T, DFn, CFn>(
@ -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<RefCell<Option<W>>> = Rc::new(RefCell::new(None));
let prev_deps_value: Rc<RefCell<Option<W>>> = Rc::new(RefCell::new(None));
let prev_callback_value: Rc<RefCell<Option<T>>> = 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<W, T, DFn, CFn>(cx: Scope, deps: DFn, callback: CFn) -> impl Fn() + Clone
where
DFn: Fn() -> W + 'static,
CFn: Fn(&W, Option<&W>, Option<T>) -> T + Clone + 'static,
W: Clone + 'static,
T: Clone + 'static,
{
leptos::watch(cx, deps, callback, false)
}