Function leptos_use::sync_signal
source · pub fn sync_signal<T>(
left: impl Into<UseRwSignal<T>>,
right: impl Into<UseRwSignal<T>>
) -> impl Fn() + Clone
Expand description
Two-way Signals synchronization.
Note: Please consider first if you can achieve your goals with the “Good Options” described in the Leptos book firstly. Only if you really have to, use this function. This is in effect the “If you really must…”.
§Demo
§Usage
let (a, set_a) = create_signal(1);
let (b, set_b) = create_signal(2);
let stop = sync_signal((a, set_a), (b, set_b));
logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 1, b: 1
set_b.set(3);
logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 3, b: 3
set_a.set(4);
logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 4, b: 4
§RwSignal
You can mix and match RwSignal
s and Signal
-WriteSignal
pairs.
let (a, set_a) = create_signal(1);
let (b, set_b) = create_signal(2);
let c_rw = create_rw_signal(3);
let d_rw = create_rw_signal(4);
sync_signal((a, set_a), c_rw);
sync_signal(d_rw, (b, set_b));
sync_signal(c_rw, d_rw);
§One directional
You can synchronize a signal only from left to right or right to left.
let (a, set_a) = create_signal(1);
let (b, set_b) = create_signal(2);
let stop = sync_signal_with_options(
(a, set_a),
(b, set_b),
SyncSignalOptions::default().direction(SyncDirection::LeftToRight)
);
set_b.set(3); // doesn't sync
logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 1, b: 3
set_a.set(4);
logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 4, b: 4
§Custom Transform
You can optionally provide custom transforms between the two signals.
let (a, set_a) = create_signal(10);
let (b, set_b) = create_signal(2);
let stop = sync_signal_with_options(
(a, set_a),
(b, set_b),
SyncSignalOptions::default()
.transform_ltr(|left| *left * 2)
.transform_rtl(|right| *right / 2)
);
logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 10, b: 20
set_b.set(30);
logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 15, b: 30
§Different Types
SyncSignalOptions::default()
is only defined if the two signal types are identical or
implement From
for each other. Otherwise, you have to initialize the options with
with_transforms
instead of default
.
let options = SyncSignalOptions::with_transforms(
|left: &String| i32::from_str(left).unwrap_or_default(),
|right: &i32| right.to_string(),
);