Beispiel #1
0
        /// <summary>
        /// Called when the dictionary change notification is triggered
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDictionaryChangedHandler(object sender, NotifyDictionaryChangedEventArgs <string, Order> e)
        {
            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Clear:
                var clearEvent = e as NotifyDictionaryClearEventArgs <string, Order>;
                ProcessDictionaryClearNotification(clearEvent);
                return;

            case NotifyDictionaryChangedAction.Add:
                var addEvent = e as NotifyDictionaryItemAddedEventArgs <string, Order>;
                ProcessDictionaryAddNotification(addEvent);
                return;

            case NotifyDictionaryChangedAction.Update:
                var updateEvent = e as NotifyDictionaryItemUpdatedEventArgs <string, Order>;
                ProcessDictionaryUpdateNotification(updateEvent);
                return;

            case NotifyDictionaryChangedAction.Remove:
                var removeEvent = e as NotifyDictionaryItemRemovedEventArgs <string, Order>;
                ProcessDictionaryRemoveNotification(removeEvent);
                return;

            default:
                break;
            }
        }
Beispiel #2
0
        public void OnDictionaryChangedHandler <TKey, TValue>(object sender, NotifyDictionaryChangedEventArgs <TKey, TValue> e)
            where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            var state = sender as IReliableState;

            this.changeCollector.AddNewEvent(state.Name.ToString(), e);
        }
        /// <summary>
        ///     Handles the DictionaryChanged event of the Dictionary control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="string" /> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        private void Dictionary_DictionaryChanged(object sender, NotifyDictionaryChangedEventArgs <string, UserState> e)
        {
            if (this.Partition.WriteStatus != PartitionAccessStatus.Granted)
            {
                return;
            }
            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Add:

                ServiceEventSource.Current.Message("Add Dictionary.");
                return;

            case NotifyDictionaryChangedAction.Update:
                ServiceEventSource.Current.Message("Update Dictionary.");
                return;

            case NotifyDictionaryChangedAction.Remove:
                ServiceEventSource.Current.Message("Remove Dictionary.");
                return;

            case NotifyDictionaryChangedAction.Clear:
                break;

            case NotifyDictionaryChangedAction.Rebuild:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public void TestRemoveNotifiesBoundDictionarySubscriptions()
        {
            const string item = "item";

            bindableStringByteDictionary.Add(item, 0);
            var dictA = new BindableDictionary <string, byte>();

            dictA.BindTo(bindableStringByteDictionary);

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgsA1 = null;
            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgsA2 = null;

            dictA.CollectionChanged += (_, args) => triggeredArgsA1 = args;
            dictA.CollectionChanged += (_, args) => triggeredArgsA2 = args;

            var dictB = new BindableDictionary <string, byte>();

            dictB.BindTo(bindableStringByteDictionary);

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgsB1 = null;
            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgsB2 = null;

            dictB.CollectionChanged += (_, args) => triggeredArgsB1 = args;
            dictB.CollectionChanged += (_, args) => triggeredArgsB2 = args;

            bindableStringByteDictionary.Remove(item);

            Assert.That(triggeredArgsA1, Is.Not.Null);
            Assert.That(triggeredArgsA2, Is.Not.Null);
            Assert.That(triggeredArgsB1, Is.Not.Null);
            Assert.That(triggeredArgsB2, Is.Not.Null);
        }
Beispiel #5
0
        public MockReliableDictionary(Uri uri)
            : base(uri)
        {
            // Set the OnDictionaryChanged callback to fire the DictionaryChanged event.
            InternalDictionaryChanged +=
                (sender, c) =>
            {
                if (DictionaryChanged != null)
                {
                    NotifyDictionaryChangedEventArgs <TKey, TValue> e = null;
                    switch (c.ChangeType)
                    {
                    case ChangeType.Added:
                        e = new NotifyDictionaryItemAddedEventArgs <TKey, TValue>(c.Transaction, c.Key, c.Added);
                        break;

                    case ChangeType.Removed:
                        e = new NotifyDictionaryItemRemovedEventArgs <TKey, TValue>(c.Transaction, c.Key);
                        break;

                    case ChangeType.Updated:
                        e = new NotifyDictionaryItemUpdatedEventArgs <TKey, TValue>(c.Transaction, c.Key, c.Added);
                        break;
                    }

                    DictionaryChanged.Invoke(this, e);
                }

                MockDictionaryChanged?.Invoke(this, c);
            };
        }
        public void TestClearNotifiesSubscriber()
        {
            var items = new[]
            {
                new KeyValuePair <string, byte>("test0", 0),
                new KeyValuePair <string, byte>("test1", 1),
                new KeyValuePair <string, byte>("test2", 2),
                new KeyValuePair <string, byte>("test3", 3),
                new KeyValuePair <string, byte>("test4", 4)
            };

            foreach (var(key, value) in items)
            {
                bindableStringByteDictionary.Add(key, value);
            }

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs = args;

            bindableStringByteDictionary.Clear();

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyDictionaryChangedAction.Remove));
            Assert.That(triggeredArgs.OldItems, Is.EquivalentTo(items));
        }
Beispiel #7
0
        private void OnDictionaryChangedHandler(object sender, NotifyDictionaryChangedEventArgs <long, WinFabPersistence.PersistedData> e)
        {
            lock (this)
            {
                switch (e.Action)
                {
                // Clear notification should never (?) be receved.
                case NotifyDictionaryChangedAction.Clear:
                    throw new InvalidOperationException("Unexpected notification: " + e.ToString());

                case NotifyDictionaryChangedAction.Add:
                    var addEvent = e as NotifyDictionaryItemAddedEventArgs <long, WinFabPersistence.PersistedData>;
                    this.ProcessAdd((ulong)addEvent.Transaction.TransactionId, addEvent.Value.Data);
                    return;

                case NotifyDictionaryChangedAction.Update:
                    var updateEvent = e as NotifyDictionaryItemUpdatedEventArgs <long, WinFabPersistence.PersistedData>;
                    this.ProcessUpdate((ulong)updateEvent.Transaction.TransactionId, updateEvent.Value.Data);
                    return;

                case NotifyDictionaryChangedAction.Remove:
                    var deleteEvent = e as NotifyDictionaryItemRemovedEventArgs <long, WinFabPersistence.PersistedData>;
                    this.ProcessRemove((ulong)deleteEvent.Transaction.TransactionId, (ulong)deleteEvent.Key);
                    return;

                default:
                    break;
                }
            }
        }
Beispiel #8
0
 /// <summary>
 /// Dictionary has changed. Notify any listeners.
 /// </summary>
 /// <param name="e">Evevnt arguments</param>
 protected virtual void OnDictionaryChanged(NotifyDictionaryChangedEventArgs e)
 {
     if (this.DictionaryChanged != null)
     {
         this.DictionaryChanged(this, e);
     }
 }
Beispiel #9
0
        /// <summary>
        /// Update the aggregated pages as the set of tags changes.
        /// </summary>
        /// <param name="sender">collection which emits the event</param>
        /// <param name="e">event details</param>
        private void OnTagsCollectionChanged(object sender, NotifyDictionaryChangedEventArgs <string, TagPageSet> e)
        {
            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Add:
                foreach (var item in e.Items)
                {
                    _aggregatedPages.UnionWith(item.FilteredPages);
                }
                break;

            case NotifyDictionaryChangedAction.Remove:
                // rebuild the set
                _aggregatedPages.Clear();
                foreach (var item in e.Items)
                {
                    _aggregatedPages.UnionWith(item.FilteredPages);
                }
                break;

            case NotifyDictionaryChangedAction.Reset:
                _aggregatedPages.Clear();
                break;
            }
        }
        internal void OnDictionaryChanged <TKey, TValue>(object sender, NotifyDictionaryChangedEventArgs <TKey, TValue> e)
        {
            var reliableState = sender as IReliableState;

            log.Info($"DictionaryChange Event for {reliableState.Name} with Event :- {e.Action}");
            this.CollectChanges(reliableState.Name, e);
        }
Beispiel #11
0
        private void onPlayingUserStatesChanged(object sender, NotifyDictionaryChangedEventArgs <int, SpectatorState> e)
        {
            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Add:
                foreach (var(userId, state) in e.NewItems.AsNonNull())
                {
                    onUserStateAdded(userId, state);
                }
                break;

            case NotifyDictionaryChangedAction.Remove:
                foreach (var(userId, _) in e.OldItems.AsNonNull())
                {
                    onUserStateRemoved(userId);
                }
                break;

            case NotifyDictionaryChangedAction.Replace:
                foreach (var(userId, _) in e.OldItems.AsNonNull())
                {
                    onUserStateRemoved(userId);
                }

                foreach (var(userId, state) in e.NewItems.AsNonNull())
                {
                    onUserStateAdded(userId, state);
                }
                break;
            }
        }
        public void TestBindCollectionChangedNotRunIfParsingSequenceEqualEnumerable()
        {
            var dict = new BindableDictionary <string, byte>
            {
                { "a", 1 },
                { "b", 3 },
                { "c", 5 },
                { "d", 7 }
            };

            var enumerable = new[]
            {
                new KeyValuePair <string, byte>("a", 1),
                new KeyValuePair <string, byte>("b", 3),
                new KeyValuePair <string, byte>("c", 5),
                new KeyValuePair <string, byte>("d", 7)
            };

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            dict.BindCollectionChanged((_, args) => triggeredArgs = args);
            dict.Parse(enumerable);

            Assert.That(triggeredArgs, Is.Null);
        }
        public void TestBindCollectionChangedNotRunIfBoundToSequenceEqualDictionary()
        {
            var dict = new BindableDictionary <string, byte>
            {
                { "a", 1 },
                { "b", 3 },
                { "c", 5 },
                { "d", 7 }
            };

            var otherDict = new BindableDictionary <string, byte>
            {
                { "a", 1 },
                { "b", 3 },
                { "c", 5 },
                { "d", 7 }
            };

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            dict.BindCollectionChanged((_, args) => triggeredArgs = args);
            dict.BindTo(otherDict);

            Assert.That(triggeredArgs, Is.Null);
        }
 protected void AssertDictionaryEventReset(object sender, NotifyDictionaryChangedEventArgs <TKey, TItem> args)
 {
     Assert.That(args.Action, Is.EqualTo(NotifyDictionaryChangedAction.Reset));
     Assert.That(args.OldItems, Is.Null);
     Assert.That(args.NewItems, Is.Null);
     Assert.That(args.OldKeys, Is.Null);
     Assert.That(args.NewKeys, Is.Null);
 }
Beispiel #15
0
 protected virtual void OnDictionaryChanged(NotifyDictionaryChangedEventArgs <TKey, TValue> e)
 {
     if (DictionaryChanged == null)
     {
         return;
     }
     using (BlockReentrancy()) { DictionaryChanged(this, e); }
 }
Beispiel #16
0
    private void OnDictionaryItemChanged(string key, object oldValue, object newValue)
    {
        if (DictionaryChanged != null)
        {
            var eventArgs = NotifyDictionaryChangedEventArgs <string, object> .ForValueChangedAction(key, oldValue, newValue);

            DictionaryChanged(this, eventArgs);
        }
    }
Beispiel #17
0
    private void OnDictionaryReset()
    {
        if (DictionaryChanged != null)
        {
            var eventArgs = NotifyDictionaryChangedEventArgs <string, object> .ForResetAction();

            DictionaryChanged(this, eventArgs);
        }
    }
Beispiel #18
0
    private void OnDictionaryItemRemoved(string key)
    {
        if (DictionaryChanged != null)
        {
            var eventArgs = NotifyDictionaryChangedEventArgs <string, object> .ForRemoveAction(key);

            DictionaryChanged(this, eventArgs);
        }
    }
Beispiel #19
0
        /// <summary>
        ///     Handles the DictionaryChanged event of the Dictionary control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="string" /> instance containing the event data.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        private void Dictionary_DictionaryChanged(object sender, NotifyDictionaryChangedEventArgs <string, UserState> e)
        {
            if (this.Partition.WriteStatus != PartitionAccessStatus.Granted)
            {
                return;
            }
            IReliableDictionary <string, UserState> dictionary = sender as IReliableDictionary <string, UserState>;

            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Add:
                NotifyDictionaryItemAddedEventArgs <string, UserState> add = e as NotifyDictionaryItemAddedEventArgs <string, UserState>;
                this.sendService.SendMessageAsync(new FabricNotifyModel
                {
                    Action        = NotifyAction.Add,
                    Value         = add?.Value.ToJson(),
                    ServiceName   = this.Context.ServiceName.AbsoluteUri,
                    DictionaryKey = dictionary?.Name.AbsolutePath,
                    Key           = null
                }).GetAwaiter().GetResult();
                break;

            case NotifyDictionaryChangedAction.Update:
                NotifyDictionaryItemUpdatedEventArgs <string, UserState> update = e as NotifyDictionaryItemUpdatedEventArgs <string, UserState>;
                this.sendService.SendMessageAsync(new FabricNotifyModel
                {
                    Action        = NotifyAction.Update,
                    Value         = update?.Value.ToJson(),
                    ServiceName   = this.Context.ServiceTypeName,
                    DictionaryKey = dictionary?.Name.AbsolutePath,
                    Key           = null
                }).GetAwaiter().GetResult();
                break;

            case NotifyDictionaryChangedAction.Remove:
                NotifyDictionaryItemRemovedEventArgs <string, UserState> remove = e as NotifyDictionaryItemRemovedEventArgs <string, UserState>;
                this.sendService.SendMessageAsync(new FabricNotifyModel
                {
                    Action        = NotifyAction.Delete,
                    Key           = remove?.Key,
                    ServiceName   = this.Context.ServiceTypeName,
                    DictionaryKey = dictionary?.Name.AbsolutePath,
                    Value         = null
                }).GetAwaiter().GetResult();
                break;

            case NotifyDictionaryChangedAction.Clear:
                break;

            case NotifyDictionaryChangedAction.Rebuild:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public void TestAddWithStringNotifiesSubscriber(string key, byte value)
        {
            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs = args;

            bindableStringByteDictionary.Add(key, value);

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyDictionaryChangedAction.Add));
            Assert.That(triggeredArgs.NewItems, Is.EquivalentTo(new KeyValuePair <string, byte>(key, value).Yield()));
        }
        public void TestSetNotifiesSubscribersOfAddWhenItemDoesNotExist()
        {
            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs = args;

            bindableStringByteDictionary["0"] = 0;

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyDictionaryChangedAction.Add));
            Assert.That(triggeredArgs.OldItems, Is.Null);
            Assert.That(triggeredArgs.NewItems, Is.EquivalentTo(new KeyValuePair <string, byte>("0", 0).Yield()));
        }
        public void TestRemoveDoesNotNotifySubscribersBeforeItemIsRemoved()
        {
            const string item = "item";

            bindableStringByteDictionary.Add(item, 0);

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs = args;

            Assert.That(triggeredArgs, Is.Null);
        }
        public void TestBindCollectionChangedWithoutRunningImmediately()
        {
            var dict = new BindableDictionary <string, byte> {
                { "a", 1 }
            };

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            dict.BindCollectionChanged((_, args) => triggeredArgs = args);

            Assert.That(triggeredArgs, Is.Null);
        }
        public void TestBindCollectionChangedWithRunImmediately()
        {
            var dict = new BindableDictionary <string, byte> {
                { "a", 1 }
            };

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            dict.BindCollectionChanged((_, args) => triggeredArgs = args, true);

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyDictionaryChangedAction.Add));
            Assert.That(triggeredArgs.NewItems, Is.EquivalentTo(dict));
        }
        public void TestBoundCopyWithAdd()
        {
            var boundCopy = bindableStringByteDictionary.GetBoundCopy();

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            boundCopy.CollectionChanged += (_, args) => triggeredArgs = args;

            bindableStringByteDictionary.Add("test", 0);

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyDictionaryChangedAction.Add));
            Assert.That(triggeredArgs.NewItems, Is.EquivalentTo(new KeyValuePair <string, byte>("test", 0).Yield()));
        }
        private void Dictionary_DictionaryChanged(object sender, NotifyDictionaryChangedEventArgs <int, NationalCountyStats> e)
        {
            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Clear:
                return;

            case NotifyDictionaryChangedAction.Add:
                NotifyDictionaryItemAddedEventArgs <int, NationalCountyStats> addEvent = e as NotifyDictionaryItemAddedEventArgs <int, NationalCountyStats>;

                long tmp = -1;

                if (this.statsDictionary.TryGetValue("totalDoctors", out tmp))
                {
                    this.statsDictionary["totalDoctors"]           += addEvent.Value.DoctorCount;
                    this.statsDictionary["totalPatientCount"]      += addEvent.Value.PatientCount;
                    this.statsDictionary["totalHealthReportCount"] += addEvent.Value.HealthReportCount;
                }
                else
                {
                    this.statsDictionary["totalDoctors"]           = addEvent.Value.DoctorCount;
                    this.statsDictionary["totalPatientCount"]      = addEvent.Value.PatientCount;
                    this.statsDictionary["totalHealthReportCount"] = addEvent.Value.HealthReportCount;
                }

                this.historyDictionary[addEvent.Key] = new DataSet(
                    addEvent.Value.DoctorCount,
                    addEvent.Value.PatientCount,
                    addEvent.Value.HealthReportCount);
                return;

            case NotifyDictionaryChangedAction.Update:
                NotifyDictionaryItemUpdatedEventArgs <int, NationalCountyStats> updateEvent =
                    e as NotifyDictionaryItemUpdatedEventArgs <int, NationalCountyStats>;
                this.statsDictionary["totalDoctors"]           += (updateEvent.Value.DoctorCount - this.historyDictionary[updateEvent.Key].totalDoctors);
                this.statsDictionary["totalPatientCount"]      += (updateEvent.Value.PatientCount - this.historyDictionary[updateEvent.Key].totalPatientCount);
                this.statsDictionary["totalHealthReportCount"] +=
                    (updateEvent.Value.HealthReportCount - this.historyDictionary[updateEvent.Key].totalHealthReportCount);
                this.historyDictionary[updateEvent.Key] = new DataSet(
                    updateEvent.Value.DoctorCount,
                    updateEvent.Value.PatientCount,
                    updateEvent.Value.HealthReportCount);
                return;

            case NotifyDictionaryChangedAction.Remove:
                return;

            default:
                break;
            }
        }
            private void OnCompletedNoCheck(bool committed)
            {
                bool   existsAfterTx;
                TValue valueToSignal;
                NotifyDictionaryChangedEventArgs <TKey, TValue> changeArgs = null;

                lock (_owner._syncLock)
                {
                    _owner._pendingTransactions.Remove(_key);
                    if (!committed)
                    {
                        if (Exists)
                        {
                            _owner._internalDictionary[_key] = OriginalValue;
                        }
                        else
                        {
                            _owner._internalDictionary.TryRemove(_key, out _);
                        }

                        existsAfterTx = _owner._internalDictionary.TryGetValue(_key, out valueToSignal);
                    }
                    else
                    {
                        existsAfterTx = _owner._internalDictionary.TryGetValue(_key, out valueToSignal);

                        if (Exists)
                        {
                            changeArgs = existsAfterTx
                                ? (NotifyDictionaryChangedEventArgs <TKey, TValue>)
                                         new NotifyDictionaryItemUpdatedEventArgs <TKey, TValue>(_transaction, _key, valueToSignal)
                                : new NotifyDictionaryItemRemovedEventArgs <TKey, TValue>(_transaction, _key);
                        }
                        else
                        {
                            changeArgs = new NotifyDictionaryItemAddedEventArgs <TKey, TValue>(_transaction, _key, valueToSignal);
                        }
                    }
                }

                while (_waiters?.Count > 0)
                {
                    var waiter = _waiters.Dequeue();
                    Task.Run(() => waiter.Signal(existsAfterTx, valueToSignal));
                }

                if (changeArgs != null)
                {
                    _owner.OnDictionaryChanged(changeArgs);
                }
            }
        public void TestClearDoesNotNotifySubscriberBeforeClear()
        {
            for (byte i = 0; i < 5; i++)
            {
                bindableStringByteDictionary.Add($"test{i}", i);
            }

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs = args;

            Assert.That(triggeredArgs, Is.Null);

            bindableStringByteDictionary.Clear();
        }
        public void TestRemoveNotifiesSubscriber()
        {
            const string item = "item";

            bindableStringByteDictionary.Add(item, 0);

            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgs = null;

            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs = args;

            bindableStringByteDictionary.Remove(item);

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyDictionaryChangedAction.Remove));
            Assert.That(triggeredArgs.OldItems, Is.EquivalentTo(new KeyValuePair <string, byte>("item", 0).Yield()));
        }
        public void TestAddWithStringNotifiesMultipleSubscribersOnlyAfterTheAdd(string key, byte value)
        {
            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgsA = null;
            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgsB = null;
            NotifyDictionaryChangedEventArgs <string, byte> triggeredArgsC = null;

            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgsA = args;
            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgsB = args;
            bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgsC = args;

            Assert.That(triggeredArgsA, Is.Null);
            Assert.That(triggeredArgsB, Is.Null);
            Assert.That(triggeredArgsC, Is.Null);

            bindableStringByteDictionary.Add(key, value);
        }