flo_scene/host/commands/
read_command.rs

1use crate::host::command_trait::*;
2use crate::host::scene_context::*;
3use crate::host::scene_message::*;
4
5use futures::prelude::*;
6
7use std::marker::{PhantomData};
8
9///
10/// The read command just sends its input straight to its output (this is useful to read the result of a query when used with `spawn_query`)
11///
12pub struct ReadCommand<TInputType>(PhantomData<TInputType>);
13
14impl<TInputType> Default for ReadCommand<TInputType> {
15    fn default() -> Self {
16        ReadCommand(PhantomData)
17    }
18}
19
20impl<TInputType> Clone for ReadCommand<TInputType> {
21    fn clone(&self) -> Self {
22        ReadCommand(PhantomData)
23    }
24}
25
26impl<TInputType> Command for ReadCommand<TInputType> 
27where
28    TInputType: 'static + SceneMessage
29{
30    type Input = TInputType;
31    type Output = TInputType;
32
33    fn run<'a>(&'a self, input: impl 'static + Send + Stream<Item=Self::Input>, context: SceneContext) -> impl 'a + Send + Future<Output=()> {
34        async move {
35            if let Ok(output) = context.send(()) {
36                let mut input   = Box::pin(input);
37                let mut output  = output;
38
39                while let Some(next) = input.next().await {
40                    if let Err(_) = output.send(next).await {
41                        break;
42                    }
43                }
44            }
45        }
46    }
47}