chore+feat(emitter-and-signal): slightly update design and errors

This commit is contained in:
2026-01-07 01:47:05 -05:00
parent 3dbf2eb475
commit 97aef026b2
6 changed files with 108 additions and 33 deletions

View File

@@ -1,11 +1,17 @@
use ext_trait::extension;
use tokio::{select, task::JoinHandle};
use std::future::Future;
use super::emitter::{Capacity, Emitter, NextError};
use ext_trait::extension;
use tokio::select;
use super::emitter::{Capacity, Emitter, JoinError, NextError};
#[extension(pub trait EmitterExt)]
impl<T> Emitter<T> {
fn map<M, F>(self, mut func: F, capacity: Capacity) -> (Emitter<M>, JoinHandle<()>)
fn map<M, F>(
self,
mut func: F,
capacity: Capacity,
) -> (Emitter<M>, impl Future<Output = Result<(), JoinError>>)
where
T: Send + 'static + Clone,
M: Send + 'static + Clone,
@@ -28,7 +34,7 @@ impl<T> Emitter<T> {
match event_res {
Ok(event) => publisher.publish(func(event)),
Err(NextError::Lagged { .. }) => {},
Err(NextError::ProducerExited(_)) => return,
Err(NextError::ProducerExited { .. }) => return,
}
}
}
@@ -39,7 +45,11 @@ impl<T> Emitter<T> {
)
}
fn filter<F>(self, mut func: F, capacity: Capacity) -> (Emitter<T>, JoinHandle<()>)
fn filter<F>(
self,
mut func: F,
capacity: Capacity,
) -> (Emitter<T>, impl Future<Output = Result<(), JoinError>>)
where
T: Send + 'static + Clone,
F: Send + 'static + FnMut(&T) -> bool,
@@ -63,7 +73,7 @@ impl<T> Emitter<T> {
publisher.publish(event)
},
Err(NextError::Lagged { .. }) => {},
Err(NextError::ProducerExited(_)) => return,
Err(NextError::ProducerExited { .. }) => return,
}
}
}
@@ -74,7 +84,11 @@ impl<T> Emitter<T> {
)
}
fn filter_mut<F>(self, mut func: F, capacity: Capacity) -> (Emitter<T>, JoinHandle<()>)
fn filter_mut<F>(
self,
mut func: F,
capacity: Capacity,
) -> (Emitter<T>, impl Future<Output = Result<(), JoinError>>)
where
T: Send + 'static + Clone,
F: Send + 'static + FnMut(&mut T) -> bool,
@@ -98,7 +112,7 @@ impl<T> Emitter<T> {
publisher.publish(event)
},
Err(NextError::Lagged { .. }) => {},
Err(NextError::ProducerExited(_)) => return,
Err(NextError::ProducerExited { .. }) => return,
}
}
}
@@ -109,7 +123,11 @@ impl<T> Emitter<T> {
)
}
fn filter_map<M, F>(self, mut func: F, capacity: Capacity) -> (Emitter<M>, JoinHandle<()>)
fn filter_map<M, F>(
self,
mut func: F,
capacity: Capacity,
) -> (Emitter<M>, impl Future<Output = Result<(), JoinError>>)
where
T: Send + 'static + Clone,
M: Send + 'static + Clone,
@@ -134,7 +152,7 @@ impl<T> Emitter<T> {
publisher.publish(mapped)
},
Err(NextError::Lagged { .. }) => {},
Err(NextError::ProducerExited(_)) => return,
Err(NextError::ProducerExited { .. }) => return,
}
}
}