diff --git a/application-vanilla/src/commonTest/kotlin/com/fraktalio/fmodel/application/StateStoredAggregateTest.kt b/application-vanilla/src/commonTest/kotlin/com/fraktalio/fmodel/application/StateStoredAggregateTest.kt index 95f638b..3c7ac41 100644 --- a/application-vanilla/src/commonTest/kotlin/com/fraktalio/fmodel/application/StateStoredAggregateTest.kt +++ b/application-vanilla/src/commonTest/kotlin/com/fraktalio/fmodel/application/StateStoredAggregateTest.kt @@ -7,6 +7,7 @@ import com.fraktalio.fmodel.application.examples.numbers.even.command.evenNumber import com.fraktalio.fmodel.application.examples.numbers.even.command.evenNumberStateRepository import com.fraktalio.fmodel.application.examples.numbers.numberStateRepository import com.fraktalio.fmodel.domain.IDecider +import com.fraktalio.fmodel.domain.ISaga import com.fraktalio.fmodel.domain.combine import com.fraktalio.fmodel.domain.examples.numbers.api.Description import com.fraktalio.fmodel.domain.examples.numbers.api.EvenNumberState @@ -14,6 +15,7 @@ import com.fraktalio.fmodel.domain.examples.numbers.api.NumberCommand.EvenNumber import com.fraktalio.fmodel.domain.examples.numbers.api.NumberValue import com.fraktalio.fmodel.domain.examples.numbers.api.OddNumberState import com.fraktalio.fmodel.domain.examples.numbers.even.command.evenNumberDecider +import com.fraktalio.fmodel.domain.examples.numbers.numberSaga import com.fraktalio.fmodel.domain.examples.numbers.odd.command.oddNumberDecider import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec @@ -38,6 +40,16 @@ private suspend fun IDecider.given( stateRepository = repository ).handleOptimistically(command()) +private suspend fun IDecider.given( + repository: StateRepository, + saga: ISaga, + command: () -> C +) = StateStoredOrchestratingAggregate( + decider = this, + saga = saga, + stateRepository = repository +).handle(command()) + /** * DSL - When */ @@ -61,6 +73,7 @@ class StateStoredAggregateTest : FunSpec({ val evenNumberStateRepository = evenNumberStateRepository() as EvenNumberStateRepository val evenNumberLockingStateRepository = evenNumberLockingStateRepository() as EvenNumberLockingStateRepository val numberStateRepository = numberStateRepository() as NumberStateRepository + val numberSaga = numberSaga() test("State-stored aggregate - add even number") { with(evenDecider) { @@ -105,4 +118,16 @@ class StateStoredAggregateTest : FunSpec({ ) } } + + test("Orchestrated state-stored aggregate - add even and odd number") { + with(combinedDecider) { + numberStateRepository.deleteAll() + given(numberStateRepository, numberSaga) { + whenCommand(AddEvenNumber(Description("4"), NumberValue(4))) + } thenState Pair( + EvenNumberState(Description("4"), NumberValue(4)), + OddNumberState(Description("3"), NumberValue(3)) + ) + } + } }) diff --git a/application/src/commonMain/kotlin/com/fraktalio/fmodel/application/StateStoredAggregate.kt b/application/src/commonMain/kotlin/com/fraktalio/fmodel/application/StateStoredAggregate.kt index 8a89a6c..d7892e8 100644 --- a/application/src/commonMain/kotlin/com/fraktalio/fmodel/application/StateStoredAggregate.kt +++ b/application/src/commonMain/kotlin/com/fraktalio/fmodel/application/StateStoredAggregate.kt @@ -97,8 +97,10 @@ interface StateOrchestratingComputation : ISaga, StateComputation override suspend fun S?.computeNewState(command: C): S { val currentState = this ?: initialState val events = decide(command, currentState) - val newState = events.fold(currentState) { s, e -> evolve(s, e) } - events.flatMapConcat { react(it) }.onEach { newState.computeNewState(it) }.collect() + var newState = events.fold(currentState) { s, e -> evolve(s, e) } + events.flatMapConcat { react(it) }.collect { + newState = newState.computeNewState(it) + } return newState } }