Allow listening to runtime events in subscriptions
This commit is contained in:
parent
9b84b6e403
commit
98160406f7
7 changed files with 89 additions and 40 deletions
|
|
@ -1,51 +1,59 @@
|
|||
//! Generate events asynchronously for you application.
|
||||
|
||||
/// An event subscription.
|
||||
pub struct Subscription<T> {
|
||||
handles: Vec<Box<dyn Handle<Output = T>>>,
|
||||
pub struct Subscription<I, O> {
|
||||
connections: Vec<Box<dyn Connection<Input = I, Output = O>>>,
|
||||
}
|
||||
|
||||
impl<T> Subscription<T> {
|
||||
impl<I, O> Subscription<I, O> {
|
||||
pub fn none() -> Self {
|
||||
Self {
|
||||
handles: Vec::new(),
|
||||
connections: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn batch(subscriptions: impl Iterator<Item = Subscription<T>>) -> Self {
|
||||
pub fn batch(
|
||||
subscriptions: impl Iterator<Item = Subscription<I, O>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
handles: subscriptions
|
||||
.flat_map(|subscription| subscription.handles)
|
||||
connections: subscriptions
|
||||
.flat_map(|subscription| subscription.connections)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handles(self) -> Vec<Box<dyn Handle<Output = T>>> {
|
||||
self.handles
|
||||
pub fn connections(
|
||||
self,
|
||||
) -> Vec<Box<dyn Connection<Input = I, Output = O>>> {
|
||||
self.connections
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A> From<A> for Subscription<T>
|
||||
impl<I, O, T> From<T> for Subscription<I, O>
|
||||
where
|
||||
A: Handle<Output = T> + 'static,
|
||||
T: Connection<Input = I, Output = O> + 'static,
|
||||
{
|
||||
fn from(handle: A) -> Self {
|
||||
fn from(handle: T) -> Self {
|
||||
Self {
|
||||
handles: vec![Box::new(handle)],
|
||||
connections: vec![Box::new(handle)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The handle of an event subscription.
|
||||
pub trait Handle {
|
||||
/// The connection of an event subscription.
|
||||
pub trait Connection {
|
||||
type Input;
|
||||
type Output;
|
||||
|
||||
fn id(&self) -> u64;
|
||||
|
||||
fn stream(&self) -> futures::stream::BoxStream<'static, Self::Output>;
|
||||
fn stream(
|
||||
&self,
|
||||
input: Self::Input,
|
||||
) -> futures::stream::BoxStream<'static, Self::Output>;
|
||||
}
|
||||
|
||||
impl<T> std::fmt::Debug for Subscription<T> {
|
||||
impl<I, O> std::fmt::Debug for Subscription<I, O> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Subscription").finish()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue