File size: 2,137 Bytes
287a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::sync::Arc;

use tokio::select;

use crate::system::ComponentContext;

use super::{
    sender::{Sender, Wrapper},
    system::System,
    Component,
};

struct Inner<C>
where
    C: Component,
{
    pub(super) sender: Sender<C>,
    pub(super) cancellation_token: tokio_util::sync::CancellationToken,
    pub(super) system: System,
}

#[derive(Clone)]
/// # Description
/// The executor holds the context for a components execution and is responsible for
/// running the components handler methods
pub(super) struct ComponentExecutor<C>
where
    C: Component,
{
    inner: Arc<Inner<C>>,
    handler: C,
}

impl<C> ComponentExecutor<C>
where
    C: Component + Send + 'static,
{
    pub(super) fn new(
        sender: Sender<C>,
        cancellation_token: tokio_util::sync::CancellationToken,
        handler: C,
        system: System,
    ) -> Self {
        ComponentExecutor {
            inner: Arc::new(Inner {
                sender,
                cancellation_token,
                system,
            }),
            handler,
        }
    }

    pub(super) async fn run(&mut self, mut channel: tokio::sync::mpsc::Receiver<Wrapper<C>>) {
        loop {
            select! {
                    _ = self.inner.cancellation_token.cancelled() => {
                        break;
                    }
                    message = channel.recv() => {
                        match message {
                            Some(mut message) => {
                                message.handle(&mut self.handler,
                                    &ComponentContext{
                                        system: self.inner.system.clone(),
                                        sender: self.inner.sender.clone(),
                                        cancellation_token: self.inner.cancellation_token.clone(),
                                    }
                                ).await;
                            }
                            None => {
                                // TODO: Log error
                            }
                        }
                }
            }
        }
    }
}