From 9903dcf03d2f25eb83a65b990a7ca5ec0fabb543 Mon Sep 17 00:00:00 2001 From: Jared Moulton Date: Thu, 21 Nov 2024 11:04:33 -0700 Subject: [PATCH] rename get update to DerivedRw (#697) --- examples/timer/src/main.rs | 4 +- examples/tokio-timer/src/main.rs | 4 +- examples/widget-gallery/src/slider.rs | 4 +- reactive/src/{get_update_fn.rs => derived.rs} | 41 ++++++++++++++----- reactive/src/lib.rs | 4 +- 5 files changed, 39 insertions(+), 18 deletions(-) rename reactive/src/{get_update_fn.rs => derived.rs} (78%) diff --git a/examples/timer/src/main.rs b/examples/timer/src/main.rs index 31413be3..59a05003 100644 --- a/examples/timer/src/main.rs +++ b/examples/timer/src/main.rs @@ -3,7 +3,7 @@ use std::time::{Duration, Instant}; use floem::{ action::exec_after, reactive::{ - create_effect, create_get_update, create_rw_signal, SignalGet, SignalTrack, SignalUpdate, + create_effect, create_rw_signal, DerivedRwSignal, SignalGet, SignalTrack, SignalUpdate, }, unit::{Pct, UnitExt}, views::{button, container, label, slider, stack, text, v_stack, Decorators}, @@ -47,7 +47,7 @@ fn app_view() -> impl IntoView { }); }); - let progress = create_get_update( + let progress = DerivedRwSignal::new( target_duration, move |val| Pct(elapsed_time.get().as_secs_f64() / val.0 * 100.), |val| *val, diff --git a/examples/tokio-timer/src/main.rs b/examples/tokio-timer/src/main.rs index d49b4668..46496cc2 100644 --- a/examples/tokio-timer/src/main.rs +++ b/examples/tokio-timer/src/main.rs @@ -2,7 +2,7 @@ use std::time::Duration; use floem::ext_event::create_signal_from_stream; use floem::prelude::*; -use floem::reactive::create_get_update; +use floem::reactive::DerivedRwSignal; use floem::unit::Pct; use tokio::runtime::Runtime; use tokio::time::Instant; @@ -41,7 +41,7 @@ fn app_view() -> impl IntoView { ) }); - let progress = create_get_update( + let progress = DerivedRwSignal::new( target_duration, move |val| Pct(elapsed_time().as_secs_f64() / val.0 * 100.), |val| *val, diff --git a/examples/widget-gallery/src/slider.rs b/examples/widget-gallery/src/slider.rs index 560e19c0..6be4c741 100644 --- a/examples/widget-gallery/src/slider.rs +++ b/examples/widget-gallery/src/slider.rs @@ -1,5 +1,5 @@ use floem::{ - reactive::{create_get_update, create_rw_signal, SignalGet}, + reactive::{create_rw_signal, DerivedRwSignal, SignalGet}, unit::UnitExt, views::{label, slider, stack, text_input, Decorators}, IntoView, @@ -9,7 +9,7 @@ use crate::form::{self, form_item}; pub fn slider_view() -> impl IntoView { let input = create_rw_signal(String::from("50")); - let slider_state = create_get_update( + let slider_state = DerivedRwSignal::new( input, |val| val.parse::().unwrap_or_default().pct(), |val| val.0.to_string(), diff --git a/reactive/src/get_update_fn.rs b/reactive/src/derived.rs similarity index 78% rename from reactive/src/get_update_fn.rs rename to reactive/src/derived.rs index 80578532..65875a37 100644 --- a/reactive/src/get_update_fn.rs +++ b/reactive/src/derived.rs @@ -2,23 +2,28 @@ use std::marker::PhantomData; use crate::{read::SignalTrack, RwSignal, SignalGet, SignalUpdate, SignalWith}; -pub struct GetUpdateFn O + Clone + 'static, UF: Fn(&O) -> T + 'static> { +/// A signal that is derived from an [RwSignal](super::RwSignal) but lets you specify getters and setters for the signal. +/// +/// This is useful when you want a single state variable and don't want to use effects to synchronize multiple signals. +/// +/// This is also useful when you want a derived signal that implements the [SignalGet], [SignalWith], etc. traits. +pub struct DerivedRwSignal O + Clone + 'static, UF: Fn(&O) -> T + 'static> { signal: RwSignal, getter: RwSignal>, setter: RwSignal>, ty: PhantomData, } -impl O + Copy, UF: Fn(&O) -> T + Copy> Clone for GetUpdateFn { +impl O + Copy, UF: Fn(&O) -> T + Copy> Clone for DerivedRwSignal { fn clone(&self) -> Self { *self } } -impl O + Copy, UF: Fn(&O) -> T + Copy> Copy for GetUpdateFn {} +impl O + Copy, UF: Fn(&O) -> T + Copy> Copy for DerivedRwSignal {} impl O + Copy, UF: Fn(&O) -> T + Copy> SignalGet - for GetUpdateFn + for DerivedRwSignal { fn id(&self) -> crate::id::Id { self.signal.id @@ -48,7 +53,7 @@ impl O + Copy, UF: Fn(&O) -> T + Cop } impl O + Copy, UF: Fn(&O) -> T + Copy> SignalWith - for GetUpdateFn + for DerivedRwSignal { fn id(&self) -> crate::id::Id { self.signal.id @@ -99,7 +104,7 @@ impl O + Copy, UF: Fn(&O) -> T + Cop } impl O + Copy, UF: Fn(&O) -> T + Copy> SignalTrack - for GetUpdateFn + for DerivedRwSignal { fn id(&self) -> crate::id::Id { self.signal.id @@ -116,7 +121,7 @@ impl O + Copy, UF: Fn(&O) -> T + Cop } impl O + Copy, UF: Fn(&O) -> T + Copy> SignalUpdate - for GetUpdateFn + for DerivedRwSignal { fn id(&self) -> crate::id::Id { self.signal.id @@ -168,19 +173,35 @@ impl O + Copy, UF: Fn(&O) -> T + Copy> SignalUpdate }) } } +impl DerivedRwSignal +where + GF: Fn(&T) -> O + Clone + 'static, + UF: Fn(&O) -> T + 'static, +{ + pub fn new(signal: RwSignal, getter: GF, setter: UF) -> Self { + let getter = RwSignal::new(Box::new(getter)); + let setter = RwSignal::new(Box::new(setter)); + DerivedRwSignal { + signal, + getter, + setter, + ty: PhantomData, + } + } +} -pub fn create_get_update( +pub fn create_derived_rw_signal( signal: RwSignal, getter: GF, setter: UF, -) -> GetUpdateFn +) -> DerivedRwSignal where GF: Fn(&T) -> O + Clone + 'static, UF: Fn(&O) -> T + 'static, { let getter = RwSignal::new(Box::new(getter)); let setter = RwSignal::new(Box::new(setter)); - GetUpdateFn { + DerivedRwSignal { signal, getter, setter, diff --git a/reactive/src/lib.rs b/reactive/src/lib.rs index 53e9312e..6854424e 100644 --- a/reactive/src/lib.rs +++ b/reactive/src/lib.rs @@ -6,8 +6,8 @@ mod base; mod context; +mod derived; mod effect; -mod get_update_fn; mod id; mod impls; mod memo; @@ -20,8 +20,8 @@ mod write; pub use base::{create_base_signal, BaseSignal}; pub use context::{provide_context, use_context}; +pub use derived::{create_derived_rw_signal, DerivedRwSignal}; pub use effect::{batch, create_effect, create_stateful_updater, create_updater, untrack}; -pub use get_update_fn::{create_get_update, GetUpdateFn}; pub use memo::{create_memo, Memo}; pub use read::{ReadSignalValue, SignalGet, SignalRead, SignalTrack, SignalWith}; pub use scope::{as_child_of_current_scope, with_scope, Scope};