Skip to content

Commit 6b6644a

Browse files
authored
feat: Add an is_listening() method to the event listener
This allows users to determine if the listener is registered without needing to keep track of external state. Signed-off-by: John Nunley <[email protected]>
1 parent 7c42a41 commit 6b6644a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,32 @@ impl<T> EventListener<T> {
679679
notify::full_fence();
680680
}
681681

682+
/// Tell if this [`EventListener`] is currently listening for a notification.
683+
///
684+
/// # Examples
685+
///
686+
/// ```
687+
/// use event_listener::{Event, EventListener};
688+
///
689+
/// let event = Event::new();
690+
/// let mut listener = Box::pin(EventListener::new(&event));
691+
///
692+
/// // The listener starts off not listening.
693+
/// assert!(!listener.is_listening());
694+
///
695+
/// // After listen() is called, the listener is listening.
696+
/// listener.as_mut().listen();
697+
/// assert!(listener.is_listening());
698+
///
699+
/// // Once the future is notified, the listener is no longer listening.
700+
/// event.notify(1);
701+
/// listener.as_mut().wait();
702+
/// assert!(!listener.is_listening());
703+
/// ```
704+
pub fn is_listening(&self) -> bool {
705+
self.0.listener.is_some()
706+
}
707+
682708
/// Blocks until a notification is received.
683709
///
684710
/// # Examples

0 commit comments

Comments
 (0)