pub struct WalRouter { /* private fields */ }Expand description
Routes typed WAL change events to per-table async handlers.
Register handlers with on_insert,
on_update, on_delete, and an
optional on_default fallback, then drive an
EventStream with run. Handlers are async and
Send + 'static; capture shared state by cloning an Arc into the closure.
§Error behavior
The typed handlers deserialize the row before invoking your closure. A
deserialization failure (schema mismatch, malformed data) — or any Err
your handler returns — propagates out of run and terminates
the stream. There is no per-event skip. If you need to log-and-continue,
deserialize leniently inside an on_default handler (or
drive the stream yourself with
EventStream::for_each_event /
next_event() and match event types by hand), returning Ok(()) to skip.
Implementations§
Source§impl WalRouter
impl WalRouter
Sourcepub fn on_insert<T, F, Fut>(
&mut self,
table: impl Into<Arc<str>>,
f: F,
) -> &mut Self
pub fn on_insert<T, F, Fut>( &mut self, table: impl Into<Arc<str>>, f: F, ) -> &mut Self
Register an async handler for INSERT events on table, deserialized into T.
Sourcepub fn on_update<T, F, Fut>(
&mut self,
table: impl Into<Arc<str>>,
f: F,
) -> &mut Self
pub fn on_update<T, F, Fut>( &mut self, table: impl Into<Arc<str>>, f: F, ) -> &mut Self
Register an async handler for UPDATE events on table. The closure receives
(old, new) where old is Some per the table’s replica identity.
Sourcepub fn on_delete<T, F, Fut>(
&mut self,
table: impl Into<Arc<str>>,
f: F,
) -> &mut Self
pub fn on_delete<T, F, Fut>( &mut self, table: impl Into<Arc<str>>, f: F, ) -> &mut Self
Register an async handler for DELETE events on table, deserialized into T.
Sourcepub fn on_default<F, Fut>(&mut self, f: F) -> &mut Self
pub fn on_default<F, Fut>(&mut self, f: F) -> &mut Self
Set the fallback handler for events with no registered (table, kind)
handler (including non-DML events like Begin/Commit/Relation).
The event is passed by reference — the router performs no clone, so a
firehose of unhandled events costs nothing here. If your handler needs to
own the event across an .await, clone it explicitly inside the closure
(extract what you need synchronously first, otherwise the returned future
would borrow the event and fail to satisfy the 'static bound).
Sourcepub async fn run(&mut self, es: &mut EventStream) -> Result<()>
pub async fn run(&mut self, es: &mut EventStream) -> Result<()>
Drive an EventStream: dispatch each event, auto-advance applied LSN
after each Ok, and exit gracefully on cancellation.
Sourcepub fn on_insert_of<T, Fut>(
&mut self,
f: impl FnMut(T) -> Fut + Send + 'static,
) -> &mut Self
pub fn on_insert_of<T, Fut>( &mut self, f: impl FnMut(T) -> Fut + Send + 'static, ) -> &mut Self
Register an INSERT handler for T, inferring the table from
WalTable::TABLE. Level-A convenience
over on_insert — the closure captures its own state.
The closure is an impl argument (not a named generic), so a turbofish only needs the row type and the future: on_insert_of::<User, _>(...), or annotate the closure param and omit it: on_insert_of(|u: User| ...).
Sourcepub fn on_update_of<T, Fut>(
&mut self,
f: impl FnMut(Option<T>, T) -> Fut + Send + 'static,
) -> &mut Self
pub fn on_update_of<T, Fut>( &mut self, f: impl FnMut(Option<T>, T) -> Fut + Send + 'static, ) -> &mut Self
Register an UPDATE handler for T, inferring the table from T::TABLE.
Sourcepub fn on_delete_of<T, Fut>(
&mut self,
f: impl FnMut(T) -> Fut + Send + 'static,
) -> &mut Self
pub fn on_delete_of<T, Fut>( &mut self, f: impl FnMut(T) -> Fut + Send + 'static, ) -> &mut Self
Register a DELETE handler for T, inferring the table from T::TABLE.