diff options
author | 2019-12-05 06:10:13 +0100 | |
---|---|---|
committer | 2019-12-05 06:10:13 +0100 | |
commit | d575f4541126e2ab25908fe55c6805f16716b2a5 (patch) | |
tree | 9d5221fe201b64e2ec649228ebae26be819dbf58 /core/src/subscription.rs | |
parent | e92ea48e8814b42fc566017db085ca9bdaf3c272 (diff) | |
download | iced-d575f4541126e2ab25908fe55c6805f16716b2a5.tar.gz iced-d575f4541126e2ab25908fe55c6805f16716b2a5.tar.bz2 iced-d575f4541126e2ab25908fe55c6805f16716b2a5.zip |
Draft first version of event subscriptions :tada:
Diffstat (limited to 'core/src/subscription.rs')
-rw-r--r-- | core/src/subscription.rs | 61 |
1 files changed, 61 insertions, 0 deletions
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<T> { + definitions: Vec<Box<dyn Definition<Message = T>>>, +} + +impl<T> Subscription<T> { + pub fn none() -> Self { + Self { + definitions: Vec::new(), + } + } + + pub fn batch(subscriptions: impl Iterator<Item = Subscription<T>>) -> Self { + Self { + definitions: subscriptions + .flat_map(|subscription| subscription.definitions) + .collect(), + } + } + + pub fn definitions(self) -> Vec<Box<dyn Definition<Message = T>>> { + self.definitions + } +} + +impl<T, A> From<A> for Subscription<T> +where + A: Definition<Message = T> + '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<dyn Handle>, + ); +} + +pub trait Handle { + fn cancel(&mut self); +} + +impl<T> std::fmt::Debug for Subscription<T> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Command").finish() + } +} |