Signals
Signals are values with change tracking built in. In practice, this means you create a signal once, bind it to views and modifiers, and then update it as application state changes.
Creating a signal
let count = Signal::new(0);
Signals are Copy, so you can pass them into bindings and callbacks without cloning or borrowing state.
Signals are commonly stored on a model:
pub struct AppData {
count: Signal<i32>,
}
impl Model for AppData {}
Signals are lightweight handles and are Copy, so you can pass them into bindings and callbacks without cloning or borrowing state.
Where to read and write
- For read patterns (
get,with,map,SignalGet), see Reading Signals. - For write patterns (
set,update,set_if_changed,write), see Writing Signals.
Capturing signals in closures
When a closure captures a signal, prefer move:
Binding::new(cx, count, move |cx| {
if count.get(cx) > 10 {
Label::new(cx, "Large value");
}
});
Because signals are Copy, moving a signal into the closure just copies the handle.