Skip to content

Commit

Permalink
ObserverManager: Fix collection moddified. (MudBlazor#7843)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletKuro authored Dec 1, 2023
1 parent 08670f3 commit 544fa8f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,34 @@ async Task NotificationAsync(string observer)
.VerifyLogging($"Adding entry for {DefunctObserverId}/{DefunctObserver}. 1 total observers after add.")
.VerifyLogging($"Removing defunct entry for {DefunctObserverId}. 0 total observers after remove.");
}

[Test]
public void CollectionModified()
{
// Arrange
var observerManager = new ObserverManager<int, int>(NullLogger.Instance);

for (var i = 0; i < 1000; i++)
{
observerManager.Subscribe(i, i);
}

bool Predicate(int id, int observer)
{
if (id == 500)
{
observerManager.Subscribe(1001, 1001);
}

return true;
}

Task NotificationAsync(int observer)
{
return Task.CompletedTask;
}

// Act & Assert
Assert.DoesNotThrowAsync(() => observerManager.NotifyAsync(NotificationAsync, Predicate));
}
}
2 changes: 1 addition & 1 deletion src/MudBlazor/Utilities/ObserverManager/ObserverManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void Unsubscribe(TIdentity id)
public async Task NotifyAsync(Func<TObserver, Task> notification, Func<TIdentity, TObserver, bool>? predicate = null)
{
var defunct = default(List<TIdentity>);
foreach (var observer in _observers)
foreach (var observer in _observers.ToArray())
{
// Skip observers which don't match the provided predicate.
if (predicate != null && !predicate(observer.Key, observer.Value.Observer))
Expand Down

0 comments on commit 544fa8f

Please sign in to comment.