pub struct ProjectionRuntime<P: Projection> { /* private fields */ }Expand description
Live read-model runtime owning the DB connection, the broadcaster handle, the projection impl, and the per-key Mutex registry (D-13).
Implementations§
Source§impl<P: Projection> ProjectionRuntime<P>
impl<P: Projection> ProjectionRuntime<P>
Sourcepub fn new(
db: DatabaseConnection,
broadcaster: Arc<Broadcaster>,
projection: P,
) -> Self
pub fn new( db: DatabaseConnection, broadcaster: Arc<Broadcaster>, projection: P, ) -> Self
Construct a new runtime. Consumers typically wrap in Arc for
sharing across tokio tasks (and register requires Arc<Self>
to wire into the global dispatcher).
Sourcepub async fn read(
&self,
key: &ProjectionKey,
) -> Result<Option<P::State>, ProjectionError>
pub async fn read( &self, key: &ProjectionKey, ) -> Result<Option<P::State>, ProjectionError>
Read the persisted snapshot for key. Returns Ok(None) if no
row exists. Does NOT take the per-key Mutex (D-33) — readers
see either the pre- or post-upsert state, no torn reads.
Sourcepub async fn read_required(
&self,
key: &ProjectionKey,
) -> Result<P::State, ProjectionError>
pub async fn read_required( &self, key: &ProjectionKey, ) -> Result<P::State, ProjectionError>
Read with a hard error if the snapshot is absent (D-30). Wraps
read; consumers wanting Result<State, _> use this instead
of Result<Option<State>, _>.
Sourcepub async fn apply_event(&self, event: &P::Event) -> Result<(), ProjectionError>
pub async fn apply_event(&self, event: &P::Event) -> Result<(), ProjectionError>
Apply a single event. Implements the 7-step D-19 sequence
inside a per-key tokio::sync::Mutex. Cross-key applies run
in parallel; same-key applies serialize.
Sourcepub fn register(self: Arc<Self>)
pub fn register(self: Arc<Self>)
Register a ProjectionListener<P> into the global event
dispatcher (D-15). Killer-feature one-line wiring; every
P::Event::dispatch().await now flows through this projection.
Not idempotent on Arc identity (D-36). Calling register
twice on the same Arc<Self> registers TWO listeners; both
fire on each dispatch (same semantic as Laravel’s
Event::listen). Register once at app startup.
Sourcepub async fn rebuild<I>(
&self,
key: &ProjectionKey,
events: I,
) -> Result<P::State, ProjectionError>where
I: IntoIterator<Item = P::Event>,
pub async fn rebuild<I>(
&self,
key: &ProjectionKey,
events: I,
) -> Result<P::State, ProjectionError>where
I: IntoIterator<Item = P::Event>,
Discard the persisted snapshot for key, fold the supplied event
sequence through P::State::default() via P::apply, persist
the final state, broadcast ONE "rebuild" frame, and return
the rebuilt state (D-17).
Acquires the same per-key Mutex as apply_event, so rebuild
serializes against in-flight applies.
Not transactional (D-44). DELETE + folded INSERT under the
per-key Mutex; if the process crashes mid-rebuild, the snapshot
is gone but a subsequent apply_event re-initializes from
Default.
Empty iterator wipes the snapshot row (D-43): returns
P::State::default() with no insert and no broadcast.
The broadcast event name is "rebuild" (overrides
P::broadcast_event_name); payload is the final state. Clients
reset their local state on receipt of a "rebuild" frame
(D-41).