Browse Source
Merge pull request #2997 from MarchingCube/fix-weakevent-unsubscribe
Fix WeakEventHandlerManager unsubscribe
timer-overload
Steven Kirk
7 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
19 additions and
4 deletions
-
src/Avalonia.Base/Utilities/WeakEventHandlerManager.cs
-
tests/Avalonia.Base.UnitTests/WeakEventHandlerManagerTests.cs
|
|
|
@ -161,9 +161,8 @@ namespace Avalonia.Utilities |
|
|
|
for (int c = 0; c < _count; ++c) |
|
|
|
{ |
|
|
|
var reference = _data[c].Subscriber; |
|
|
|
TSubscriber instance; |
|
|
|
|
|
|
|
if (reference != null && reference.TryGetTarget(out instance) && instance == s) |
|
|
|
if (reference != null && reference.TryGetTarget(out TSubscriber instance) && Equals(instance, s.Target)) |
|
|
|
{ |
|
|
|
_data[c] = default; |
|
|
|
removed = true; |
|
|
|
|
|
|
|
@ -36,7 +36,7 @@ namespace Avalonia.Base.UnitTests |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void EventShoudBePassedToSubscriber() |
|
|
|
public void EventShouldBePassedToSubscriber() |
|
|
|
{ |
|
|
|
bool handled = false; |
|
|
|
var subscriber = new Subscriber(() => handled = true); |
|
|
|
@ -47,7 +47,23 @@ namespace Avalonia.Base.UnitTests |
|
|
|
Assert.True(handled); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void EventShouldNotBeRaisedAfterUnsubscribe() |
|
|
|
{ |
|
|
|
bool handled = false; |
|
|
|
var subscriber = new Subscriber(() => handled = true); |
|
|
|
var source = new EventSource(); |
|
|
|
WeakEventHandlerManager.Subscribe<EventSource, EventArgs, Subscriber>(source, "Event", |
|
|
|
subscriber.OnEvent); |
|
|
|
|
|
|
|
WeakEventHandlerManager.Unsubscribe<EventArgs, Subscriber>(source, "Event", |
|
|
|
subscriber.OnEvent); |
|
|
|
|
|
|
|
source.Fire(); |
|
|
|
|
|
|
|
Assert.False(handled); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void EventHandlerShouldNotBeKeptAlive() |
|
|
|
{ |
|
|
|
|