Skip to main content

EventDispatcher

Struct EventDispatcher 

Source
pub struct EventDispatcher { /* private fields */ }
Expand description

事件分发器

管理 ObserverEventSubscriber 的注册与分发。

§分发顺序

  1. 先按注册顺序调用所有 Observer 的对应方法
  2. 再按注册顺序调用所有订阅了该事件的 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

Source

pub fn new() -> Self

创建空的事件分发器

Source

pub fn with_max_errors(self, max_errors: usize) -> Self

设置错误缓冲区最大容量

当 errors 达到此容量时,新增错误会淘汰最早错误(FIFO)。 设置为 0 表示无限制(不推荐,可能导致内存泄漏)。

Source

pub fn add_observer(&self, observer: Box<dyn Observer>)

注册 Observer

接收 Box<dyn Observer>(向后兼容),内部转 Arc<dyn Observer> 存储, 以便 dispatch 时可以 cheap clone 快照后释放读锁。

Source

pub fn subscribe(&self, subscriber: Box<dyn EventSubscriber>)

注册 EventSubscriber

接收 Box<dyn EventSubscriber>(向后兼容),内部转 Arc<dyn EventSubscriber> 存储。

Source

pub fn clear(&self)

清空所有注册

Source

pub fn observer_count(&self) -> usize

返回已注册的 Observer 数量

Source

pub fn subscriber_count(&self) -> usize

返回已注册的 EventSubscriber 数量

Source

pub fn drain_errors(&self) -> Vec<SubscriberError>

取出收集到的非致命错误(清空内部缓冲)

Source

pub fn error_count(&self) -> usize

返回当前错误缓冲区中的错误数量

Source

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
Source

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,避免持读锁时获取写锁造成死锁
Source

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§

Source§

impl Default for EventDispatcher

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more