public virtual IDisposable Subscribe(ISubscriber subscriber, string cursor, Func <MessageResult, object, Task <bool> > callback, int maxMessages, object state) { if (subscriber == null) { throw new ArgumentNullException("subscriber"); } if (callback == null) { throw new ArgumentNullException("callback"); } Subscription subscription = CreateSubscription(subscriber, cursor, callback, maxMessages, state); // Set the subscription for this subscriber subscriber.Subscription = subscription; var topics = new HashSet <Topic>(); foreach (var key in subscriber.EventKeys) { // Create or retrieve topic and set it as HasSubscriptions Topic topic = SubscribeTopic(key); // Set the subscription for this topic subscription.SetEventTopic(key, topic); topics.Add(topic); } subscriber.EventKeyAdded += _addEvent; subscriber.EventKeyRemoved += _removeEvent; subscriber.WriteCursor = subscription.WriteCursor; var subscriptionState = new SubscriptionState(subscriber); var disposable = new DisposableAction(_disposeSubscription, subscriptionState); // When the subscription itself is disposed then dispose it subscription.Disposable = disposable; // Add the subscription when it's all set and can be scheduled // for work. It's important to do this after everything is wired up for the // subscription so that publishes can schedule work at the right time. foreach (var topic in topics) { topic.AddSubscription(subscription); } subscriptionState.Initialized.Set(); // If there's a cursor then schedule work for this subscription if (!String.IsNullOrEmpty(cursor)) { _broker.Schedule(subscription); } return(disposable); }
private void AddEvent(ISubscriber subscriber, string eventKey) { Topic topic = SubscribeTopic(eventKey); // Add or update the cursor (in case it already exists) if (subscriber.Subscription.AddEvent(eventKey, topic)) { // Add it to the list of subs topic.AddSubscription(subscriber.Subscription); } }