Moved enum_split::SignalStorage to it's own module.

This commit is contained in:
Filipe Rodrigues 2025-05-29 21:54:45 +01:00
parent 5728d63ab7
commit 0a53e6421b
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU
3 changed files with 45 additions and 34 deletions

View File

@ -3,11 +3,13 @@
// Modules
mod ctx;
mod either;
mod storage;
// Exports
pub use self::{
ctx::EnumSplitValueUpdateCtx,
either::{All1, All2, All3, Either1, Either2, Either3},
storage::SignalStorage,
};
// Imports
@ -121,36 +123,6 @@ impl<S, T: EnumSplitValue<S, W>, W: EnumSplitWorld<S, T>> SignalBorrow for EnumS
impl<S, T: EnumSplitValue<S, W>, W: EnumSplitWorld<S, T>> SignalWithDefaultImpl for EnumSplitSignal<S, T, W> {}
/// Signal storage.
#[derive(Debug)]
pub struct SignalStorage<T> {
/// Signal
// TODO: Allow the user to specify another type of signal?
signal: Signal<T>,
/// Write-back effect
// TODO: Not use dynamic dispatch here
write_back_effect: Effect<dyn EffectRun>,
}
impl<T> SignalStorage<T> {
/// Clones the signal in storage
#[must_use]
pub fn signal(&self) -> Signal<T> {
self.signal.clone()
}
/// Sets the value of the signal in storage.
///
/// Suppresses the write-back effect during.
pub fn set(&self, new_value: T)
where
T: 'static,
{
self.write_back_effect.suppressed(|| self.signal.set(new_value));
}
}
/// Effect fn inner
struct EffectFnInner<S, T: EnumSplitValue<S, W>, W: ReactiveWorld> {
/// Signals

View File

@ -51,9 +51,6 @@ impl<'a, S, W: ReactiveWorld> EnumSplitValueUpdateCtx<'a, S, W> {
signal.with(|_| ());
});
SignalStorage {
signal,
write_back_effect,
}
SignalStorage::new(signal, write_back_effect)
}
}

View File

@ -0,0 +1,42 @@
//! Storage
// Imports
use crate::{Effect, EffectRun, Signal, SignalSet};
/// Signal storage.
#[derive(Debug)]
pub struct SignalStorage<T> {
/// Signal
// TODO: Allow the user to specify another type of signal?
signal: Signal<T>,
/// Write-back effect
// TODO: Not use dynamic dispatch here
write_back_effect: Effect<dyn EffectRun>,
}
impl<T> SignalStorage<T> {
/// Creates a new signal storage
pub(crate) fn new(signal: Signal<T>, write_back_effect: Effect<dyn EffectRun>) -> Self {
Self {
signal,
write_back_effect,
}
}
/// Clones the signal in storage
#[must_use]
pub fn signal(&self) -> Signal<T> {
self.signal.clone()
}
/// Sets the value of the signal in storage.
///
/// Suppresses the write-back effect during.
pub fn set(&self, new_value: T)
where
T: 'static,
{
self.write_back_effect.suppressed(|| self.signal.set(new_value));
}
}