Struct esrs::AggregateState
source · pub struct AggregateState<S> { /* private fields */ }
Expand description
The internal state for an Aggregate. It contains:
- an id uniquely representing the aggregate,
- an incremental sequence number,
- a lock representing the atomicity of the access to the aggregate,
- a state defined by the user of this library.
Implementations§
source§impl<S: Default> AggregateState<S>
impl<S: Default> AggregateState<S>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new instance of an AggregateState
with a new unique id.
sourcepub fn with_id(id: impl Into<Uuid>) -> Self
pub fn with_id(id: impl Into<Uuid>) -> Self
Creates a new instance of an AggregateState
with the given aggregate id.
This should be used almost exclusively when loading by aggregate id yields nothing, and this becomes the brand new aggregate state for that id.
Other uses are strongly discouraged, since the id could be already existing and a clash might happen when persisting events.
sourcepub fn apply_store_events<T, F>(
self,
store_events: Vec<StoreEvent<T>>,
apply_event: F
) -> Selfwhere
F: Fn(S, T) -> S,
pub fn apply_store_events<T, F>(
self,
store_events: Vec<StoreEvent<T>>,
apply_event: F
) -> Selfwhere
F: Fn(S, T) -> S,
Consumes the aggregate state and generates a new one with the events applied to it,
as dictated by apply_event
.
sourcepub fn into_inner(self) -> S
pub fn into_inner(self) -> S
Consumes self and extracts the internal state.
sourcepub const fn sequence_number(&self) -> &SequenceNumber
pub const fn sequence_number(&self) -> &SequenceNumber
Returns the internal sequence number.
sourcepub fn next_sequence_number(&mut self) -> SequenceNumber
pub fn next_sequence_number(&mut self) -> SequenceNumber
Computes the internal sequence number incremented by 1.
sourcepub fn set_lock(&mut self, guard: EventStoreLockGuard)
pub fn set_lock(&mut self, guard: EventStoreLockGuard)
Inserts the lock guard into self, replacing any current one.
sourcepub fn take_lock(&mut self) -> Option<EventStoreLockGuard>
pub fn take_lock(&mut self) -> Option<EventStoreLockGuard>
Extracts the lock from self, leaving nothing behind.
Trait Implementations§
source§impl<S: Debug> Debug for AggregateState<S>
impl<S: Debug> Debug for AggregateState<S>
source§impl<S: Default> Default for AggregateState<S>
impl<S: Default> Default for AggregateState<S>
Default implementation for AggregateState