rsiot/components/cmp_leptos/component/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use gloo::storage::{LocalStorage, Storage};
use leptos::prelude::*;
use reactive_stores::Store;
use tokio::{sync::mpsc, task::JoinSet};
use tracing::debug;

use crate::{
    executor::{join_set_spawn, CmpInOut},
    message::{system_messages::*, *},
};

use super::{
    super::{utils, Error, GlobalState, Result},
    Config, StoreBound,
};

pub async fn fn_process<TMsg, TView, TIntoView, TService, TInputStore, TOutputStore>(
    config: Config<TMsg, TView, TIntoView, TInputStore, TOutputStore>,
    in_out: CmpInOut<TMsg, TService>,
) -> Result
where
    TMsg: MsgDataBound + 'static,
    TView: Fn() -> TIntoView + 'static,
    TIntoView: IntoView,
    TService: ServiceBound + 'static,
    TInputStore: StoreBound + 'static,
    TOutputStore: StoreBound + 'static,
{
    let hostname = utils::define_hostname().unwrap();

    let gs = GlobalState::<TMsg> {
        hostname,
        input: RwSignal::new(None),
        output: RwSignal::new(None),
        cache: in_out.cache.clone(),
        auth_perm: RwSignal::new(AuthPermissions::NoAccess),
    };

    // Монтируем корневой компонент
    let input_store = Store::new(config.input_store);
    let input_store_clone = input_store;
    let output_store = Store::new(config.output_store);
    let output_store_clone = output_store;
    mount_to_body(move || {
        view! {
            <RootComponent
                body_component = config.body_component
                input_store=input_store_clone
                output_store=output_store_clone
            />
        }
    });
    debug!("Leptos app mounted");

    let mut task_set: JoinSet<Result> = JoinSet::new();

    let task = task_input(in_out.clone(), config.fn_input, input_store);
    join_set_spawn(&mut task_set, task);

    let task = task_output(in_out.clone(), config.fn_output, output_store);
    join_set_spawn(&mut task_set, task);

    while let Some(task_result) = task_set.join_next().await {
        task_result??
    }
    Ok(())
}

async fn task_input<TMsg, TService, TInputStore>(
    mut msg_bus: CmpInOut<TMsg, TService>,
    fn_input: fn(&Message<TMsg>, &Store<TInputStore>),
    input_store: Store<TInputStore>,
) -> Result
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
    TInputStore: StoreBound + 'static,
{
    while let Ok(msg) = msg_bus.recv_input().await {
        (fn_input)(&msg, &input_store);
    }
    Ok(())
}

async fn task_output<TMsg, TService, TOutputStore>(
    msg_bus: CmpInOut<TMsg, TService>,
    fn_output: fn(Store<TOutputStore>, mpsc::Sender<TMsg>),
    output_store: Store<TOutputStore>,
) -> Result
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
    TOutputStore: StoreBound + 'static,
{
    let (tx, mut rx) = mpsc::channel(100);

    (fn_output)(output_store, tx);

    while let Some(msg) = rx.recv().await {
        let msg = Message::new_custom(msg);
        msg_bus.send_output(msg).await.map_err(Error::CmpOutput)?;
    }

    Ok(())
}

/// Пробуем найти токен в LocalStorage.
///
/// Если токен присутствует, отправляем запрос на проверку токена
fn try_to_find_token<TMsg>() -> Option<Message<TMsg>>
where
    TMsg: MsgDataBound,
{
    let msg: Message<TMsg> = LocalStorage::get("System-AuthResponseOk").ok()?;
    match msg.data {
        MsgData::System(System::AuthResponseOk(value)) => {
            let value = AuthRequestByToken { token: value.token };
            let msg = message_new!("System-AuthRequestByToken::value");
            Some(msg)
        }
        _ => None,
    }
}

/// Корневой компонент
#[component]
fn RootComponent<TView, TIntoView, TInputStore, TOutputStore>(
    body_component: TView,
    input_store: Store<TInputStore>,
    output_store: Store<TOutputStore>,
) -> impl IntoView
where
    TView: Fn() -> TIntoView,
    TIntoView: IntoView,
    TInputStore: StoreBound + 'static,
    TOutputStore: StoreBound + 'static,
{
    provide_context(input_store);
    provide_context(output_store);

    view! {
        { body_component() }
    }
}