Skip to content

Commit 364f617

Browse files
committed
#3 Use of Java Stream is strange
Fixing the issue where the new state was not properly updated recursively.
1 parent 17008ed commit 364f617

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
<name>fmodel-java</name>
1010
<description>fmodel-java</description>
1111
<properties>
12-
<java.version>21</java.version>
12+
<java.version>23</java.version>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14-
<maven.compiler.source>21</maven.compiler.source>
15-
<maven.compiler.target>21</maven.compiler.target>
14+
<maven.compiler.source>23</maven.compiler.source>
15+
<maven.compiler.target>23</maven.compiler.target>
1616
</properties>
1717
<dependencies>
1818
<dependency>
@@ -28,8 +28,8 @@
2828
<groupId>org.apache.maven.plugins</groupId>
2929
<artifactId>maven-compiler-plugin</artifactId>
3030
<configuration>
31-
<source>21</source>
32-
<target>21</target>
31+
<source>23</source>
32+
<target>23</target>
3333
</configuration>
3434
</plugin>
3535
</plugins>

src/main/java/com/fraktalio/fmodel/application/aggregate/statestored/StateStoredLockingOrchestratingAggregate.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ public Pair<S, V> handle(C command) {
8585
private S computeNewState(S state, C command) {
8686
var currentState = state != null ? state : initialState().get();
8787
var events = decide().apply(command, currentState);
88-
var newState = events.sequential().reduce(currentState, (s, e) -> evolve().apply(s, e), (s, s2) -> s);
89-
events.flatMap(it -> react().apply(it)).forEach(it -> computeNewState(newState, it));
90-
return newState;
88+
89+
return events.sequential()
90+
.reduce(currentState, (s, e) -> {
91+
var evolved = evolve().apply(s, e);
92+
return react().apply(e).sequential()
93+
.reduce(evolved, this::computeNewState, (s1, s2) -> s1);
94+
}, (s1, s2) -> s1);
9195
}
9296

9397
}

src/main/java/com/fraktalio/fmodel/application/aggregate/statestored/StateStoredOrchestratingAggregate.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ public S handle(C command) {
7676
private S computeNewState(S state, C command) {
7777
var currentState = state != null ? state : initialState().get();
7878
var events = decide().apply(command, currentState);
79-
var newState = events.sequential().reduce(currentState, (s, e) -> evolve().apply(s, e), (s, s2) -> s);
80-
events.flatMap(it -> react().apply(it)).forEach(it -> computeNewState(newState, it));
81-
return newState;
79+
80+
return events.sequential()
81+
.reduce(currentState, (s, e) -> {
82+
var evolved = evolve().apply(s, e);
83+
return react().apply(e).sequential()
84+
.reduce(evolved, this::computeNewState, (s1, s2) -> s1);
85+
}, (s1, s2) -> s1);
8286
}
8387

8488
}

0 commit comments

Comments
 (0)