Skip to content

Commit

Permalink
feat: Add an is_listening() method to the event listener
Browse files Browse the repository at this point in the history
This allows users to determine if the listener is registered without needing to keep track of external state.

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Aug 6, 2023
1 parent 7c42a41 commit 6b6644a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,32 @@ impl<T> EventListener<T> {
notify::full_fence();
}

/// Tell if this [`EventListener`] is currently listening for a notification.
///
/// # Examples
///
/// ```
/// use event_listener::{Event, EventListener};
///
/// let event = Event::new();
/// let mut listener = Box::pin(EventListener::new(&event));
///
/// // The listener starts off not listening.
/// assert!(!listener.is_listening());
///
/// // After listen() is called, the listener is listening.
/// listener.as_mut().listen();
/// assert!(listener.is_listening());
///
/// // Once the future is notified, the listener is no longer listening.
/// event.notify(1);
/// listener.as_mut().wait();
/// assert!(!listener.is_listening());
/// ```
pub fn is_listening(&self) -> bool {
self.0.listener.is_some()
}

/// Blocks until a notification is received.
///
/// # Examples
Expand Down

0 comments on commit 6b6644a

Please sign in to comment.