private void SubscribeEvents(ReplicatedMapEventHandlers <TKey, TValue> events)
            {
                // TODO when event states are available, re-implement as ...?

                /*
                 * await dictionary.SubscribeAsync(events => events
                 *  .EntryAdded((sender, args, state) => { state.Foo++; }), state);
                 */

                events
                .EntryAdded((sender, args) => Interlocked.Increment(ref EntryAddedCount))
                .EntryUpdated((sender, args) => Interlocked.Increment(ref EntryUpdatedCount))
                .EntryRemoved((sender, args) => Interlocked.Increment(ref EntryRemovedCount))
                .Cleared((sender, args) => Interlocked.Increment(ref ClearedCount));
            }
示例#2
0
        private async Task <Guid> SubscribeAsync(Action <ReplicatedMapEventHandlers <TKey, TValue> > events, Maybe <TKey> key, IPredicate predicate, object state)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

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

            events(handlers);

            // 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 => ReplicatedMapAddEntryListenerCodec.EncodeRequest(Name, Cluster.IsSmartRouting),
                1 => ReplicatedMapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(keyv), Cluster.IsSmartRouting),
                2 => ReplicatedMapAddEntryListenerWithPredicateCodec.EncodeRequest(Name, ToData(predicate), Cluster.IsSmartRouting),
                3 => ReplicatedMapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(Name, ToData(keyv), ToData(predicate), Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

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

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

            return(subscription.Id);
        }
示例#3
0
 public SubscriptionState(int mode, string name, ReplicatedMapEventHandlers <TKey, TValue> handlers, object state)
     : base(name, handlers, state)
 {
     Mode = mode;
 }