Skip to main content

Projection

Trait Projection 

Source
pub trait Projection: Send + Sync {
    // Required methods
    fn observes(&self) -> &[TypeCode];
    fn on_event(
        &mut self,
        event: &EventRecord,
        ctx: &ProjectionContext<'_>,
    ) -> Result<(), ProjectionError>;
    fn last_applied(&self) -> Option<(u64, Tick)>;

    // Provided method
    fn on_state_change(
        &mut self,
        _new_state: ObserverState,
    ) -> Result<(), ProjectionError> { ... }
}
Expand description

L2 projection worker. Each implementor owns a read-model view that is kept in sync with the L1 event stream for a specific set of TypeCodes.

Implementors must be Send + Sync so the ProjectionRouter can run across worker threads. Stream dedup / gap detection is centralised in the router’s own cursor; the router additionally consults Projection::last_applied to skip events a projection has already applied (catch-up after a router rebuild or a partially failed fan-out).

Required Methods§

Source

fn observes(&self) -> &[TypeCode]

TypeCodes this projection observes — the router filters incoming events against this slice.

Source

fn on_event( &mut self, event: &EventRecord, ctx: &ProjectionContext<'_>, ) -> Result<(), ProjectionError>

Apply an event. Called only after router-side dedup + gap checks have succeeded. Implementations must:

  1. Update their internal view state.
  2. Bump last_applied to the event’s (sequence, tick).
Source

fn last_applied(&self) -> Option<(u64, Tick)>

Last (sequence, tick) applied — None if the projection is fresh. The router skips any event at or before this position (tick-major order), so the on_event bump is what makes application at-most-once across redeliveries.

Provided Methods§

Source

fn on_state_change( &mut self, _new_state: ObserverState, ) -> Result<(), ProjectionError>

React to a worker-state transition (Passive ↔ Active ↔ Draining). Default is no-op.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§