Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abortable Task #2496

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 55 additions & 18 deletions runtime/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,6 @@ impl<T> Task<T> {
Self::stream(stream.map(f))
}

/// Creates a new [`Task`] that runs the given [`Future`] and produces
/// its output.
pub fn future(future: impl Future<Output = T> + MaybeSend + 'static) -> Self
where
T: 'static,
{
Self::stream(stream::once(future))
}

/// Creates a new [`Task`] that runs the given [`Stream`] and produces
/// each of its items.
pub fn stream(stream: impl Stream<Item = T> + MaybeSend + 'static) -> Self
where
T: 'static,
{
Self(Some(boxed_stream(stream.map(Action::Output))))
}

/// Combines the given tasks and produces a single [`Task`] that will run all of them
/// in parallel.
pub fn batch(tasks: impl IntoIterator<Item = Self>) -> Self
Expand Down Expand Up @@ -176,6 +158,61 @@ impl<T> Task<T> {
))),
}
}

/// Creates a new [`Task`] that can be aborted with the returned [`Handle`].
pub fn abortable(self) -> (Self, Handle)
where
T: 'static,
{
match self.0 {
Some(stream) => {
let (stream, handle) = stream::abortable(stream);

(Self(Some(boxed_stream(stream))), Handle(Some(handle)))
}
None => (Self(None), Handle(None)),
}
}

/// Creates a new [`Task`] that runs the given [`Future`] and produces
/// its output.
pub fn future(future: impl Future<Output = T> + MaybeSend + 'static) -> Self
where
T: 'static,
{
Self::stream(stream::once(future))
}

/// Creates a new [`Task`] that runs the given [`Stream`] and produces
/// each of its items.
pub fn stream(stream: impl Stream<Item = T> + MaybeSend + 'static) -> Self
where
T: 'static,
{
Self(Some(boxed_stream(stream.map(Action::Output))))
}
}

/// A handle to a [`Task`] that can be used for aborting it.
#[derive(Debug, Clone)]
pub struct Handle(Option<stream::AbortHandle>);

impl Handle {
/// Aborts the [`Task`] of this [`Handle`].
pub fn abort(&self) {
if let Some(handle) = &self.0 {
handle.abort();
}
}

/// Returns `true` if the [`Task`] of this [`Handle`] has been aborted.
pub fn is_aborted(&self) -> bool {
if let Some(handle) = &self.0 {
handle.is_aborted()
} else {
true
}
}
}

impl<T> Task<Option<T>> {
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@ pub use crate::core::{
Length, Padding, Pixels, Point, Radians, Rectangle, Rotation, Shadow, Size,
Theme, Transformation, Vector,
};
pub use crate::runtime::{exit, Task};
pub use crate::runtime::exit;
pub use iced_futures::Subscription;

pub mod task {
//! Create runtime tasks.
pub use crate::runtime::task::{Handle, Task};
}

pub mod clipboard {
//! Access the clipboard.
pub use crate::runtime::clipboard::{
Expand Down Expand Up @@ -309,6 +314,7 @@ pub use executor::Executor;
pub use font::Font;
pub use renderer::Renderer;
pub use settings::Settings;
pub use task::Task;

#[doc(inline)]
pub use application::application;
Expand Down
Loading