2019-12-05 06:10:13 +01:00
|
|
|
//! Generate events asynchronously for you application.
|
|
|
|
|
|
|
|
|
|
/// An event subscription.
|
|
|
|
|
pub struct Subscription<T> {
|
2019-12-07 08:51:44 +01:00
|
|
|
handles: Vec<Box<dyn Handle<Output = T>>>,
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Subscription<T> {
|
|
|
|
|
pub fn none() -> Self {
|
|
|
|
|
Self {
|
2019-12-07 08:51:44 +01:00
|
|
|
handles: Vec::new(),
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn batch(subscriptions: impl Iterator<Item = Subscription<T>>) -> Self {
|
|
|
|
|
Self {
|
2019-12-07 08:51:44 +01:00
|
|
|
handles: subscriptions
|
|
|
|
|
.flat_map(|subscription| subscription.handles)
|
2019-12-05 06:10:13 +01:00
|
|
|
.collect(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 08:51:44 +01:00
|
|
|
pub fn handles(self) -> Vec<Box<dyn Handle<Output = T>>> {
|
|
|
|
|
self.handles
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T, A> From<A> for Subscription<T>
|
|
|
|
|
where
|
2019-12-07 08:51:44 +01:00
|
|
|
A: Handle<Output = T> + 'static,
|
2019-12-05 06:10:13 +01:00
|
|
|
{
|
2019-12-07 08:51:44 +01:00
|
|
|
fn from(handle: A) -> Self {
|
2019-12-05 06:10:13 +01:00
|
|
|
Self {
|
2019-12-07 08:51:44 +01:00
|
|
|
handles: vec![Box::new(handle)],
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 08:51:44 +01:00
|
|
|
/// The handle of an event subscription.
|
|
|
|
|
pub trait Handle {
|
|
|
|
|
type Output;
|
2019-12-05 06:10:13 +01:00
|
|
|
|
|
|
|
|
fn id(&self) -> u64;
|
|
|
|
|
|
2019-12-07 08:51:44 +01:00
|
|
|
fn stream(&self) -> futures::stream::BoxStream<'static, Self::Output>;
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> std::fmt::Debug for Subscription<T> {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|