rsiot/components/cmp_webstorage/tasks/
input.rs

1use gloo::storage::{LocalStorage, SessionStorage, Storage};
2
3use crate::message::MsgDataBound;
4
5use super::{
6    super::{config::FnInput, ConfigStorageKind},
7    TaskInput, TaskOutput,
8};
9
10pub struct Input<TMsg>
11where
12    TMsg: MsgDataBound,
13{
14    pub input: TaskInput<TMsg>,
15    pub output: TaskOutput<TMsg>,
16    pub storage_kind: ConfigStorageKind,
17    pub fn_input: FnInput<TMsg>,
18}
19
20impl<TMsg> Input<TMsg>
21where
22    TMsg: MsgDataBound,
23{
24    pub async fn spawn(mut self) -> super::Result<()> {
25        while let Some(msg) = self.input.recv().await {
26            let msg = (self.fn_input)(msg);
27            let Some(msg) = msg else { continue };
28            match self.storage_kind {
29                ConfigStorageKind::LocalStorage => LocalStorage::set(msg.key.clone(), &msg)?,
30                ConfigStorageKind::SessionStorage => SessionStorage::set(msg.key.clone(), &msg)?,
31            };
32            // Отправляем сообщение на выход
33            self.output
34                .send(msg)
35                .await
36                .map_err(|e| super::Error::TokioSyncMpsc(e.to_string()))?;
37        }
38
39        Err(super::Error::TaskEndInput)
40    }
41}