pub struct EventLoop { /* private fields */ }Expand description
Generic event loop with type-safe callbacks
Implementations§
Source§impl EventLoop
impl EventLoop
Sourcepub fn new() -> (Self, UnboundedReceiver<(String, Value)>)
pub fn new() -> (Self, UnboundedReceiver<(String, Value)>)
Create a new event loop
Sourcepub async fn on_event<T, F, Fut>(&self, event_type: &str, callback: F)where
T: DeserializeOwned + Send + Sync + 'static,
F: Fn(ServerEvent<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
pub async fn on_event<T, F, Fut>(&self, event_type: &str, callback: F)where
T: DeserializeOwned + Send + Sync + 'static,
F: Fn(ServerEvent<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
Sourcepub async fn on<T, F, Fut>(&self, event_type: &'static str, handler: F)where
T: DeserializeOwned + Send + Sync + 'static,
F: Fn(ServerEvent<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
pub async fn on<T, F, Fut>(&self, event_type: &'static str, handler: F)where
T: DeserializeOwned + Send + Sync + 'static,
F: Fn(ServerEvent<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
Register a handler function directly (simplified API)
This is a convenience wrapper around on_event that allows direct
registration of async handler functions without wrapping them in closures.
§Example
ⓘ
// 定义 handler 函数
async fn on_message_received(event: ServerEvent<MessageReceivedData>) {
println!("New message: {:?}", event.data.content);
}
// 直接注册,无需包装闭包
event_loop.on(EVENT_MESSAGE_RECEIVED, on_message_received).await;Sourcepub async fn clear_callbacks(&self)
pub async fn clear_callbacks(&self)
Clear all callbacks
Sourcepub async fn process_event(&self, event_type: &str, payload: Value)
pub async fn process_event(&self, event_type: &str, payload: Value)
Process a single event
Sourcepub async fn start(&self, event_rx: UnboundedReceiver<(String, Value)>)
pub async fn start(&self, event_rx: UnboundedReceiver<(String, Value)>)
Start the event loop
Sourcepub async fn is_running(&self) -> bool
pub async fn is_running(&self) -> bool
Check if the event loop is running
Sourcepub fn event_sender(&self) -> UnboundedSender<(String, Value)>
pub fn event_sender(&self) -> UnboundedSender<(String, Value)>
Get event sender
Trait Implementations§
Auto Trait Implementations§
impl Freeze for EventLoop
impl !RefUnwindSafe for EventLoop
impl Send for EventLoop
impl Sync for EventLoop
impl Unpin for EventLoop
impl UnsafeUnpin for EventLoop
impl !UnwindSafe for EventLoop
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
Mutably borrows from an owned value. Read more