pub struct SubscriptionState { /* private fields */ }Expand description
Generic subscription state tracker for WebSocket connections.
Maintains three separate states for subscriptions:
- Confirmed: Successfully subscribed and actively streaming data.
- Pending Subscribe: Subscription requested but not yet confirmed by server.
- Pending Unsubscribe: Unsubscription requested but not yet confirmed by server.
§Reference Counting
The tracker maintains reference counts for each topic. When multiple components subscribe to the same topic, only the first subscription sends a message to the server. Similarly, only the last unsubscription sends an unsubscribe message.
§Thread Safety
All operations are thread-safe and can be called concurrently from multiple tasks.
Implementations§
Source§impl SubscriptionState
impl SubscriptionState
Sourcepub fn new(delimiter: char) -> Self
pub fn new(delimiter: char) -> Self
Creates a new subscription state tracker with the specified topic delimiter.
Sourcepub fn confirmed(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>
pub fn confirmed(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>
Returns a clone of the confirmed subscriptions map.
Sourcepub fn pending_subscribe(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>
pub fn pending_subscribe(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>
Returns a clone of the pending subscribe map.
Sourcepub fn pending_unsubscribe(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>
pub fn pending_unsubscribe(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>
Returns a clone of the pending unsubscribe map.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of confirmed subscriptions.
Counts both channel-level and symbol-level subscriptions.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are no subscriptions (confirmed or pending).
Sourcepub fn mark_subscribe(&self, topic: &str)
pub fn mark_subscribe(&self, topic: &str)
Marks a topic as pending subscription.
This should be called after sending a subscribe request to the server. Idempotent: if topic is already confirmed, this is a no-op. If topic is pending unsubscription, removes it.
Sourcepub fn mark_unsubscribe(&self, topic: &str)
pub fn mark_unsubscribe(&self, topic: &str)
Marks a topic as pending unsubscription.
This removes the topic from both confirmed and pending_subscribe, then adds it to pending_unsubscribe. This handles the case where a user unsubscribes before the initial subscription is confirmed.
Sourcepub fn confirm_subscribe(&self, topic: &str)
pub fn confirm_subscribe(&self, topic: &str)
Confirms a subscription by moving it from pending to confirmed.
This should be called when the server acknowledges a subscribe request. Ignores the confirmation if the topic is pending unsubscription (handles late confirmations after user has already unsubscribed).
Sourcepub fn confirm_unsubscribe(&self, topic: &str)
pub fn confirm_unsubscribe(&self, topic: &str)
Confirms an unsubscription by removing it from pending and confirmed state.
This should be called when the server acknowledges an unsubscribe request. Removes the topic from pending_unsubscribe and confirmed. Does NOT clear pending_subscribe to support immediate re-subscribe patterns (e.g., user calls subscribe() before unsubscribe ack arrives).
Stale ACK handling: Ignores unsubscribe ACKs if the topic is no longer in pending_unsubscribe (meaning user has already re-subscribed). This prevents stale ACKs from removing topics that were re-confirmed after the re-subscribe.
Sourcepub fn mark_failure(&self, topic: &str)
pub fn mark_failure(&self, topic: &str)
Marks a subscription as failed, moving it from confirmed back to pending.
This is useful when a subscription fails but should be retried on reconnect. Ignores the failure if the topic is pending unsubscription (user cancelled it).
Sourcepub fn pending_subscribe_topics(&self) -> Vec<String>
pub fn pending_subscribe_topics(&self) -> Vec<String>
Returns all pending subscribe topics as strings.
Sourcepub fn pending_unsubscribe_topics(&self) -> Vec<String>
pub fn pending_unsubscribe_topics(&self) -> Vec<String>
Returns all pending unsubscribe topics as strings.
Sourcepub fn all_topics(&self) -> Vec<String>
pub fn all_topics(&self) -> Vec<String>
Returns all topics that should be active (confirmed + pending_subscribe).
This is the key method for reconnection: it returns all topics that should be resubscribed after a connection is re-established.
Note: Does NOT include pending_unsubscribe topics, as those are being removed.
Sourcepub fn add_reference(&self, topic: &str) -> bool
pub fn add_reference(&self, topic: &str) -> bool
Increments the reference count for a topic.
Returns true if this is the first subscription (caller should send subscribe
message to server).
§Panics
Panics if the reference count exceeds usize::MAX subscriptions for a single topic.
Sourcepub fn remove_reference(&self, topic: &str) -> bool
pub fn remove_reference(&self, topic: &str) -> bool
Decrements the reference count for a topic.
Returns true if this was the last subscription (caller should send unsubscribe
message to server).
§Panics
Panics if the internal reference count state becomes inconsistent (should never happen if the API is used correctly).
Sourcepub fn get_reference_count(&self, topic: &str) -> usize
pub fn get_reference_count(&self, topic: &str) -> usize
Returns the current reference count for a topic.
Returns 0 if the topic has no references.
Trait Implementations§
Source§impl Clone for SubscriptionState
impl Clone for SubscriptionState
Source§fn clone(&self) -> SubscriptionState
fn clone(&self) -> SubscriptionState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SubscriptionState
impl !RefUnwindSafe for SubscriptionState
impl Send for SubscriptionState
impl Sync for SubscriptionState
impl Unpin for SubscriptionState
impl !UnwindSafe for SubscriptionState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more