settings/message/
delegate.rs1use crate::message::base::messenger::Descriptor;
6#[cfg(test)]
7use crate::message::base::MessengerPresenceResult;
8use crate::message::base::{
9 CreateMessengerResult, MessageError, MessengerAction, MessengerActionSender, MessengerType,
10 Signature,
11};
12
13#[derive(Clone)]
16pub struct Delegate {
17 messenger_action_tx: MessengerActionSender,
18}
19
20impl Delegate {
21 pub(super) fn new(action_tx: MessengerActionSender) -> Delegate {
22 Delegate { messenger_action_tx: action_tx }
23 }
24
25 pub(crate) async fn create(&self, messenger_type: MessengerType) -> CreateMessengerResult {
26 self.create_messenger(messenger_type).await
27 }
28
29 #[cfg(test)]
30 pub(crate) async fn create_sink(&self) -> CreateMessengerResult {
31 self.create_messenger(MessengerType::EventSink).await
32 }
33
34 async fn create_messenger(&self, messenger_type: MessengerType) -> CreateMessengerResult {
35 let (tx, rx) = futures::channel::oneshot::channel::<CreateMessengerResult>();
36
37 self.messenger_action_tx
39 .unbounded_send(MessengerAction::Create(
40 Descriptor { messenger_type },
41 tx,
42 self.messenger_action_tx.clone(),
43 ))
44 .expect("Failed to send MessengerAction::Create message");
45
46 rx.await.map_err(|_| MessageError::Unexpected).and_then(std::convert::identity)
47 }
48
49 #[cfg(test)]
53 pub(crate) async fn contains(&self, signature: Signature) -> MessengerPresenceResult {
54 let (tx, rx) = futures::channel::oneshot::channel::<MessengerPresenceResult>();
55 self.messenger_action_tx
56 .unbounded_send(MessengerAction::CheckPresence(signature, tx))
57 .unwrap();
58 rx.await.unwrap_or(Err(MessageError::Unexpected))
59 }
60
61 pub(crate) fn delete(&self, signature: Signature) {
62 self.messenger_action_tx
63 .unbounded_send(MessengerAction::DeleteBySignature(signature))
64 .expect(
65 "Delegate::delete, messenger_action_tx failed to send DeleteBySignature messenger\
66 action",
67 );
68 }
69}