public static async Task ConsoleLogAllEvents()
        {
            await _client.SubscribeToAllAsync(Position.End, async (subscription, evnt, cancellationToken) =>
            {
                Console.WriteLine($"{Environment.NewLine}{evnt.Event.EventType} appended{Environment.NewLine}");

                await Task.CompletedTask;
            }, filterOptions : new SubscriptionFilterOptions(EventTypeFilter.RegularExpression("Event")));
        }
        public static async Task WhenFruitAdded()
        {
            await _client.SubscribeToAllAsync(Position.End,
                                              async (subscription, evnt, cancellationToken) =>
            {
                try
                {
                    dynamic e = evnt.Event.EventType switch
                    {
                        "AppleAddedEvent" => JsonConvert.DeserializeObject <AppleAddedEvent>(Encoding.UTF8.GetString(evnt.Event.Data.Span)),
                        "PearAddedEvent" => JsonConvert.DeserializeObject <PearAddedEvent>(Encoding.UTF8.GetString(evnt.Event.Data.Span)),
                        _ => null
                    };

                    Console.WriteLine($"{evnt.Event.EventNumber}");

                    await new SummonFruitCommandHandler().Handle(e.Id, e.Weight, e.FruitCondition, TypeOfFruit.Apple);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error in subscr. " + e.Message);
                }
            }, resolveLinkTos : true, filterOptions : new SubscriptionFilterOptions(EventTypeFilter.RegularExpression("AppleAddedEvent|PearAddedEvent")));
        }
        public async Task regular_expression_event_type()
        {
            const string eventTypePrefix = nameof(regular_expression_event_type);
            var          streamPrefix    = _fixture.GetStreamName();
            var          events          = _fixture.CreateTestEvents(10)
                                           .Select(e =>
                                                   new EventData(e.EventId, $"{eventTypePrefix}-{Guid.NewGuid():n}", e.Data, e.Metadata, e.ContentType))
                                           .ToArray();

            foreach (var e in events)
            {
                await _fixture.Client.AppendToStreamAsync($"{streamPrefix}_{Guid.NewGuid():n}",
                                                          AnyStreamRevision.NoStream, new[] { e });
            }

            var result = await _fixture.Client.ReadAllAsync(Direction.Backwards, Position.End, 20,
                                                            filterOptions : new FilterOptions(EventTypeFilter.RegularExpression(new Regex($"^{eventTypePrefix}"))))
                         .ToArrayAsync();

            Assert.Equal(events.Select(x => x.EventId), result.Reverse().Select(x => x.OriginalEvent.EventId));
        }
Example #4
0
 public async Task SubscribeAll_EventFilterExpression()
 {
     var prefixEventFilter = new SubscriptionFilterOptions(EventTypeFilter.RegularExpression("^test"));
     await _client.SubscribeToAllAsync(SubscribeReturn,
                                       filterOptions : prefixEventFilter);
 }