watch_pausable
Pausable watch.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_watch_pausable::{watch_pausable, WatchPausableReturn};
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal("foo".to_string());
let WatchPausableReturn {
stop,
pause,
resume,
..
} = watch_pausable(
move || source.get(),
|v, _, _| {
log!("Changed to {}", v);
},
);
set_source.set("bar".to_string()); // > "Changed to bar"
pause();
set_source.set("foobar".to_string()); // (nothing happens)
resume();
set_source.set("hello".to_string()); // > "Changed to hello"
view! { }
}
There’s also watch_pausable_with_options which takes the same options as watch.
Server-Side Rendering
Make sure you follow the instructions in 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.
See also
leptos::watch
Installation
cargo add leptos-use-watch-pausable
Options
WatchOptions
Options for watch_with_options
The filter is a type parameter so that the debounce and throttle code is only compiled into your app if you use it.
Every option is set with a builder method of the same name, e.g. WatchOptions::default().immediate(...). See also the API docs.
immediate
Type: bool
If immediate is true, the callback will run immediately.
If it’s false, the callbackwill run only after the first change is detected of any signal that is accessed indeps. Defaults to false`.
filter
Type: F
Allows to debounce or throttle the callback with a FilterOptions. Defaults to no filter.