trillium_channels/subscriptions.rs
1use crate::ChannelEvent;
2use dashmap::DashSet;
3use std::sync::Arc;
4
5/**
6A data structure that tracks what topics a given client is subscribed to.
7*/
8#[derive(Clone, Default, Debug)]
9pub struct Subscriptions(Arc<DashSet<String>>);
10impl Subscriptions {
11 /**
12 adds the provided topic to the set of subscriptions. please note
13 that this is case sensitive .
14 */
15 pub fn join(&self, topic: String) {
16 self.0.insert(topic);
17 }
18
19 /**
20 removes the provided topic to the set of subscriptions, if it was
21 previously subscribed. please note that this is case sensitive
22 */
23 pub fn leave(&self, topic: &str) {
24 self.0.remove(topic);
25 }
26
27 /**
28 predicate function to determine if a ChannelEvent is applicable to
29 a given user. `phx_join` and `phx_leave` are always applicable, as
30 are any topics that are subscribed to by this client (as an exact
31 match).
32 */
33 pub fn subscribes(&self, event: &ChannelEvent) -> bool {
34 event.is_system_event() || self.0.contains(event.topic())
35 }
36}