Skip to main content

ProjectionRuntime

Struct ProjectionRuntime 

Source
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>

Source

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).

Source

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.

Source

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>, _>.

Source

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.

Source

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.

Source

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).

Auto Trait Implementations§

§

impl<P> !RefUnwindSafe for ProjectionRuntime<P>

§

impl<P> !UnwindSafe for ProjectionRuntime<P>

§

impl<P> Freeze for ProjectionRuntime<P>
where P: Freeze,

§

impl<P> Send for ProjectionRuntime<P>

§

impl<P> Sync for ProjectionRuntime<P>

§

impl<P> Unpin for ProjectionRuntime<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for ProjectionRuntime<P>
where P: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more