rsiot/components/cmp_webstorage/tasks/
input.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
use gloo::storage::{LocalStorage, SessionStorage, Storage};

use crate::message::MsgDataBound;

use super::{
    super::{config::FnInput, ConfigStorageKind},
    TaskInput, TaskOutput,
};

pub struct Input<TMsg>
where
    TMsg: MsgDataBound,
{
    pub input: TaskInput<TMsg>,
    pub output: TaskOutput<TMsg>,
    pub storage_kind: ConfigStorageKind,
    pub fn_input: FnInput<TMsg>,
}

impl<TMsg> Input<TMsg>
where
    TMsg: MsgDataBound,
{
    pub async fn spawn(mut self) -> super::Result<()> {
        while let Some(msg) = self.input.recv().await {
            let msg = (self.fn_input)(msg);
            let Some(msg) = msg else { continue };
            match self.storage_kind {
                ConfigStorageKind::LocalStorage => LocalStorage::set(msg.key.clone(), &msg)?,
                ConfigStorageKind::SessionStorage => SessionStorage::set(msg.key.clone(), &msg)?,
            };
            // Отправляем сообщение на выход
            self.output
                .send(msg)
                .await
                .map_err(|e| super::Error::TokioSyncMpsc(e.to_string()))?;
        }

        Err(super::Error::TaskEndInput)
    }
}