Skip to content

Commit

Permalink
Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mahara committed Aug 16, 2024
1 parent fe71212 commit e15adff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public void CanAdd_EmptyObservableCollection()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var addedItems = args.NewItems! as IEnumerable;
var addedItems = args.NewItems as IList;

Assert.That(addedItems, Is.Not.Null);
Assert.That(addedItems.Cast<object>().Count(), Is.EqualTo(addedItemsCount));
Assert.That(addedItems, Has.Count.EqualTo(addedItemsCount));
Assert.That(args.NewStartingIndex, Is.EqualTo(itemsCount));

itemsCount += addedItemsCount;
Expand Down Expand Up @@ -75,10 +75,10 @@ public void CanRemove_NonEmptyObservableCollection()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var removedItems = args.OldItems! as IEnumerable;
var removedItems = args.OldItems as IList;

Assert.That(removedItems, Is.Not.Null);
Assert.That(removedItems.Cast<object>().Count(), Is.EqualTo(removedItemsCount));
Assert.That(removedItems, Has.Count.EqualTo(removedItemsCount));
Assert.That(args.OldStartingIndex, Is.EqualTo(removedItemIndex));

itemsCount -= removedItemsCount;
Expand Down Expand Up @@ -111,10 +111,10 @@ public void CanAddRange_EmptyObservableCollection()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var addedItems = args.NewItems! as IEnumerable;
var addedItems = args.NewItems as IList;

Assert.That(addedItems, Is.Not.Null);
Assert.That(addedItems.Cast<object>().Count(), Is.EqualTo(addedItemsCount));
Assert.That(addedItems, Has.Count.EqualTo(addedItemsCount));
Assert.That(args.NewStartingIndex, Is.EqualTo(itemsCount));

itemsCount += addedItemsCount;
Expand Down Expand Up @@ -147,10 +147,10 @@ public void CanAddRange_NonEmptyObservableCollection()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var addedItems = args.NewItems! as IEnumerable;
var addedItems = args.NewItems as IList;

Assert.That(addedItems, Is.Not.Null);
Assert.That(addedItems.Cast<object>().Count(), Is.EqualTo(addedItemsCount));
Assert.That(addedItems, Has.Count.EqualTo(addedItemsCount));
Assert.That(args.NewStartingIndex, Is.EqualTo(itemsCount));

itemsCount += addedItemsCount;
Expand Down Expand Up @@ -185,10 +185,10 @@ public void CanRemoveRange_NonEmptyObservableCollection()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var removedItems = args.OldItems! as IEnumerable;
var removedItems = args.OldItems as IList;

Assert.That(removedItems, Is.Not.Null);
Assert.That(removedItems.Cast<object>().Count(), Is.EqualTo(removedItemsCount));
Assert.That(removedItems, Has.Count.EqualTo(removedItemsCount));
Assert.That(args.OldStartingIndex, Is.GreaterThanOrEqualTo(0));

itemsCount -= removedItemsCount;
Expand Down Expand Up @@ -256,10 +256,10 @@ public void CanRemoveRangeByIndexAndCount_NonEmptyObservableCollection()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var removedItems = args.OldItems! as IEnumerable;
var removedItems = args.OldItems as IList;

Assert.That(removedItems, Is.Not.Null);
Assert.That(removedItems.Cast<object>().Count(), Is.EqualTo(removedItemsCount));
Assert.That(removedItems, Has.Count.EqualTo(removedItemsCount));
Assert.That(args.OldStartingIndex, Is.EqualTo(removedItemsIndex));

itemsCount -= removedItemsCount;
Expand Down Expand Up @@ -327,10 +327,10 @@ public void CanReplaceRange_NonEmptyObservableCollection()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.GreaterThanOrEqualTo(1));

var addedItems = args.NewItems! as IEnumerable;
var addedItems = args.NewItems as IList;

Assert.That(addedItems, Is.Not.Null);
Assert.That(addedItems.Cast<object>().Count(), Is.EqualTo(itemsCount));
Assert.That(addedItems, Has.Count.EqualTo(itemsCount));
Assert.That(args.NewStartingIndex, Is.EqualTo(replaceStartingIndex));

var newItemsCount = _items.Count - 4 + _items.Count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public void CanAdd_EmptyObservableSet()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var addedItems = args.NewItems! as IEnumerable;
var addedItems = args.NewItems as IList;

Assert.That(addedItems, Is.Not.Null);
Assert.That(addedItems.Cast<object>().Count(), Is.EqualTo(addedItemsCount));
Assert.That(addedItems, Has.Count.EqualTo(addedItemsCount));
Assert.That(args.NewStartingIndex, Is.EqualTo(-1));

itemsCount += addedItemsCount;
Expand Down Expand Up @@ -75,10 +75,10 @@ public void CanRemove_NonEmptyObservableSet()
Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.EqualTo(1));

var removedItems = args.OldItems! as IEnumerable;
var removedItems = args.OldItems as IList;

Assert.That(removedItems, Is.Not.Null);
Assert.That(removedItems.Cast<object>().Count(), Is.EqualTo(removedItemsCount));
Assert.That(removedItems, Has.Count.EqualTo(removedItemsCount));
Assert.That(args.OldStartingIndex, Is.EqualTo(-1));

itemsCount -= removedItemsCount;
Expand Down
34 changes: 18 additions & 16 deletions src/NHibernate.ObservableCollections/ObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace Iesi.Collections.Generic;
/// </typeparam>
/// <remarks>
/// REFERENCES:
/// - <see href="https://github.com/dotnet/designs/pull/320" />
/// - <see href="https://github.com/dotnet/runtime/issues/18087" />
/// - <see href="https://github.com/dotnet/wpf/pull/9568" />
/// - <see href="https://gist.github.com/weitzhandler/65ac9113e31d12e697cb58cd92601091" />
/// - <see href="https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each" />
/// - <see href="https://github.com/CodingOctocat/WpfObservableRangeCollection" />
Expand All @@ -38,7 +40,7 @@ public class ObservableCollection<T> :
private int _blockReentrancyCount;

[NonSerialized]
private DeferredEventsCollection? _deferredEventsCollection;
private DeferredEventArgsCollection? _deferredEventArgsCollection;

/// <summary>
/// Initializes a new instance of <see cref="ObservableCollection{T}" />
Expand Down Expand Up @@ -177,7 +179,7 @@ protected virtual void MoveItem(int oldIndex, int newIndex)
protected override void ClearItems()
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();
using var __ = DeferEventNotifications();

CheckReentrancy();

Expand All @@ -196,7 +198,7 @@ protected override void ClearItems()
public void AddRange(IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();
using var __ = DeferEventNotifications();

InsertItemsRange(Count, collection);
}
Expand All @@ -210,7 +212,7 @@ public void AddRange(IEnumerable<T> collection)
public void InsertRange(int index, IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();
using var __ = DeferEventNotifications();

InsertItemsRange(index, collection);
}
Expand All @@ -223,7 +225,7 @@ public void InsertRange(int index, IEnumerable<T> collection)
public void RemoveRange(IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();
using var __ = DeferEventNotifications();

RemoveItemsRange(collection);
}
Expand All @@ -237,7 +239,7 @@ public void RemoveRange(IEnumerable<T> collection)
public void RemoveRange(int index, int count)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();
using var __ = DeferEventNotifications();

RemoveItemsRange(index, count);
}
Expand All @@ -252,7 +254,7 @@ public void RemoveRange(int index, int count)
public void ReplaceRange(int index, int count, IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();
using var __ = DeferEventNotifications();

RemoveItemsRange(index, count);
InsertItemsRange(index, collection);
Expand Down Expand Up @@ -390,9 +392,9 @@ protected virtual void RemoveItemsRange(int index, int count)
}
}

protected virtual IDisposable DeferEvents()
protected virtual IDisposable DeferEventNotifications()
{
return new DeferredEventsCollection(this);
return new DeferredEventArgsCollection(this);
}

/// <summary>
Expand All @@ -406,9 +408,9 @@ protected virtual IDisposable DeferEvents()
/// </remarks>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (_deferredEventsCollection is not null)
if (_deferredEventArgsCollection is not null)
{
_deferredEventsCollection.Add(e);
_deferredEventArgsCollection.Add(e);

return;
}
Expand Down Expand Up @@ -581,22 +583,22 @@ public void Dispose()
}
}

private sealed class DeferredEventsCollection : List<NotifyCollectionChangedEventArgs>, IDisposable
private sealed class DeferredEventArgsCollection : Collection<NotifyCollectionChangedEventArgs>, IDisposable
{
private readonly ObservableCollection<T> _collection;

public DeferredEventsCollection(ObservableCollection<T> collection)
public DeferredEventArgsCollection(ObservableCollection<T> collection)
{
Debug.Assert(collection is not null);
Debug.Assert(collection!._deferredEventsCollection is null);
Debug.Assert(collection!._deferredEventArgsCollection is null);

_collection = collection;
_collection._deferredEventsCollection = this;
_collection._deferredEventArgsCollection = this;
}

public void Dispose()
{
_collection._deferredEventsCollection = null;
_collection._deferredEventArgsCollection = null;
foreach (var args in this)
{
_collection.OnCollectionChanged(args);
Expand Down

0 comments on commit e15adff

Please sign in to comment.