-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathCommitStreamObserver.cs
More file actions
53 lines (48 loc) · 1.86 KB
/
Copy pathCommitStreamObserver.cs
File metadata and controls
53 lines (48 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Runtime.ExceptionServices;
namespace NEventStore
{
/// <summary>
/// Represents an async observer that can receive and stores commits from a stream.
/// Can be used as base class for other observers.
/// </summary>
public class CommitStreamObserver : IAsyncObserver<ICommit>
{
/// <summary>
/// The list of commits read from the stream.
/// </summary>
public IList<ICommit> Commits { get; } = [];
/// <summary>
/// Indicates if the read operation has completed.
/// </summary>
public bool ReadCompleted { get; private set; }
/// <summary>
/// Store the commits received from the stream in the <see cref="Commits"/> collection.
/// </summary>
public virtual Task<bool> OnNextAsync(ICommit value, CancellationToken cancellationToken)
{
Commits.Add(value);
return Task.FromResult(true);
}
/// <summary>
/// <para>Notifies the observer that the provider has experienced an error condition.</para>
/// <para>
/// Preserve the stack trace and rethrow the exception that occurred while reading commits from the stream.
/// </para>
/// <para>
/// Override this method to log and handle the error.
/// </para>
/// </summary>
public virtual Task OnErrorAsync(Exception ex, CancellationToken cancellationToken)
{
// Preserve the stack trace and rethrow the exception
ExceptionDispatchInfo.Capture(ex).Throw();
return Task.CompletedTask;
}
/// <inheritdoc/>
public virtual Task OnCompletedAsync(CancellationToken cancellationToken)
{
ReadCompleted = true;
return Task.CompletedTask;
}
}
}