Skip to main content

EventSubscriber

Trait EventSubscriber 

Source
pub trait EventSubscriber: Send + Sync {
    // Required methods
    fn subscribed_events(&self) -> Vec<Event>;
    fn on_event(
        &self,
        event: Event,
        ctx: &HookContext,
        attrs: &HashMap<String, Value>,
    ) -> SubscriberResult<()>;

    // Provided method
    fn name(&self) -> &str { ... }
}
Expand description

事件订阅者 trait

Observer 不同,EventSubscriber 只接收订阅的特定事件。 适合只关心特定事件的场景(如缓存失效仅关心 UPDATE/DELETE)。

Required Methods§

Source

fn subscribed_events(&self) -> Vec<Event>

返回订阅的事件列表

仅当事件在此列表中时,on_event 才会被调用。

Source

fn on_event( &self, event: Event, ctx: &HookContext, attrs: &HashMap<String, Value>, ) -> SubscriberResult<()>

事件回调

§参数
  • event:触发的事件
  • ctx:钩子上下文
  • attrs:当前属性(before 事件可修改)
§返回
  • Ok(()):继续执行后续订阅者
  • Err(SubscriberError::Vetoed):中止 before 事件的后续执行
  • Err(SubscriberError::Failed):记录错误,继续执行后续订阅者

Provided Methods§

Source

fn name(&self) -> &str

订阅者名称

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§