Implement Command::run for executing a Stream to completion
This commit is contained in:
parent
133f4da901
commit
3b7d479534
4 changed files with 48 additions and 3 deletions
|
|
@ -1,7 +1,7 @@
|
|||
//! Run commands and keep track of subscriptions.
|
||||
use crate::core::event::{self, Event};
|
||||
use crate::subscription;
|
||||
use crate::{BoxFuture, Executor, MaybeSend};
|
||||
use crate::{BoxFuture, BoxStream, Executor, MaybeSend};
|
||||
|
||||
use futures::{channel::mpsc, Sink};
|
||||
use std::marker::PhantomData;
|
||||
|
|
@ -69,6 +69,29 @@ where
|
|||
self.executor.spawn(future);
|
||||
}
|
||||
|
||||
/// Runs a [`Stream`] in the [`Runtime`] until completion.
|
||||
///
|
||||
/// The resulting `Message`s will be forwarded to the `Sender` of the
|
||||
/// [`Runtime`].
|
||||
///
|
||||
/// [`Stream`]: BoxStream
|
||||
pub fn run(&mut self, stream: BoxStream<Message>) {
|
||||
use futures::{FutureExt, StreamExt};
|
||||
|
||||
let sender = self.sender.clone();
|
||||
let future =
|
||||
stream.map(Ok).forward(sender).map(|result| match result {
|
||||
Ok(()) => (),
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
"Stream could not run until completion: {error}"
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
self.executor.spawn(future);
|
||||
}
|
||||
|
||||
/// Tracks a [`Subscription`] in the [`Runtime`].
|
||||
///
|
||||
/// It will spawn new streams or close old ones as necessary! See
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue