mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-02-08 21:33:09 -05:00
moved to leptos::watch
This commit is contained in:
parent
8f96b10f13
commit
6fab76c13f
9 changed files with 68 additions and 82 deletions
|
@ -56,10 +56,10 @@
|
||||||
|
|
||||||
# Watch
|
# Watch
|
||||||
|
|
||||||
- [watch](watch/watch.md)
|
|
||||||
- [watch_debounced](watch/watch_debounced.md)
|
- [watch_debounced](watch/watch_debounced.md)
|
||||||
- [watch_pausable](watch/watch_pausable.md)
|
- [watch_pausable](watch/watch_pausable.md)
|
||||||
- [watch_throttled](watch/watch_throttled.md)
|
- [watch_throttled](watch/watch_throttled.md)
|
||||||
|
- [watch_with_options](watch/watch_with_options.md)
|
||||||
- [whenever](watch/whenever.md)
|
- [whenever](watch/whenever.md)
|
||||||
|
|
||||||
# Utilities
|
# Utilities
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
# watch
|
# watch_with_options
|
||||||
|
|
||||||
<!-- cmdrun python3 ../extract_doc_comment.py watch -->
|
<!-- cmdrun python3 ../extract_doc_comment.py watch -->
|
|
@ -56,10 +56,10 @@ mod use_to_string;
|
||||||
mod use_websocket;
|
mod use_websocket;
|
||||||
mod use_window_focus;
|
mod use_window_focus;
|
||||||
mod use_window_scroll;
|
mod use_window_scroll;
|
||||||
mod watch;
|
|
||||||
mod watch_debounced;
|
mod watch_debounced;
|
||||||
mod watch_pausable;
|
mod watch_pausable;
|
||||||
mod watch_throttled;
|
mod watch_throttled;
|
||||||
|
mod watch_with_options;
|
||||||
mod whenever;
|
mod whenever;
|
||||||
|
|
||||||
pub use is_err::*;
|
pub use is_err::*;
|
||||||
|
@ -98,8 +98,8 @@ pub use use_to_string::*;
|
||||||
pub use use_websocket::*;
|
pub use use_websocket::*;
|
||||||
pub use use_window_focus::*;
|
pub use use_window_focus::*;
|
||||||
pub use use_window_scroll::*;
|
pub use use_window_scroll::*;
|
||||||
pub use watch::*;
|
|
||||||
pub use watch_debounced::*;
|
pub use watch_debounced::*;
|
||||||
pub use watch_pausable::*;
|
pub use watch_pausable::*;
|
||||||
pub use watch_throttled::*;
|
pub use watch_throttled::*;
|
||||||
|
pub use watch_with_options::*;
|
||||||
pub use whenever::*;
|
pub use whenever::*;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::core::ElementMaybeSignal;
|
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 cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
|
@ -170,6 +170,7 @@ where
|
||||||
let _ = style.set_property(&prop.get_untracked(), val);
|
let _ = style.set_property(&prop.get_untracked(), val);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::core::MaybeRwSignal;
|
use crate::core::MaybeRwSignal;
|
||||||
use crate::watch;
|
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
|
|
||||||
|
@ -158,7 +157,12 @@ where
|
||||||
let _ = {
|
let _ = {
|
||||||
let set = set.clone();
|
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 {
|
UseCycleListReturn {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::core::MaybeRwSignal;
|
use crate::core::MaybeRwSignal;
|
||||||
use crate::watch;
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
|
@ -104,6 +103,7 @@ pub fn use_favicon_with_options(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::utils::Pausable;
|
use crate::utils::Pausable;
|
||||||
use crate::watch;
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::leptos_dom::helpers::IntervalHandle;
|
use leptos::leptos_dom::helpers::IntervalHandle;
|
||||||
|
@ -127,6 +126,7 @@ where
|
||||||
resume();
|
resume();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
on_cleanup(cx, stop_watch);
|
on_cleanup(cx, stop_watch);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::core::ElementsMaybeSignal;
|
use crate::core::ElementsMaybeSignal;
|
||||||
use crate::{use_supported, watch};
|
use crate::use_supported;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
@ -109,7 +109,7 @@ where
|
||||||
let stop_watch = {
|
let stop_watch = {
|
||||||
let cleanup = cleanup.clone();
|
let cleanup = cleanup.clone();
|
||||||
|
|
||||||
watch(
|
leptos::watch(
|
||||||
cx,
|
cx,
|
||||||
move || targets.get(),
|
move || targets.get(),
|
||||||
move |targets, _, _| {
|
move |targets, _, _| {
|
||||||
|
@ -127,6 +127,7 @@ where
|
||||||
observer.replace(Some(obs));
|
observer.replace(Some(obs));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,43 +5,13 @@ use leptos::*;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
/// A version of `create_effect` that listens to any dependency that is accessed inside `deps`.
|
/// A version of `leptos::watch` but with additional options.
|
||||||
/// 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, }
|
|
||||||
/// # }
|
|
||||||
/// ```
|
|
||||||
///
|
///
|
||||||
/// ## Immediate
|
/// ## 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
|
/// If it's `false`, the `callback` will run only after
|
||||||
/// the first change is detected of any signal that is accessed in `deps`.
|
/// 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
|
/// 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
|
/// 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
|
/// ## See also
|
||||||
///
|
///
|
||||||
/// * [`watch_throttled`]
|
/// * [`watch_throttled`]
|
||||||
/// * [`watch_debounced`]
|
/// * [`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.
|
/// Version of `watch` that accepts `WatchOptions`. See [`watch`] for how to use.
|
||||||
pub fn watch_with_options<W, T, DFn, CFn>(
|
pub fn watch_with_options<W, T, DFn, CFn>(
|
||||||
|
@ -141,8 +102,6 @@ where
|
||||||
W: Clone + 'static,
|
W: Clone + 'static,
|
||||||
T: 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 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_deps_value: Rc<RefCell<Option<W>>> = Rc::new(RefCell::new(None));
|
||||||
let prev_callback_value: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(None));
|
let prev_callback_value: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(None));
|
||||||
|
@ -167,19 +126,12 @@ where
|
||||||
let filtered_callback =
|
let filtered_callback =
|
||||||
create_filter_wrapper(options.filter.filter_fn(), wrapped_callback.clone());
|
create_filter_wrapper(options.filter.filter_fn(), wrapped_callback.clone());
|
||||||
|
|
||||||
create_effect(cx, move |did_run_before| {
|
leptos::watch(
|
||||||
if !is_active.get() {
|
cx,
|
||||||
return;
|
deps,
|
||||||
}
|
move |deps_value, previous_deps_value, did_run_before| {
|
||||||
|
|
||||||
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()));
|
cur_deps_value.replace(Some(deps_value.clone()));
|
||||||
|
prev_deps_value.replace(previous_deps_value.cloned());
|
||||||
|
|
||||||
let callback_value = if options.immediate && did_run_before.is_none() {
|
let callback_value = if options.immediate && did_run_before.is_none() {
|
||||||
Some(wrapped_callback())
|
Some(wrapped_callback())
|
||||||
|
@ -188,13 +140,29 @@ where
|
||||||
};
|
};
|
||||||
|
|
||||||
prev_callback_value.replace(callback_value);
|
prev_callback_value.replace(callback_value);
|
||||||
|
},
|
||||||
|
options.immediate,
|
||||||
|
)
|
||||||
|
|
||||||
prev_deps_value.replace(Some(deps_value));
|
// create_effect(cx, move |did_run_before| {
|
||||||
});
|
// if !is_active.get() {
|
||||||
|
// return;
|
||||||
move || {
|
// }
|
||||||
set_active.set(false);
|
//
|
||||||
}
|
// 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`
|
/// Options for `watch_with_options`
|
||||||
|
@ -216,3 +184,15 @@ impl WatchOptions {
|
||||||
filter
|
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)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue