pub struct EventDispatcher { /* private fields */ }Expand description
事件分发器
管理 Observer 与 EventSubscriber 的注册与分发。
§分发顺序
- 先按注册顺序调用所有
Observer的对应方法 - 再按注册顺序调用所有订阅了该事件的
EventSubscriber
§错误处理
before_*事件中任何订阅者返回Err(Vetoed)会立即中止后续执行after_*事件中的错误仅记录,不影响后续执行
§线程安全
内部使用 RwLock<Vec<Arc<...>>>,支持多线程并发。
§死锁防护(v0.2.1 修复 Critical C-3)
dispatch / dispatch_before_mut 在调用用户回调前会先 clone 一份
Vec<Arc<dyn ...>> 快照并释放读锁,避免持读锁调用用户代码——
否则用户回调中若尝试注册新订阅者(需要写锁)会自我死锁。
Implementations§
Source§impl EventDispatcher
impl EventDispatcher
Sourcepub fn with_max_errors(self, max_errors: usize) -> Self
pub fn with_max_errors(self, max_errors: usize) -> Self
设置错误缓冲区最大容量
当 errors 达到此容量时,新增错误会淘汰最早错误(FIFO)。 设置为 0 表示无限制(不推荐,可能导致内存泄漏)。
Sourcepub fn add_observer(&self, observer: Box<dyn Observer>)
pub fn add_observer(&self, observer: Box<dyn Observer>)
注册 Observer
接收 Box<dyn Observer>(向后兼容),内部转 Arc<dyn Observer> 存储,
以便 dispatch 时可以 cheap clone 快照后释放读锁。
Sourcepub fn subscribe(&self, subscriber: Box<dyn EventSubscriber>)
pub fn subscribe(&self, subscriber: Box<dyn EventSubscriber>)
注册 EventSubscriber
接收 Box<dyn EventSubscriber>(向后兼容),内部转 Arc<dyn EventSubscriber> 存储。
Sourcepub fn observer_count(&self) -> usize
pub fn observer_count(&self) -> usize
返回已注册的 Observer 数量
Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
返回已注册的 EventSubscriber 数量
Sourcepub fn drain_errors(&self) -> Vec<SubscriberError>
pub fn drain_errors(&self) -> Vec<SubscriberError>
取出收集到的非致命错误(清空内部缓冲)
Sourcepub fn error_count(&self) -> usize
pub fn error_count(&self) -> usize
返回当前错误缓冲区中的错误数量
Sourcepub fn dispatch(
&self,
event: Event,
ctx: &HookContext,
attrs: &HashMap<String, Value>,
)
pub fn dispatch( &self, event: Event, ctx: &HookContext, attrs: &HashMap<String, Value>, )
分发事件(after_* 事件,attrs 不可变)
错误仅记录,不影响后续订阅者执行。
§实现要点
- v0.2.1 修复 Critical C-3:调用用户回调前先 clone
Vec<Arc<...>>快照 并释放读锁,避免持读锁调用用户代码(防止死锁) - 错误先收集到本地
Vec,循环结束后一次性批量写入self.errors
Sourcepub fn dispatch_before_mut(
&self,
event: Event,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()>
pub fn dispatch_before_mut( &self, event: Event, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> SubscriberResult<()>
分发 before 事件(attrs 可变)
任何订阅者返回 Err(Vetoed) 会立即中止并返回错误。
§实现要点
- Vetoed 时直接返回该错误,不会再次调用
on_event(避免订阅者副作用翻倍) - v0.2.1 修复 Critical C-3:调用用户回调前先 clone 快照并释放读锁
- 错误先收集到本地
Vec,避免持读锁时获取写锁造成死锁
Sourcepub fn dispatch_after_find(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()>
pub fn dispatch_after_find( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> SubscriberResult<()>
分发 after_find 事件(attrs 可变,用于修改读出的数据)
§实现要点
- v0.2.1 修复 Critical C-3:调用用户回调前先 clone 快照并释放读锁
- 错误先收集到本地
Vec,避免持读锁时获取写锁造成死锁
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for EventDispatcher
impl !RefUnwindSafe for EventDispatcher
impl !UnwindSafe for EventDispatcher
impl Send for EventDispatcher
impl Sync for EventDispatcher
impl Unpin for EventDispatcher
impl UnsafeUnpin for EventDispatcher
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> 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