2019-12-05 06:10:13 +01:00
|
|
|
//! Generate events asynchronously for you application.
|
|
|
|
|
|
|
|
|
|
/// An event subscription.
|
2019-12-08 08:21:26 +01:00
|
|
|
pub struct Subscription<I, O> {
|
|
|
|
|
connections: Vec<Box<dyn Connection<Input = I, Output = O>>>,
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-08 08:21:26 +01:00
|
|
|
impl<I, O> Subscription<I, O> {
|
2019-12-05 06:10:13 +01:00
|
|
|
pub fn none() -> Self {
|
|
|
|
|
Self {
|
2019-12-08 08:21:26 +01:00
|
|
|
connections: Vec::new(),
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 08:21:26 +01:00
|
|
|
pub fn batch(
|
|
|
|
|
subscriptions: impl Iterator<Item = Subscription<I, O>>,
|
|
|
|
|
) -> Self {
|
2019-12-05 06:10:13 +01:00
|
|
|
Self {
|
2019-12-08 08:21:26 +01:00
|
|
|
connections: subscriptions
|
|
|
|
|
.flat_map(|subscription| subscription.connections)
|
2019-12-05 06:10:13 +01:00
|
|
|
.collect(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 08:21:26 +01:00
|
|
|
pub fn connections(
|
|
|
|
|
self,
|
|
|
|
|
) -> Vec<Box<dyn Connection<Input = I, Output = O>>> {
|
|
|
|
|
self.connections
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 08:21:26 +01:00
|
|
|
impl<I, O, T> From<T> for Subscription<I, O>
|
2019-12-05 06:10:13 +01:00
|
|
|
where
|
2019-12-08 08:21:26 +01:00
|
|
|
T: Connection<Input = I, Output = O> + 'static,
|
2019-12-05 06:10:13 +01:00
|
|
|
{
|
2019-12-08 08:21:26 +01:00
|
|
|
fn from(handle: T) -> Self {
|
2019-12-05 06:10:13 +01:00
|
|
|
Self {
|
2019-12-08 08:21:26 +01:00
|
|
|
connections: vec![Box::new(handle)],
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 08:21:26 +01:00
|
|
|
/// The connection of an event subscription.
|
|
|
|
|
pub trait Connection {
|
|
|
|
|
type Input;
|
2019-12-07 08:51:44 +01:00
|
|
|
type Output;
|
2019-12-05 06:10:13 +01:00
|
|
|
|
|
|
|
|
fn id(&self) -> u64;
|
|
|
|
|
|
2019-12-08 08:21:26 +01:00
|
|
|
fn stream(
|
|
|
|
|
&self,
|
|
|
|
|
input: Self::Input,
|
|
|
|
|
) -> futures::stream::BoxStream<'static, Self::Output>;
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-08 08:21:26 +01:00
|
|
|
impl<I, O> std::fmt::Debug for Subscription<I, O> {
|
2019-12-05 06:10:13 +01:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2019-12-07 08:51:44 +01:00
|
|
|
f.debug_struct("Subscription").finish()
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
}
|