client-dotnet/src/INetMock.Client/Audit/Serialization/EventServerStreamReader.cs

32 lines
962 B
C#
Raw Normal View History

using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
2021-02-21 16:23:48 +00:00
using INetMock.Client.Rpc;
namespace INetMock.Client.Audit.Serialization
{
public sealed class EventServerStreamReader : IProtoEventReader
{
2021-02-21 16:23:48 +00:00
private readonly AsyncServerStreamingCall<WatchEventsResponse> _asyncEventStream;
2021-02-21 16:23:48 +00:00
public EventServerStreamReader(AsyncServerStreamingCall<WatchEventsResponse> asyncEventStream)
{
_asyncEventStream = asyncEventStream;
}
public async Task<EventEntity?> ReadAsync(CancellationToken token = default)
{
if (!await _asyncEventStream.ResponseStream.MoveNext(token)) return null;
2021-02-21 16:23:48 +00:00
return _asyncEventStream.ResponseStream.Current.Entity;
}
public void Dispose() => _asyncEventStream.Dispose();
public ValueTask DisposeAsync()
{
_asyncEventStream.Dispose();
return ValueTask.CompletedTask;
}
}
}