From d575f4541126e2ab25908fe55c6805f16716b2a5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 5 Dec 2019 06:10:13 +0100 Subject: Draft first version of event subscriptions :tada: --- core/src/subscription.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 core/src/subscription.rs (limited to 'core/src/subscription.rs') diff --git a/core/src/subscription.rs b/core/src/subscription.rs new file mode 100644 index 00000000..1e6695d6 --- /dev/null +++ b/core/src/subscription.rs @@ -0,0 +1,61 @@ +//! Generate events asynchronously for you application. + +/// An event subscription. +pub struct Subscription { + definitions: Vec>>, +} + +impl Subscription { + pub fn none() -> Self { + Self { + definitions: Vec::new(), + } + } + + pub fn batch(subscriptions: impl Iterator>) -> Self { + Self { + definitions: subscriptions + .flat_map(|subscription| subscription.definitions) + .collect(), + } + } + + pub fn definitions(self) -> Vec>> { + self.definitions + } +} + +impl From for Subscription +where + A: Definition + 'static, +{ + fn from(definition: A) -> Self { + Self { + definitions: vec![Box::new(definition)], + } + } +} + +/// The definition of an event subscription. +pub trait Definition { + type Message; + + fn id(&self) -> u64; + + fn stream( + &self, + ) -> ( + futures::stream::BoxStream<'static, Self::Message>, + Box, + ); +} + +pub trait Handle { + fn cancel(&mut self); +} + +impl std::fmt::Debug for Subscription { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Command").finish() + } +} -- cgit