chore: clean up emitter-and-signal library design

This commit is contained in:
J / Jacob Babich
2026-07-15 14:33:51 -04:00
parent a09a97d06d
commit 4786b1e6ba
5 changed files with 8 additions and 14 deletions

View File

@@ -1,9 +1,9 @@
use snafu::Snafu; use snafu::Snafu;
pub mod emitter; pub mod emitter;
pub mod emitter_ext; mod emitter_ext;
pub mod signal; pub mod signal;
pub mod signal_ext; mod signal_ext;
pub use emitter::Emitter; pub use emitter::Emitter;
pub use emitter_ext::EmitterExt; pub use emitter_ext::EmitterExt;

View File

@@ -1,30 +1,24 @@
use std::future::Future; use std::future::Future;
use ext_trait::extension; use ext_trait::extension;
use snafu::{ResultExt, Snafu};
use tokio::select; use tokio::select;
use crate::ProducerExited; use crate::ProducerExited;
use super::signal::{JoinError, Signal}; use super::signal::{JoinError, Signal};
#[derive(Debug, Snafu)]
pub struct ProducerAlreadyExited {
source: ProducerExited,
}
#[extension(pub trait SignalExt)] #[extension(pub trait SignalExt)]
impl<T> Signal<T> { impl<T> Signal<T> {
fn map<M, F>( fn map<M, F>(
self, self,
mut func: F, mut func: F,
) -> Result<(Signal<M>, impl Future<Output = Result<(), JoinError>>), ProducerAlreadyExited> ) -> Result<(Signal<M>, impl Future<Output = Result<(), JoinError>>), ProducerExited>
where where
T: 'static + Sync + Send + Clone, T: 'static + Sync + Send + Clone,
M: 'static + Sync + Send + Clone, M: 'static + Sync + Send + Clone,
F: 'static + Send + FnMut(T) -> M, F: 'static + Send + FnMut(T) -> M,
{ {
let initial = func(self.subscribe().context(ProducerAlreadyExitedSnafu)?.get()); let initial = func(self.subscribe()?.get());
Ok(Signal::new(initial, |mut publisher_stream| async move { Ok(Signal::new(initial, |mut publisher_stream| async move {
while let Some(publisher) = publisher_stream.wait().await { while let Some(publisher) = publisher_stream.wait().await {

View File

@@ -19,7 +19,7 @@ mod mode;
pub use attributes::InputNumberAttributes; pub use attributes::InputNumberAttributes;
pub use mode::InputNumberMode; pub use mode::InputNumberMode;
#[derive(Debug, Snafu)] #[derive(Debug, Clone, Snafu)]
pub enum CreateSignalError { pub enum CreateSignalError {
/// couldn't get the underlying state object signal /// couldn't get the underlying state object signal
StateObjectSignalError { StateObjectSignalError {
@@ -28,7 +28,7 @@ pub enum CreateSignalError {
/// couldn't map the state object to a power value /// couldn't map the state object to a power value
MappedSignalError { MappedSignalError {
source: emitter_and_signal::signal_ext::ProducerAlreadyExited, source: emitter_and_signal::ProducerExited,
}, },
} }

View File

@@ -47,7 +47,7 @@ pub enum CreateSignalError {
/// couldn't map the state object to a power value /// couldn't map the state object to a power value
MappedSignalError { MappedSignalError {
source: emitter_and_signal::signal_ext::ProducerAlreadyExited, source: emitter_and_signal::ProducerExited,
}, },
} }

View File

@@ -28,7 +28,7 @@ pub struct StateObject<State, Attributes, ContextEvent> {
pub type ExtractStateObjectError<'a, 'py, State, Attributes, ContextEvent> = pub type ExtractStateObjectError<'a, 'py, State, Attributes, ContextEvent> =
<StateObject<State, Attributes, ContextEvent> as FromPyObject<'a, 'py>>::Error; <StateObject<State, Attributes, ContextEvent> as FromPyObject<'a, 'py>>::Error;
#[derive(Debug, Snafu)] #[derive(Debug, Clone, Snafu)]
pub enum CreateSignalError { pub enum CreateSignalError {
/// couldn't get the state machine from the Home Assistant object /// couldn't get the state machine from the Home Assistant object
GetStatesError { source: GetStatesError }, GetStatesError { source: GetStatesError },