private async Task <Guid> SubscribeAsync(IPredicate predicate, bool hasPredicate, TKey key, bool hasKey, Action <ReplicatedDictionaryEventHandlers <TKey, TValue> > handle)
        {
            if (hasKey && key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (hasPredicate && predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

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

            handle(handlers);

            // 0: no entryKey, no predicate
            // 1: entryKey, no predicate
            // 2: no entryKey, predicate
            // 3: entryKey, predicate
            var mode = (hasKey ? 1 : 0) + (hasPredicate ? 2 : 0);

            var subscribeRequest = mode switch
            {
                0 => ReplicatedMapAddEntryListenerCodec.EncodeRequest(Name, Cluster.IsSmartRouting),
                1 => ReplicatedMapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(key), Cluster.IsSmartRouting),
                2 => ReplicatedMapAddEntryListenerWithPredicateCodec.EncodeRequest(Name, ToData(predicate), Cluster.IsSmartRouting),
                3 => ReplicatedMapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(Name, ToData(key), ToData(predicate), Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

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

            await Cluster.Events.InstallSubscriptionAsync(subscription).CAF();

            return(subscription.Id);
        }
 public MapSubscriptionState(int mode, string name, ReplicatedDictionaryEventHandlers <TKey, TValue> handlers)
     : base(name, handlers)
 {
     Mode = mode;
 }