7
7
namespace Eventuous . Tests . Fakes {
8
8
public class InMemoryEventStore : IEventStore {
9
9
readonly Dictionary < string , List < StreamEvent > > _storage = new ( ) ;
10
+ readonly List < StreamEvent > _global = new ( ) ;
10
11
11
- public Task AppendEvents (
12
+ public Task < AppendEventsResult > AppendEvents (
12
13
string stream ,
13
14
ExpectedStreamVersion expectedVersion ,
14
15
IReadOnlyCollection < StreamEvent > events ,
15
16
CancellationToken cancellationToken
16
17
) {
17
- return _storage . TryGetValue ( stream , out var existing ) ? AddToExisting ( ) : AddToNew ( ) ;
18
+ if ( _storage . TryGetValue ( stream , out var existing ) ) AddToExisting ( ) ;
19
+ else AddToNew ( ) ;
18
20
19
- Task AddToExisting ( ) {
21
+ _global . AddRange ( events ) ;
22
+
23
+ return Task . FromResult ( new AppendEventsResult ( ( ulong ) ( _global . Count - 1 ) , _storage [ stream ] . Count ) ) ;
24
+
25
+ void AddToExisting ( ) {
20
26
if ( existing . Count >= expectedVersion . Value )
21
27
throw new WrongVersion ( expectedVersion , existing . Count - 1 ) ;
22
28
23
29
existing . AddRange ( events ) ;
24
- return Task . CompletedTask ;
25
30
}
26
31
27
- Task AddToNew ( ) {
28
- _storage [ stream ] = events . ToList ( ) ;
29
- return Task . CompletedTask ;
30
- }
32
+ void AddToNew ( ) => _storage [ stream ] = events . ToList ( ) ;
31
33
}
32
34
33
- public Task < StreamEvent [ ] > ReadEvents ( string stream , StreamReadPosition start , int count , CancellationToken cancellationToken )
35
+ public Task < StreamEvent [ ] > ReadEvents (
36
+ string stream ,
37
+ StreamReadPosition start ,
38
+ int count ,
39
+ CancellationToken cancellationToken
40
+ )
34
41
=> Task . FromResult ( FindStream ( stream ) . Take ( count ) . ToArray ( ) ) ;
35
42
36
43
public Task < StreamEvent [ ] > ReadEventsBackwards ( string stream , int count , CancellationToken cancellationToken ) {
@@ -40,7 +47,12 @@ public Task<StreamEvent[]> ReadEventsBackwards(string stream, int count, Cancell
40
47
return Task . FromResult ( reversed . Take ( count ) . ToArray ( ) ) ;
41
48
}
42
49
43
- public Task ReadStream ( string stream , StreamReadPosition start , Action < StreamEvent > callback , CancellationToken cancellationToken ) {
50
+ public Task ReadStream (
51
+ string stream ,
52
+ StreamReadPosition start ,
53
+ Action < StreamEvent > callback ,
54
+ CancellationToken cancellationToken
55
+ ) {
44
56
foreach ( var streamEvent in FindStream ( stream ) ) {
45
57
callback ( streamEvent ) ;
46
58
}
0 commit comments