Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable layer commands to directly return results #2215

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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