Skip to content

Commit

Permalink
update the 'View' trait
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel.eades committed Jan 2, 2025
1 parent b44b0f9 commit 0d855c3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/persist/doc.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use crate::doc::MyAggregate;
use crate::doc::{MyAggregate, MyEvents};
use crate::persist::event_stream::ReplayStream;
use crate::persist::{
PersistedEventRepository, PersistenceError, SerializedEvent, SerializedSnapshot, ViewContext,
ViewRepository,
};
use crate::{Aggregate, EventEnvelope, View};
use crate::{Aggregate, View};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct MyView;

impl View<MyAggregate> for MyView {
fn update(&mut self, _event: &EventEnvelope<MyAggregate>) {
impl View for MyView {
type Event = MyEvents;
fn apply(&mut self, _event: &Self::Event) {

Check warning on line 17 in src/persist/doc.rs

View check run for this annotation

Codecov / codecov/patch

src/persist/doc.rs#L17

Added line #L17 was not covered by tests
todo!()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/persist/generic_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{Aggregate, EventEnvelope, Query, View};
pub struct GenericQuery<R, V, A>
where
R: ViewRepository<V, A>,
V: View<A>,
V: View<Event = A::Event>,
A: Aggregate,
{
view_repository: Arc<R>,
Expand All @@ -22,7 +22,7 @@ where
impl<R, V, A> GenericQuery<R, V, A>
where
R: ViewRepository<V, A>,
V: View<A>,
V: View<Event = A::Event>,
A: Aggregate,
{
/// Creates a new `GenericQuery` using the provided `ViewRepository`.
Expand Down Expand Up @@ -102,7 +102,7 @@ where
) -> Result<(), PersistenceError> {
let (mut view, view_context) = self.load_mut(view_id.to_string()).await?;
for event in events {
view.update(event);
view.apply(&event.payload);

Check warning on line 105 in src/persist/generic_query.rs

View check run for this annotation

Codecov / codecov/patch

src/persist/generic_query.rs#L105

Added line #L105 was not covered by tests
}
self.view_repository.update_view(view, view_context).await?;
Ok(())
Expand All @@ -119,7 +119,7 @@ where
impl<R, V, A> Query<A> for GenericQuery<R, V, A>
where
R: ViewRepository<V, A>,
V: View<A>,
V: View<Event = A::Event>,
A: Aggregate,
{
async fn dispatch(&self, view_id: &str, events: &[EventEnvelope<A>]) {
Expand Down
2 changes: 1 addition & 1 deletion src/persist/view_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use async_trait::async_trait;
#[async_trait]
pub trait ViewRepository<V, A>: Send + Sync
where
V: View<A>,
V: View<Event = A::Event>,
A: Aggregate,
{
/// Returns the current view instance.
Expand Down
6 changes: 4 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ pub trait Query<A: Aggregate>: Send + Sync {
/// A `View` represents a materialized view, generally serialized for persistence, that is updated by a query.
/// This a read element in a CQRS system.
///
pub trait View<A: Aggregate>: Debug + Default + Serialize + DeserializeOwned + Send + Sync {
pub trait View: Debug + Default + Serialize + DeserializeOwned + Send + Sync {
/// The event type that this view is constructed from
type Event;
/// Each implemented view is responsible for updating its state based on events passed via
/// this method.
fn update(&mut self, event: &EventEnvelope<A>);
fn apply(&mut self, event: &Self::Event);
}

0 comments on commit 0d855c3

Please sign in to comment.