Skip to content

Commit

Permalink
added assign_ltr and assign_rtl to sync_signal
Browse files Browse the repository at this point in the history
  • Loading branch information
maccesch committed Aug 12, 2024
1 parent d32d725 commit cef0e68
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
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).

## [Unreleased] -
## [0.11.4] - 2024-08-12

### New Features 🚀

- `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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leptos-use"
version = "0.11.3"
version = "0.11.4"
edition = "2021"
authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"]
Expand Down
39 changes: 37 additions & 2 deletions src/sync_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ where
direction,
transform_ltr,
transform_rtl,
assign_ltr,
assign_rtl,
} = options;

let left = left.into();
Expand All @@ -183,7 +185,7 @@ where
let new_value = (*transform_ltr)(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,
Expand All @@ -197,7 +199,7 @@ where
let new_value = (*transform_rtl)(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,
Expand All @@ -221,6 +223,8 @@ pub enum SyncDirection {
Both,
}

pub type AssignFn<T> = Rc<dyn Fn(&mut T, T)>;

/// Options for [`sync_signal_with_options`].
#[derive(DefaultBuilder)]
pub struct SyncSignalOptions<L, R> {
Expand All @@ -241,6 +245,16 @@ pub struct SyncSignalOptions<L, R> {
/// Defaults to identity.
#[builder(skip)]
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> {
Expand All @@ -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
pub fn with_transforms(
transform_ltr: impl Fn(&L) -> R + 'static,
Expand All @@ -272,6 +303,8 @@ impl<L, R> SyncSignalOptions<L, R> {
direction: SyncDirection::Both,
transform_ltr: Rc::new(transform_ltr),
transform_rtl: Rc::new(transform_rtl),
assign_ltr: Rc::new(|right, left| *right = left),
assign_rtl: Rc::new(|left, right| *left = right),
}
}
}
Expand All @@ -287,6 +320,8 @@ where
direction: SyncDirection::Both,
transform_ltr: 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),
}
}
}

0 comments on commit cef0e68

Please sign in to comment.