Fix overeager subscription cleanup (#712)

This commit is contained in:
E.Z. Hart 2017-01-24 15:25:44 -07:00 коммит произвёл Rui Marinho
Родитель 4b19d49aa2
Коммит ebb24b7edd
2 изменённых файлов: 19 добавлений и 1 удалений

Просмотреть файл

@ -319,6 +319,24 @@ namespace Xamarin.Forms.Core.UnitTests
Assert.True(success); // TestCallbackSource.SuccessCallback() should be invoked to make success == true
}
[Test]
public void MultipleSubscribersOfTheSameClass()
{
var sub1 = new object();
var sub2 = new object();
string args2 = null;
const string message = "message";
MessagingCenter.Subscribe<MessagingCenterTests, string>(sub1, message, (sender, args) => { });
MessagingCenter.Subscribe<MessagingCenterTests, string>(sub2, message, (sender, args) => args2 = args);
MessagingCenter.Unsubscribe<MessagingCenterTests, string>(sub1, message);
MessagingCenter.Send(this, message, "Testing");
Assert.That(args2, Is.EqualTo("Testing"), "unsubscribing sub1 should not unsubscribe sub2");
}
class TestSubcriber
{
public void SetSuccess()

Просмотреть файл

@ -205,7 +205,7 @@ namespace Xamarin.Forms
var key = new Sender(message, senderType, argType);
if (!s_subscriptions.ContainsKey(key))
return;
s_subscriptions[key].RemoveAll(sub => !sub.CanBeRemoved() || sub.Subscriber.Target == subscriber);
s_subscriptions[key].RemoveAll(sub => sub.CanBeRemoved() || sub.Subscriber.Target == subscriber);
if (!s_subscriptions[key].Any())
s_subscriptions.Remove(key);
}