public void Subscribe(IActorRef subscriber, string stream, long?from, int max,
                              Func <ResolvedEvent, object> resolved)
        {
            if (!_subscriptions.TryGetValue(subscriber, out var subscriptions))
            {
                subscriptions = new HashSet <EventStoreCatchUpSubscription>();
                _subscriptions.Add(subscriber, subscriptions);
                _context.WatchWith(subscriber, new Unsubscribe(stream, subscriber));
            }

            try
            {
// need to have this since ES Catchup Subscription needs null for from when user wants to
                // read from begging of the stream
                long?nullable = null;

                var self = _context.Self;

                var subscription = _conn.SubscribeToStreamFrom(
                    stream,
                    from.HasValue && from.Value == 0 ? nullable : from,
                    new CatchUpSubscriptionSettings(max * 2, 500, false, true),
                    (sub, @event) =>
                {
                    var p = resolved(@event);
                    if (p != null)
                    {
                        subscriber.Tell(p, self);
                    }
                },
                    _ => subscriber.Tell(CaughtUp.Instance, self),
                    (_, reason, exception) =>
                {
                    var msg = $"Subscription dropped due reason {reason.ToString()}";
                    subscriber.Tell(new SubscriptionDroppedException(msg, exception), self);
                });


                subscriptions.Add(subscription);
            }
            catch (Exception)
            {
                if (subscriptions.Count == 0)
                {
                    _context.Unwatch(subscriber);
                }

                throw;
            }
        }
Esempio n. 2
0
 public static TypedActorReference <TMessage> Unwatch <TMessage>(this IActorContext context, TypedActorReference <TMessage> typedReference)
 {
     context.Unwatch(typedReference.Ref);
     return(typedReference);
 }