pub trait Observer: Send + Sync {
// Provided methods
fn name(&self) -> &str { ... }
fn before_insert(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()> { ... }
fn after_insert(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()> { ... }
fn before_update(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()> { ... }
fn after_update(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()> { ... }
fn before_delete(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()> { ... }
fn after_delete(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()> { ... }
fn after_find(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()> { ... }
}Expand description
模型观察者 trait
与 EventSubscriber 不同,Observer 默认订阅所有事件。
适合需要监控所有生命周期事件的场景(如审计日志)。
§实现要点
- 所有方法默认实现为 no-op,按需 override
- 任何方法返回
Err(SubscriberError::Vetoed)会中止 before 事件的后续执行
Provided Methods§
Sourcefn before_insert(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()>
fn before_insert( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> SubscriberResult<()>
插入前
Sourcefn after_insert(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()>
fn after_insert( &self, _ctx: &HookContext, _attrs: &HashMap<String, Value>, ) -> SubscriberResult<()>
插入后
Sourcefn before_update(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()>
fn before_update( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> SubscriberResult<()>
更新前
Sourcefn after_update(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()>
fn after_update( &self, _ctx: &HookContext, _attrs: &HashMap<String, Value>, ) -> SubscriberResult<()>
更新后
Sourcefn before_delete(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()>
fn before_delete( &self, _ctx: &HookContext, _attrs: &HashMap<String, Value>, ) -> SubscriberResult<()>
删除前
Sourcefn after_delete(
&self,
_ctx: &HookContext,
_attrs: &HashMap<String, Value>,
) -> SubscriberResult<()>
fn after_delete( &self, _ctx: &HookContext, _attrs: &HashMap<String, Value>, ) -> SubscriberResult<()>
删除后
Sourcefn after_find(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> SubscriberResult<()>
fn after_find( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> SubscriberResult<()>
单行查询后
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".