rsiot/components/shared_tasks/cmp_websocket_client_general/tasks/
connection_state.rsuse tokio::sync::mpsc;
use crate::{
executor::CmpInOut,
message::{Message, MsgDataBound},
};
pub struct ConnectionState<TMsg>
where
TMsg: MsgDataBound,
{
pub input: mpsc::Receiver<bool>,
pub output: CmpInOut<TMsg>,
pub fn_connection_state: fn(bool) -> Option<Message<TMsg>>,
}
impl<TMsg> ConnectionState<TMsg>
where
TMsg: MsgDataBound,
{
pub async fn spawn(mut self) -> super::Result<()> {
create_msg_and_send(false, self.fn_connection_state, &self.output).await?;
while let Some(state) = self.input.recv().await {
create_msg_and_send(state, self.fn_connection_state, &self.output).await?;
}
Err(super::Error::TaskConnectionState)
}
}
async fn create_msg_and_send<TMsg>(
state: bool,
fn_connection_state: fn(bool) -> Option<Message<TMsg>>,
output: &CmpInOut<TMsg>,
) -> super::Result<()>
where
TMsg: MsgDataBound,
{
let msg = (fn_connection_state)(state);
let Some(msg) = msg else { return Ok(()) };
output
.send_output(msg)
.await
.map_err(|_| super::Error::TokioSyncMpsc)
}