Ejemplo n.º 1
0
        /// <summary>
        ///     (Temporarily) suppresses item count change notification until the returned <see cref="IDisposable" />
        ///     has been Disposed.
        /// </summary>
        /// <param name="signalCurrentCountWhenFinished">
        ///     if set to <c>true</c> signals a the <see cref="IReadOnlyCollection{T}.Count" />
        ///     when finished.
        /// </param>
        /// <returns></returns>
        public virtual IDisposable SuppressCountChangeNotifications(bool signalCurrentCountWhenFinished = true)
        {
            CheckForAndThrowIfDisposed(false);

            IsTrackingCountChanges = false;

            return(Disposable.Create(() =>
            {
                IsTrackingCountChanges = true;

                if (signalCurrentCountWhenFinished)
                {
                    CountChangesObserver.OnNext(Count);
                }
            }));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Notifies all <see cref="CollectionChanges" /> and <see cref="Resets" /> subscribers and
        ///     raises the (observable)collection changed events.
        /// </summary>
        /// <param name="observableCollectionChange">The observable collection change.</param>
        protected virtual void NotifyObserversAboutCollectionChanges(IObservableCollectionChange <T> observableCollectionChange)
        {
            if (observableCollectionChange == null)
            {
                throw new ArgumentNullException(nameof(observableCollectionChange));
            }

            CheckForAndThrowIfDisposed();

            // go ahead and check whether a Reset or item add, -change, -move or -remove shall be signaled
            // .. based on the ThresholdAmountWhenChangesAreNotifiedAsReset value
            var actualObservableCollectionChange =
                (observableCollectionChange.ChangeType == ObservableCollectionChangeType.Reset ||
                 IsItemsChangedAmountGreaterThanResetThreshold(1, ThresholdAmountWhenChangesAreNotifiedAsReset))
                    ? ObservableCollectionChange <T> .Reset
                    : observableCollectionChange;

            // raise events and notify about collection changes

            if (actualObservableCollectionChange.ChangeType == ObservableCollectionChangeType.ItemAdded ||
                actualObservableCollectionChange.ChangeType == ObservableCollectionChangeType.ItemRemoved ||
                actualObservableCollectionChange.ChangeType == ObservableCollectionChangeType.Reset)
            {
                try
                {
                    CountChangesObserver.OnNext(Count);
                }
                catch (Exception exception)
                {
                    var observerException = new ObserverException(
                        $"An error occured notifying {nameof(CountChanges)} Observers of this {this.GetType().Name}.",
                        exception);

                    ObserverExceptionsObserver.OnNext(observerException);

                    if (observerException.Handled == false)
                    {
                        throw;
                    }
                }
            }

            try
            {
                CollectionChangesObserver.OnNext(actualObservableCollectionChange);
            }
            catch (Exception exception)
            {
                var observerException = new ObserverException(
                    $"An error occured notifying {nameof(CollectionChanges)} Observers of this {this.GetType().Name}.",
                    exception);

                ObserverExceptionsObserver.OnNext(observerException);

                if (observerException.Handled == false)
                {
                    throw;
                }
            }

            try
            {
                RaiseCollectionChanged(actualObservableCollectionChange.ToNotifyCollectionChangedEventArgs());
            }
            catch (Exception exception)
            {
                var observerException = new ObserverException(
                    $"An error occured notifying CollectionChanged Subscribers of this {this.GetType().Name}.",
                    exception);

                ObserverExceptionsObserver.OnNext(observerException);

                if (observerException.Handled == false)
                {
                    throw;
                }
            }
        }