diff --git a/src/saga.rs b/src/saga.rs index dfd9155..0ec2810 100644 --- a/src/saga.rs +++ b/src/saga.rs @@ -115,3 +115,16 @@ impl<'a, AR, A> Saga<'a, AR, A> { Saga { react: new_react } } } + +/// Formalizes the `Action Computation` algorithm for the `saga` to handle events/action_results, and produce new commands/actions. +pub trait ActionComputation { + /// Computes new commands/actions based on the event/action_result. + fn compute_new_actions(&self, event: &AR) -> Vec; +} + +impl<'a, AR, A> ActionComputation for Saga<'a, AR, A> { + /// Computes new commands/actions based on the event/action_result. + fn compute_new_actions(&self, event: &AR) -> Vec { + (self.react)(event).into_iter().collect() + } +} diff --git a/src/saga_manager.rs b/src/saga_manager.rs index 6a8498c..cbd0e7b 100644 --- a/src/saga_manager.rs +++ b/src/saga_manager.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; use async_trait::async_trait; -use crate::saga::Saga; +use crate::saga::ActionComputation; /// Publishes the action/command to some external system. /// @@ -26,21 +26,23 @@ pub trait ActionPublisher { /// - `AR` - Action Result / Event /// - `Publisher` - Action Publisher /// - `Error` - Error -pub struct SagaManager<'a, A, AR, Publisher, Error> +pub struct SagaManager where Publisher: ActionPublisher, + Saga: ActionComputation, { action_publisher: Publisher, - saga: Saga<'a, AR, A>, + saga: Saga, _marker: PhantomData<(A, AR, Error)>, } -impl<'a, A, AR, Publisher, Error> SagaManager<'a, A, AR, Publisher, Error> +impl SagaManager where Publisher: ActionPublisher, + Saga: ActionComputation, { /// Creates a new instance of [SagaManager]. - pub fn new(action_publisher: Publisher, saga: Saga<'a, AR, A>) -> Self { + pub fn new(action_publisher: Publisher, saga: Saga) -> Self { SagaManager { action_publisher, saga, @@ -49,7 +51,7 @@ where } /// Handles the action result by publishing it to the external system. pub async fn handle(&self, action_result: &AR) -> Result, Error> { - let new_actions = (self.saga.react)(action_result); + let new_actions = self.saga.compute_new_actions(action_result); let published_actions = self.action_publisher.publish(&new_actions).await?; Ok(published_actions) }