use futures::StreamExt;
use futures::stream::{BoxStream, select_all};
use super::Action;
use super::cancellation::CommandCancellation;
use super::runtime_directives::RuntimeDirectives;
use super::{CancellableCommand, CommandId};
#[must_use = "Runtime command parts may contain side effects and directives that must be handled by the runtime."]
pub struct RuntimeCommandParts<Msg: Send + 'static> {
directives: RuntimeDirectives,
leaves: Vec<BoxStream<'static, Action<Msg>>>,
cancels: Vec<CommandId>,
key: Option<CancellableCommand>,
}
impl<Msg: Send + 'static> RuntimeCommandParts<Msg> {
pub(super) fn new(
directives: RuntimeDirectives,
leaves: Vec<BoxStream<'static, Action<Msg>>>,
cancellation: CommandCancellation,
) -> Self {
Self {
directives,
leaves,
cancels: cancellation.cancels,
key: cancellation.key,
}
}
pub(crate) const fn requests_redraw(&self) -> bool {
self.directives.requests_redraw()
}
#[cfg(test)]
pub(crate) fn into_stream(self) -> Option<BoxStream<'static, Action<Msg>>> {
fold_leaves(self.leaves)
}
pub(crate) fn into_execution_parts(
self,
) -> (
Vec<CommandId>,
Option<CancellableCommand>,
Vec<BoxStream<'static, Action<Msg>>>,
) {
(self.cancels, self.key, self.leaves)
}
}
pub fn fold_leaves<Msg: Send + 'static>(
mut leaves: Vec<BoxStream<'static, Action<Msg>>>,
) -> Option<BoxStream<'static, Action<Msg>>> {
match leaves.len() {
0 => None,
1 => leaves.pop(),
_ => Some(select_all(leaves).boxed()),
}
}