summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--runtime/src/command.rs21
-rw-r--r--src/lib.rs7
2 files changed, 27 insertions, 1 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))))
+}
diff --git a/src/lib.rs b/src/lib.rs
index f9f3952c..47766e6f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -190,7 +190,6 @@ pub use crate::core::{
color, Alignment, Background, BorderRadius, Color, ContentFit, Degrees,
Gradient, Length, Padding, Pixels, Point, Radians, Rectangle, Size, Vector,
};
-pub use crate::runtime::Command;
pub mod clipboard {
//! Access the clipboard.
@@ -239,6 +238,11 @@ pub mod mouse {
};
}
+pub mod command {
+ //! Run asynchronous actions.
+ pub use crate::runtime::command::{channel, Command};
+}
+
pub mod subscription {
//! Listen to external events in your application.
pub use iced_futures::subscription::{
@@ -287,6 +291,7 @@ pub mod widget {
}
pub use application::Application;
+pub use command::Command;
pub use error::Error;
pub use event::Event;
pub use executor::Executor;