rsiot/components/cmp_slint/
fn_process.rs

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
use slint::ComponentHandle;
use tokio::{sync::mpsc, task::JoinSet};

use crate::{
    executor::CmpInOut,
    message::{MsgDataBound, ServiceBound},
};

use super::{Config, Result};

pub async fn fn_process<TMainWindow, TMsg, TService>(
    config: Config<TMainWindow, TMsg>,
    input: CmpInOut<TMsg, TService>,
) -> Result<()>
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
    TMainWindow: ComponentHandle + 'static,
{
    let mut task_set = JoinSet::new();
    task_set.spawn(fn_input(config.clone(), input.clone()));
    task_set.spawn(fn_output(config.clone(), input));

    while let Some(res) = task_set.join_next().await {
        res.unwrap();
    }

    Ok(())
}

async fn fn_input<TMainWindow, TMsg, TService>(
    config: Config<TMainWindow, TMsg>,
    mut input: CmpInOut<TMsg, TService>,
) where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
    TMainWindow: ComponentHandle,
{
    while let Ok(msg) = input.recv_input().await {
        let lock = config.instance.lock().await;
        (config.fn_input)(msg, lock);
    }
}

async fn fn_output<TMainWindow, TMsg, TService>(
    config: Config<TMainWindow, TMsg>,
    input: CmpInOut<TMsg, TService>,
) where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
    TMainWindow: ComponentHandle,
{
    let (tx, mut rx) = mpsc::channel(100);

    {
        let lock = config.instance.lock().await;
        (config.fn_output)(lock, tx);
    }

    while let Some(msg) = rx.recv().await {
        input.send_output(msg).await.unwrap();
    }
}