diff options
author | 2023-11-29 00:14:27 +0100 | |
---|---|---|
committer | 2023-11-29 00:14:27 +0100 | |
commit | a761448858521d11dc646e2ef5217e9e06628932 (patch) | |
tree | 131f3c5364134f7eded5da5a1b68bcd0ef0af12a /runtime/src | |
parent | 3b7d479534d9114ed12bb5d9ccd910e85d5c13c7 (diff) | |
download | iced-a761448858521d11dc646e2ef5217e9e06628932.tar.gz iced-a761448858521d11dc646e2ef5217e9e06628932.tar.bz2 iced-a761448858521d11dc646e2ef5217e9e06628932.zip |
Implement `command::channel` helper
It is analogous to `subscription::channel`.
Diffstat (limited to 'runtime/src')
-rw-r--r-- | runtime/src/command.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/runtime/src/command.rs b/runtime/src/command.rs index b942f3ce..f70da915 100644 --- a/runtime/src/command.rs +++ b/runtime/src/command.rs @@ -7,6 +7,7 @@ use crate::core::widget; use crate::futures::futures; use crate::futures::MaybeSend; +use futures::channel::mpsc; use futures::Stream; use std::fmt; use std::future::Future; @@ -118,3 +119,23 @@ impl<T> fmt::Debug for Command<T> { command.fmt(f) } } + +/// Creates a [`Command`] that produces the `Message`s published from a [`Future`] +/// to an [`mpsc::Sender`] with the given bounds. +pub fn channel<Fut, Message>( + size: usize, + f: impl FnOnce(mpsc::Sender<Message>) -> Fut + MaybeSend + 'static, +) -> Command<Message> +where + Fut: Future<Output = ()> + MaybeSend + 'static, + Message: 'static + MaybeSend, +{ + use futures::future; + use futures::stream::{self, StreamExt}; + + let (sender, receiver) = mpsc::channel(size); + + let runner = stream::once(f(sender)).filter_map(|_| future::ready(None)); + + Command::single(Action::Stream(Box::pin(stream::select(receiver, runner)))) +} |