Example #1
0
        private async Task <Guid> SubscribeAsync(Action <MapEventHandlers <TKey, TValue> > events, Maybe <TKey> key, IPredicate predicate, bool includeValues, object state, CancellationToken cancellationToken)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

            var handlers = new MapEventHandlers <TKey, TValue>();

            events(handlers);

            var flags = default(MapEventTypes);

            foreach (var handler in handlers)
            {
                flags |= handler.EventType;
            }

            // 0: no entryKey, no predicate
            // 1: entryKey, no predicate
            // 2: no entryKey, predicate
            // 3: entryKey, predicate
            var mode = key.Match(1, 0) + (predicate != null ? 2 : 0);
            var keyv = key.ValueOrDefault();

            var subscribeRequest = mode switch
            {
                0 => MapAddEntryListenerCodec.EncodeRequest(Name, includeValues, (int)flags, Cluster.IsSmartRouting),
                1 => MapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(keyv), includeValues, (int)flags, Cluster.IsSmartRouting),
                2 => MapAddEntryListenerWithPredicateCodec.EncodeRequest(Name, ToData(predicate), includeValues, (int)flags, Cluster.IsSmartRouting),
                3 => MapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(Name, ToData(keyv), ToData(predicate), includeValues, (int)flags, Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

            var subscription = new ClusterSubscription(
                subscribeRequest,
                ReadSubscribeResponse,
                CreateUnsubscribeRequest,
                ReadUnsubscribeResponse,
                HandleEventAsync,
                new MapSubscriptionState(mode, Name, handlers, state));

            await Cluster.Events.AddSubscriptionAsync(subscription, cancellationToken).CfAwait();

            return(subscription.Id);
        }
Example #2
0
 public MapSubscriptionState(int mode, string name, MapEventHandlers <TKey, TValue> handlers, object state)
     : base(name, handlers, state)
 {
     Mode = mode;
 }