added assign_ltr and assign_rtl to sync_signal

This commit is contained in:
Maccesch 2024-08-12 12:25:07 +01:00
parent d32d725731
commit cef0e68121
3 changed files with 40 additions and 4 deletions

View file

@ -3,11 +3,12 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] - ## [0.11.4] - 2024-08-12
### New Features 🚀 ### New Features 🚀
- `use_web_notification` now supports the options `renotify`, `silent` and `image` (thanks to @hcandelaria). - `use_web_notification` now supports the options `renotify`, `silent` and `image` (thanks to @hcandelaria).
- `sync_signal` no supports the options `assign_ltr` and `assign_rtl`.
## [0.11.3] - 2024-07-31 ## [0.11.3] - 2024-07-31

View file

@ -1,6 +1,6 @@
[package] [package]
name = "leptos-use" name = "leptos-use"
version = "0.11.3" version = "0.11.4"
edition = "2021" edition = "2021"
authors = ["Marc-Stefan Cassola"] authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"] categories = ["gui", "web-programming"]

View file

@ -168,6 +168,8 @@ where
direction, direction,
transform_ltr, transform_ltr,
transform_rtl, transform_rtl,
assign_ltr,
assign_rtl,
} = options; } = options;
let left = left.into(); let left = left.into();
@ -183,7 +185,7 @@ where
let new_value = (*transform_ltr)(new_value); let new_value = (*transform_ltr)(new_value);
if right.with_untracked(|right| right != &new_value) { if right.with_untracked(|right| right != &new_value) {
right.update(|right| *right = new_value); right.update(|right| assign_ltr(right, new_value));
} }
}, },
immediate, immediate,
@ -197,7 +199,7 @@ where
let new_value = (*transform_rtl)(new_value); let new_value = (*transform_rtl)(new_value);
if left.with_untracked(|left| left != &new_value) { if left.with_untracked(|left| left != &new_value) {
left.update(|left| *left = new_value); left.update(|left| assign_rtl(left, new_value));
} }
}, },
immediate, immediate,
@ -221,6 +223,8 @@ pub enum SyncDirection {
Both, Both,
} }
pub type AssignFn<T> = Rc<dyn Fn(&mut T, T)>;
/// Options for [`sync_signal_with_options`]. /// Options for [`sync_signal_with_options`].
#[derive(DefaultBuilder)] #[derive(DefaultBuilder)]
pub struct SyncSignalOptions<L, R> { pub struct SyncSignalOptions<L, R> {
@ -241,6 +245,16 @@ pub struct SyncSignalOptions<L, R> {
/// Defaults to identity. /// Defaults to identity.
#[builder(skip)] #[builder(skip)]
transform_rtl: Rc<dyn Fn(&R) -> L>, transform_rtl: Rc<dyn Fn(&R) -> L>,
/// Assigns the left signal to the right signal.
/// Defaults to `*r = l`.
#[builder(skip)]
assign_ltr: AssignFn<R>,
/// Assigns the right signal to the left signal.
/// Defaults to `*l = r`.
#[builder(skip)]
assign_rtl: AssignFn<L>,
} }
impl<L, R> SyncSignalOptions<L, R> { impl<L, R> SyncSignalOptions<L, R> {
@ -262,6 +276,23 @@ impl<L, R> SyncSignalOptions<L, R> {
} }
} }
/// Assigns the left signal to the right signal.
/// Defaults to `*r = l`.
pub fn assign_ltr(self, assign_ltr: impl Fn(&mut R, R) + 'static) -> Self {
Self {
assign_ltr: Rc::new(assign_ltr),
..self
}
}
/// Assigns the right signal to the left signal.
/// Defaults to `*l = r`.
pub fn assign_rtl(self, assign_rtl: impl Fn(&mut L, L) + 'static) -> Self {
Self {
assign_rtl: Rc::new(assign_rtl),
..self
}
}
/// Initializes options with transforms /// Initializes options with transforms
pub fn with_transforms( pub fn with_transforms(
transform_ltr: impl Fn(&L) -> R + 'static, transform_ltr: impl Fn(&L) -> R + 'static,
@ -272,6 +303,8 @@ impl<L, R> SyncSignalOptions<L, R> {
direction: SyncDirection::Both, direction: SyncDirection::Both,
transform_ltr: Rc::new(transform_ltr), transform_ltr: Rc::new(transform_ltr),
transform_rtl: Rc::new(transform_rtl), transform_rtl: Rc::new(transform_rtl),
assign_ltr: Rc::new(|right, left| *right = left),
assign_rtl: Rc::new(|left, right| *left = right),
} }
} }
} }
@ -287,6 +320,8 @@ where
direction: SyncDirection::Both, direction: SyncDirection::Both,
transform_ltr: Rc::new(|x| x.clone().into()), transform_ltr: Rc::new(|x| x.clone().into()),
transform_rtl: Rc::new(|x| x.clone().into()), transform_rtl: Rc::new(|x| x.clone().into()),
assign_ltr: Rc::new(|right, left| *right = left),
assign_rtl: Rc::new(|left, right| *left = right),
} }
} }
} }