lmc/
subs.rs

1use tokio::sync::mpsc;
2use super::transceiver::commands::{Command, UnsubCommand, UnsubKind};
3
4/// A reference to a "fast callback" subscription that can be used to remove
5/// the said callback.
6/// 
7/// Note that if this value is dropped before calling [`Callback::remove_callback()`],
8/// there will be no way to remove it anymore.
9/// 
10/// Also note that since v0.2, removing a callback will never cause the client to
11/// unsubscribe from the topic. Instead,
12/// [`Client::unsubscribe()`](super::Client::unsubscribe()) should be used to
13/// unsubscribe from a topic.
14pub struct Callback
15{
16    cmd_queue: mpsc::Sender<Command>,
17    topic: String,
18    id: u32
19}
20
21impl Callback
22{
23    /// Creates a [`Callback`] susbcription object.
24    /// 
25    /// Note that an entry corresponding to this topic must already exist
26    /// in the subscription map and that callback should have already
27    /// been registered.
28    pub(super) fn new(cmd_queue: mpsc::Sender<Command>, topic: String, id: u32) -> Self
29    {
30        Self { cmd_queue, topic, id }
31    }
32
33    /// Removes the callback from the susbcription.
34    /// To completely unsubscribe from a topic, use
35    /// [`Client::unsubscribe()`](super::Client::unsubscribe()).
36    pub async fn remove_callback(self)
37    {
38        let cmd = UnsubCommand { topic: self.topic, kind: UnsubKind::FastCallback(self.id) };
39        let _ = self.cmd_queue.send(cmd.into()).await; //Only possible error is that the channel has been closed, meaning we're unsubscribed anyway
40    }
41}