Skip to content

Commit

Permalink
Merge pull request #2215 from hannobraun/layers
Browse files Browse the repository at this point in the history
Enable layer commands to directly return results
  • Loading branch information
hannobraun authored Feb 15, 2024
2 parents f5810a4 + 8222147 commit 0b3ac30
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
20 changes: 17 additions & 3 deletions crates/fj-core/src/layers/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ impl<S> Layer<S> {
///
/// The command is processed synchronously. When this method returns, the
/// state has been updated.
pub fn process<C>(&mut self, command: C, events: &mut Vec<C::Event>)
pub fn process<C>(
&mut self,
command: C,
events: &mut Vec<C::Event>,
) -> C::Result
where
C: Command<S>,
{
command.decide(&self.state, events);
let result = command.decide(&self.state, events);

for event in events {
event.evolve(&mut self.state);
}

result
}

/// Drop this instance, returning the wrapped state
Expand Down Expand Up @@ -63,6 +69,14 @@ where

/// A command that encodes a request to update a layer's state
pub trait Command<S> {
/// The direct result of processing a command that is returned to the caller
///
/// Changes to the state that result from a command are encoded as events
/// (see [`Command::Event`]). In addition to that, a command may return
/// information to the caller, and `Result` defines the type of that
/// information.
type Result;

/// An event that encodes a change to the state
///
/// Events are produced by [`Command::decide`] and processed by
Expand All @@ -73,7 +87,7 @@ pub trait Command<S> {
///
/// If the command must result in changes to the state, any number of events
/// that describe these state changes can be produced.
fn decide(self, state: &S, events: &mut Vec<Self::Event>);
fn decide(self, state: &S, events: &mut Vec<Self::Event>) -> Self::Result;
}

/// An event that encodes a change to a layer's state
Expand Down
1 change: 1 addition & 0 deletions crates/fj-core/src/layers/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct InsertObject {
}

impl Command<Objects> for InsertObject {
type Result = ();
type Event = InsertObject;

fn decide(self, _: &Objects, events: &mut Vec<Self::Event>) {
Expand Down
1 change: 1 addition & 0 deletions crates/fj-core/src/layers/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Layer<Validation> {
}

impl Command<Validation> for InsertObject {
type Result = ();
type Event = ValidationFailed;

fn decide(self, state: &Validation, events: &mut Vec<Self::Event>) {
Expand Down

0 comments on commit 0b3ac30

Please sign in to comment.