2023-06-11 17:11:16 +01:00
|
|
|
use crate::{watch_with_options, WatchOptions};
|
|
|
|
|
|
|
|
/// Shorthand for watching a signal to be `true`.
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use leptos::*;
|
2023-09-12 15:24:32 +01:00
|
|
|
/// # use leptos::logging::log;
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # use leptos_use::whenever;
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # pub fn Demo() -> impl IntoView {
|
|
|
|
/// let (is_ready, set_ready) = create_signal(false);
|
2023-06-11 17:11:16 +01:00
|
|
|
///
|
2023-07-27 18:06:36 +01:00
|
|
|
/// whenever(move || is_ready.get(), |v, _, _| log!("{}", v));
|
2023-06-11 17:11:16 +01:00
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # view! { }
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Callback Function
|
|
|
|
///
|
|
|
|
/// Same as [`watch`], the callback will be called with `callback(input, prev_input, prev_return)`.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use leptos::*;
|
2023-09-12 15:24:32 +01:00
|
|
|
/// # use leptos::logging::log;
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # use leptos_use::whenever;
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # pub fn Demo() -> impl IntoView {
|
|
|
|
/// # let (is_ready, set_ready) = create_signal(false);
|
|
|
|
/// whenever(move || is_ready.get(), |value, prev_value, _| {
|
2023-06-11 17:11:16 +01:00
|
|
|
/// log!("before: {prev_value:?}; now: {value}");
|
|
|
|
/// });
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # view! { }
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Computed
|
|
|
|
///
|
|
|
|
/// Same as [`watch`], you can pass a getter function to calculate on each change.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use leptos::*;
|
2023-09-12 15:24:32 +01:00
|
|
|
/// # use leptos::logging::log;
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # use leptos_use::whenever;
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # pub fn Demo() -> impl IntoView {
|
|
|
|
/// # let (counter, set_counter) = create_signal(0);
|
2023-06-11 17:11:16 +01:00
|
|
|
/// whenever(
|
2023-06-21 13:09:00 +02:00
|
|
|
/// move || counter.get() == 7,
|
2023-06-11 17:11:16 +01:00
|
|
|
/// |_, _, _| log!("counter is 7 now!"),
|
|
|
|
/// );
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # view! { }
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Options
|
|
|
|
///
|
|
|
|
/// Options and defaults are same as [`watch_with_options`].
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use leptos::*;
|
2023-09-12 15:24:32 +01:00
|
|
|
/// # use leptos::logging::log;
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # use leptos_use::{WatchOptions, whenever_with_options};
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # pub fn Demo() -> impl IntoView {
|
|
|
|
/// # let (counter, set_counter) = create_signal(0);
|
2023-06-11 17:11:16 +01:00
|
|
|
/// whenever_with_options(
|
2023-06-21 13:09:00 +02:00
|
|
|
/// move || counter.get() == 7,
|
2023-06-11 17:11:16 +01:00
|
|
|
/// |_, _, _| log!("counter is 7 now!"),
|
|
|
|
/// WatchOptions::default().immediate(true),
|
|
|
|
/// );
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # view! { }
|
2023-06-11 17:11:16 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2023-07-14 22:43:19 +01:00
|
|
|
///
|
|
|
|
/// ## Server-Side Rendering
|
|
|
|
///
|
|
|
|
/// 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.
|
2023-07-27 18:06:36 +01:00
|
|
|
pub fn whenever<T, DFn, CFn>(source: DFn, callback: CFn) -> impl Fn() + Clone
|
2023-06-11 17:11:16 +01:00
|
|
|
where
|
|
|
|
DFn: Fn() -> bool + 'static,
|
|
|
|
CFn: Fn(bool, Option<bool>, Option<T>) -> T + Clone + 'static,
|
|
|
|
T: Clone + 'static,
|
|
|
|
{
|
2023-07-27 18:06:36 +01:00
|
|
|
whenever_with_options(source, callback, WatchOptions::default())
|
2023-06-11 17:11:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Version of `whenever` that accepts `WatchOptions`. See [`whenever`] for how to use.
|
|
|
|
pub fn whenever_with_options<T, DFn, CFn>(
|
|
|
|
source: DFn,
|
|
|
|
callback: CFn,
|
|
|
|
options: WatchOptions,
|
|
|
|
) -> impl Fn() + Clone
|
|
|
|
where
|
|
|
|
DFn: Fn() -> bool + 'static,
|
|
|
|
CFn: Fn(bool, Option<bool>, Option<T>) -> T + Clone + 'static,
|
|
|
|
T: Clone + 'static,
|
|
|
|
{
|
|
|
|
watch_with_options(
|
|
|
|
source,
|
|
|
|
move |value, prev_value, prev_return| {
|
|
|
|
if *value {
|
|
|
|
Some(callback(
|
|
|
|
*value,
|
|
|
|
prev_value.copied(),
|
|
|
|
prev_return.unwrap_or_default(),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
}
|