From cef0e681217b1a8d58f96fe43e2ea6ee94d8f4a5 Mon Sep 17 00:00:00 2001 From: Maccesch Date: Mon, 12 Aug 2024 12:25:07 +0100 Subject: [PATCH] added assign_ltr and assign_rtl to sync_signal --- CHANGELOG.md | 3 ++- Cargo.toml | 2 +- src/sync_signal.rs | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c27f5fd..0b7a398 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 24e568a..870af1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/sync_signal.rs b/src/sync_signal.rs index e67ef3d..e20b1e3 100644 --- a/src/sync_signal.rs +++ b/src/sync_signal.rs @@ -168,6 +168,8 @@ where direction, transform_ltr, transform_rtl, + assign_ltr, + assign_rtl, } = options; let left = left.into(); @@ -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, @@ -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, @@ -221,6 +223,8 @@ pub enum SyncDirection { Both, } +pub type AssignFn = Rc; + /// Options for [`sync_signal_with_options`]. #[derive(DefaultBuilder)] pub struct SyncSignalOptions { @@ -241,6 +245,16 @@ pub struct SyncSignalOptions { /// Defaults to identity. #[builder(skip)] transform_rtl: Rc L>, + + /// Assigns the left signal to the right signal. + /// Defaults to `*r = l`. + #[builder(skip)] + assign_ltr: AssignFn, + + /// Assigns the right signal to the left signal. + /// Defaults to `*l = r`. + #[builder(skip)] + assign_rtl: AssignFn, } impl SyncSignalOptions { @@ -262,6 +276,23 @@ impl SyncSignalOptions { } } + /// 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, @@ -272,6 +303,8 @@ impl SyncSignalOptions { 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), } } } @@ -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), } } }