use livekit::id::ParticipantIdentity;
use smallvec::SmallVec;
pub(crate) struct ChannelSubscription {
subscribers: SmallVec<[ParticipantIdentity; 1]>,
version: u32,
}
impl ChannelSubscription {
pub(crate) fn new() -> Self {
Self {
subscribers: SmallVec::new(),
version: 0,
}
}
fn bump_version(&mut self) {
self.version = self.version.wrapping_add(1);
}
pub fn version(&self) -> u32 {
self.version
}
pub fn subscribers(&self) -> &[ParticipantIdentity] {
&self.subscribers
}
pub fn is_empty(&self) -> bool {
self.subscribers.is_empty()
}
pub fn add(&mut self, identity: ParticipantIdentity) -> bool {
if self.subscribers.iter().any(|id| id == &identity) {
false
} else {
self.subscribers.push(identity);
self.bump_version();
true
}
}
pub fn remove(&mut self, identity: &ParticipantIdentity) -> bool {
let Some(pos) = self.subscribers.iter().position(|id| id == identity) else {
return false;
};
self.subscribers.swap_remove(pos);
self.bump_version();
true
}
}