pub fn watch<W, T>(
deps: impl Fn() -> W + 'static,
callback: impl Fn(&W, Option<&W>, Option<T>) -> T + Clone + 'static,
immediate: bool
) -> impl Fn() + Clonewhere
W: Clone + 'static,
T: 'static,
Expand description
A version of create_effect
that listens to any dependency
that is accessed inside deps
and returns a stop handler.
The return value of deps
is passed into callback
as an argument together with the previous value.
Additionally the last return value of callback
is provided as a third argument as is done in create_effect
.
§Usage
let (num, set_num) = create_signal(0);
let stop = watch(
move || num.get(),
move |num, prev_num, _| {
log::debug!("Number: {}; Prev: {:?}", num, prev_num);
},
false,
);
set_num.set(1); // > "Number: 1; Prev: Some(0)"
stop(); // stop watching
set_num.set(2); // (nothing happens)
The callback itself doesn’t track any signal that is accessed within it.
let (num, set_num) = create_signal(0);
let (cb_num, set_cb_num) = create_signal(0);
watch(
move || num.get(),
move |num, _, _| {
log::debug!("Number: {}; Cb: {}", num, cb_num.get());
},
false,
);
set_num.set(1); // > "Number: 1; Cb: 0"
set_cb_num.set(1); // (nothing happens)
set_num.set(2); // > "Number: 2; Cb: 1"
§Immediate
If the final parameter immediate
is true, the callback
will run immediately.
If it’s false
, the callback
will run only after
the first change is detected of any signal that is accessed in deps
.
let (num, set_num) = create_signal(0);
watch(
move || num.get(),
move |num, prev_num, _| {
log::debug!("Number: {}; Prev: {:?}", num, prev_num);
},
true,
); // > "Number: 0; Prev: None"
set_num.set(1); // > "Number: 1; Prev: Some(0)"