From 98160406f714728afe718f305bf9d12be1676b2d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 8 Dec 2019 08:21:26 +0100 Subject: Allow listening to runtime events in subscriptions --- core/src/subscription.rs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'core/src/subscription.rs') diff --git a/core/src/subscription.rs b/core/src/subscription.rs index 796982c7..4c021d75 100644 --- a/core/src/subscription.rs +++ b/core/src/subscription.rs @@ -1,51 +1,59 @@ //! Generate events asynchronously for you application. /// An event subscription. -pub struct Subscription { - handles: Vec>>, +pub struct Subscription { + connections: Vec>>, } -impl Subscription { +impl Subscription { pub fn none() -> Self { Self { - handles: Vec::new(), + connections: Vec::new(), } } - pub fn batch(subscriptions: impl Iterator>) -> Self { + pub fn batch( + subscriptions: impl Iterator>, + ) -> Self { Self { - handles: subscriptions - .flat_map(|subscription| subscription.handles) + connections: subscriptions + .flat_map(|subscription| subscription.connections) .collect(), } } - pub fn handles(self) -> Vec>> { - self.handles + pub fn connections( + self, + ) -> Vec>> { + self.connections } } -impl From for Subscription +impl From for Subscription where - A: Handle + 'static, + T: Connection + '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 std::fmt::Debug for Subscription { +impl std::fmt::Debug for Subscription { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Subscription").finish() } -- cgit