pub struct EventBus<T: Send + Sync + Clone + 'static> { /* private fields */ }
Expand description
高性能事件总线
使用以下优化策略:
- ArcSwap 实现无锁读取事件处理器列表
- DashMap 用于快速查找和管理事件处理器
- 原子计数器生成唯一 ID
- 批量事件处理优化
Implementations§
Source§impl<T: Send + Sync + Clone + 'static> EventBus<T>
impl<T: Send + Sync + Clone + 'static> EventBus<T>
Sourcepub fn add_event_handler(
&self,
event_handler: Arc<dyn EventHandler<T> + Send + Sync>,
) -> ForgeResult<HandlerId>
pub fn add_event_handler( &self, event_handler: Arc<dyn EventHandler<T> + Send + Sync>, ) -> ForgeResult<HandlerId>
添加事件处理器,返回处理器 ID
Sourcepub fn add_event_handlers(
&self,
event_handlers: Vec<Arc<dyn EventHandler<T> + Send + Sync>>,
) -> ForgeResult<Vec<HandlerId>>
pub fn add_event_handlers( &self, event_handlers: Vec<Arc<dyn EventHandler<T> + Send + Sync>>, ) -> ForgeResult<Vec<HandlerId>>
批量添加事件处理器
Sourcepub fn remove_event_handler(&self, handler_id: HandlerId) -> ForgeResult<bool>
pub fn remove_event_handler(&self, handler_id: HandlerId) -> ForgeResult<bool>
移除事件处理器
Sourcepub fn remove_event_handlers(
&self,
handler_ids: &[HandlerId],
) -> ForgeResult<usize>
pub fn remove_event_handlers( &self, handler_ids: &[HandlerId], ) -> ForgeResult<usize>
批量移除事件处理器
Sourcepub fn handler_count(&self) -> usize
pub fn handler_count(&self) -> usize
获取当前活跃的处理器数量
Sourcepub fn clear_handlers(&self) -> ForgeResult<()>
pub fn clear_handlers(&self) -> ForgeResult<()>
清空所有事件处理器
Sourcepub async fn destroy(&self) -> ForgeResult<()>
pub async fn destroy(&self) -> ForgeResult<()>
异步销毁事件总线
Sourcepub fn destroy_blocking(&self)
pub fn destroy_blocking(&self)
同步销毁事件总线(仅在非异步上下文中使用)
⚠️ 警告:此方法可能阻塞,应优先使用 destroy()
异步版本
Sourcepub fn start_event_loop(&self)
pub fn start_event_loop(&self)
启动事件循环
pub fn new() -> Self
pub fn with_config(config: EventConfig) -> Self
pub fn subscribe(&self) -> Receiver<T>
pub async fn broadcast(&self, event: T) -> ForgeResult<()>
Sourcepub fn broadcast_blocking(&self, event: T) -> ForgeResult<()>
pub fn broadcast_blocking(&self, event: T) -> ForgeResult<()>
Sourcepub fn get_config(&self) -> &EventConfig
pub fn get_config(&self) -> &EventConfig
获取事件配置
Sourcepub fn update_config(&mut self, config: EventConfig)
pub fn update_config(&mut self, config: EventConfig)
更新事件配置(注意:某些配置更改需要重启事件循环才能生效)
Sourcepub fn get_stats(&self) -> EventBusStats
pub fn get_stats(&self) -> EventBusStats
获取事件总线统计信息
Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
重置统计信息
Sourcepub fn get_performance_report(&self) -> EventBusPerformanceReport
pub fn get_performance_report(&self) -> EventBusPerformanceReport
获取详细的性能报告
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for EventBus<T>
impl<T> !RefUnwindSafe for EventBus<T>
impl<T> Send for EventBus<T>
impl<T> Sync for EventBus<T>
impl<T> !Unpin for EventBus<T>
impl<T> !UnwindSafe for EventBus<T>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
Converts
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>
Converts
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