public async Task <ISubscription> Subscribe(IEventHandlerFactory factory, object[] args = null)
        {
            Subscription s = new Subscription(true);

            var lastPosition = (StreamPosition)await _stream.LastPosition();

            string[]      prefixes = _events.Select(x => x.Name).ToArray();
            FilterOptions filters  = new FilterOptions(EventTypeFilter.Prefix(prefixes));

            // be very careful. We need to subscribe after the global position.

            await _eventStore.SubscribeToAllAsync(lastPosition.GlobalPosition,
                                                  async (s, r, c) =>
            {
                var type = _schema.EventByName(r.Event.EventType);
                if (type != null && _events.Contains(type))
                {
                    using (var scope = factory.Scope())
                    {
                        var handler = scope.CreateHandler(type);

                        var(m, e) = _eventConverter.Convert(type, r);

                        await handler.Execute(m, e);
                    }
                }
            }, true, null, filters);

            return(s);
        }
Exemplo n.º 2
0
        public async Task SubscribeToStreams(IStreamPosition streamPosition, params SubscriptionInfo[] eventTypes)
        {
            var           methods       = new Dictionary <string, Func <IStreamSubscription, ResolvedEvent, CancellationToken, Task> >();
            var           prefixes      = eventTypes.Select(x => x.EventType.Name).ToArray();
            var           filter        = EventTypeFilter.Prefix(prefixes);
            FilterOptions filterOptions = new FilterOptions(filter);

            var esStreamPosition = ((StreamPosition)(streamPosition)).GlobalPosition;

            var handlers = eventTypes.ToDictionary(x => x.EventType.Name, x => x.HandlerType);
            var dict     = eventTypes.Select(x => x.EventType).ToDictionary(x => x.Name);
            await _connection.SubscribeToAllAsync(esStreamPosition, async (s, r, c) =>
            {
                await OnEventAppearead(s, r, methods, handlers, dict, c);
            }, true, OnDropped, filterOptions);
        }