pub struct EventEmitter { /* private fields */ }Expand description
Event emitter for subscribing to and emitting events
The event emitter maintains a map of event types to their listeners. Multiple listeners can subscribe to the same event type, and all will be called when the event is emitted.
§Thread Safety
EventEmitter is thread-safe and can be shared across threads using Arc<EventEmitter>.
§Example
use gun::events::{EventEmitter, Event};
use serde_json::json;
use std::sync::Arc;
let emitter = Arc::new(EventEmitter::new());
// Subscribe to events
let emitter_clone = emitter.clone();
emitter.on("test_event", Box::new(move |event: &Event| {
println!("Received event: {:?}", event);
}));
// Emit an event
emitter.emit(&Event {
event_type: "test_event".to_string(),
data: json!({"message": "hello"}),
});Implementations§
Source§impl EventEmitter
impl EventEmitter
Sourcepub fn on(&self, event_type: &str, callback: EventCallback) -> u64
pub fn on(&self, event_type: &str, callback: EventCallback) -> u64
Register an event listener Returns the listener ID for later removal
§Panics
This function will panic if the lock is poisoned, which should never happen in practice since we don’t panic while holding the lock.
Sourcepub fn off(&self, event_type: &str, id: u64)
pub fn off(&self, event_type: &str, id: u64)
Remove an event listener by ID
§Panics
This function will panic if the lock is poisoned, which should never happen in practice since we don’t panic while holding the lock.
Sourcepub fn off_all(&self, event_type: &str)
pub fn off_all(&self, event_type: &str)
Remove all listeners for an event type
§Panics
This function will panic if the lock is poisoned, which should never happen in practice since we don’t panic while holding the lock.
Sourcepub fn emit(&self, event: &Event)
pub fn emit(&self, event: &Event)
Emit an event
§Panics
This function will panic if the lock is poisoned, which should never happen in practice since we don’t panic while holding the lock.
Sourcepub fn remove_all_listeners(&self, event_type: &str)
pub fn remove_all_listeners(&self, event_type: &str)
Remove all listeners for an event type
Sourcepub fn listener_count(&self, event_type: &str) -> usize
pub fn listener_count(&self, event_type: &str) -> usize
Get count of listeners for an event type
§Panics
This function will panic if the lock is poisoned, which should never happen in practice since we don’t panic while holding the lock.