chore: clean up emitter-and-signal library design
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use snafu::Snafu;
|
||||
|
||||
pub mod emitter;
|
||||
pub mod emitter_ext;
|
||||
mod emitter_ext;
|
||||
pub mod signal;
|
||||
pub mod signal_ext;
|
||||
mod signal_ext;
|
||||
|
||||
pub use emitter::Emitter;
|
||||
pub use emitter_ext::EmitterExt;
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
use std::future::Future;
|
||||
|
||||
use ext_trait::extension;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tokio::select;
|
||||
|
||||
use crate::ProducerExited;
|
||||
|
||||
use super::signal::{JoinError, Signal};
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub struct ProducerAlreadyExited {
|
||||
source: ProducerExited,
|
||||
}
|
||||
|
||||
#[extension(pub trait SignalExt)]
|
||||
impl<T> Signal<T> {
|
||||
fn map<M, F>(
|
||||
self,
|
||||
mut func: F,
|
||||
) -> Result<(Signal<M>, impl Future<Output = Result<(), JoinError>>), ProducerAlreadyExited>
|
||||
) -> Result<(Signal<M>, impl Future<Output = Result<(), JoinError>>), ProducerExited>
|
||||
where
|
||||
T: 'static + Sync + Send + Clone,
|
||||
M: 'static + Sync + Send + Clone,
|
||||
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 {
|
||||
while let Some(publisher) = publisher_stream.wait().await {
|
||||
|
||||
@@ -19,7 +19,7 @@ mod mode;
|
||||
pub use attributes::InputNumberAttributes;
|
||||
pub use mode::InputNumberMode;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[derive(Debug, Clone, Snafu)]
|
||||
pub enum CreateSignalError {
|
||||
/// couldn't get the underlying state object signal
|
||||
StateObjectSignalError {
|
||||
@@ -28,7 +28,7 @@ pub enum CreateSignalError {
|
||||
|
||||
/// couldn't map the state object to a power value
|
||||
MappedSignalError {
|
||||
source: emitter_and_signal::signal_ext::ProducerAlreadyExited,
|
||||
source: emitter_and_signal::ProducerExited,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ pub enum CreateSignalError {
|
||||
|
||||
/// couldn't map the state object to a power value
|
||||
MappedSignalError {
|
||||
source: emitter_and_signal::signal_ext::ProducerAlreadyExited,
|
||||
source: emitter_and_signal::ProducerExited,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ pub struct StateObject<State, Attributes, ContextEvent> {
|
||||
pub type ExtractStateObjectError<'a, 'py, State, Attributes, ContextEvent> =
|
||||
<StateObject<State, Attributes, ContextEvent> as FromPyObject<'a, 'py>>::Error;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[derive(Debug, Clone, Snafu)]
|
||||
pub enum CreateSignalError {
|
||||
/// couldn't get the state machine from the Home Assistant object
|
||||
GetStatesError { source: GetStatesError },
|
||||
|
||||
Reference in New Issue
Block a user