public async IAsyncEnumerable <Event> ReadAllEventsFromStreamAsync(string stream) { using var store = await _streamStoreProvider.GetStreamStore(); var pos = 0; var end = false; var streamId = new StreamId(stream); var page = await store.ReadStreamForwards(streamId.Id, 0, PAGE_SIZE); if (page.Status == PageReadStatus.StreamNotFound) { throw new StreamNotFoundException(stream); } while (!end) { //process page results var serializationTasks = new List <Task <Event> >(page.Messages.Length); foreach (var message in page.Messages) { serializationTasks.Add(_eventSerializer.DeseralizeEvent(message)); } await Task.WhenAll(serializationTasks); foreach (var task in serializationTasks) { yield return(task.Result); } //Check next page if (!page.IsEnd) { pos += PAGE_SIZE; page = await store.ReadStreamForwards(streamId.Id, pos, PAGE_SIZE); } else { end = true; } } yield break; }
public async IAsyncEnumerable <Event> ReadAllEventsFromStreamAsync(string stream) { var credentials = await _connectionProvider.GetCredentials(); long position = 0; var slice = default(StreamEventsSlice); do { slice = await _conn.ReadStreamEventsForwardAsync(stream, position, 100, false, credentials); switch (slice.Status) { case SliceReadStatus.StreamDeleted: throw new StreamDeletedException(stream); case SliceReadStatus.StreamNotFound: throw new StreamNotFoundException(stream); default: break; } foreach (var resolvedEvent in slice.Events) { if (resolvedEvent.OriginalStreamId[0] == '$') { continue; } var eventNumber = resolvedEvent.Event.EventNumber; position = eventNumber; var @event = _eventSerializer.DeseralizeEvent(resolvedEvent); yield return(@event); } if (!slice.IsEndOfStream) { position += 1; } }while (!slice.IsEndOfStream); yield break; }
private async Task HandleEvent(StreamMessage message, CancellationToken cancellation) { Func <Event> lazyEvent = new Func <Event>(() => { var deserializationTask = _eventSerializer.DeseralizeEvent(message); deserializationTask.Wait(); return(deserializationTask.Result); }); var streamMessage = new SubscriptionMessage(message.MessageId, message.StreamId, message.Type, lazyEvent); await RaiseMessage(streamMessage); }
private async Task HandleEvent(ResolvedEvent resolvedEvent) { Func <Event> lazyEvent = new Func <Event>(() => { var @event = _eventSerializer.DeseralizeEvent(resolvedEvent); @event.EventId = resolvedEvent.OriginalEvent.EventId; return(@event); }); var streamMessage = new SubscriptionMessage( resolvedEvent.OriginalEvent.EventId, resolvedEvent.Event.EventStreamId, resolvedEvent.Event.EventType, lazyEvent); await RaiseMessage(streamMessage); }