Fix overeager subscription cleanup (#712)
authorE.Z. Hart <hartez@users.noreply.github.com>
Tue, 24 Jan 2017 22:25:44 +0000 (15:25 -0700)
committerRui Marinho <me@ruimarinho.net>
Tue, 24 Jan 2017 22:25:44 +0000 (22:25 +0000)
Xamarin.Forms.Core.UnitTests/MessagingCenterTests.cs
Xamarin.Forms.Core/MessagingCenter.cs

index ae16fce..3cd91ed 100644 (file)
@@ -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()
index 5e60046..b6a167d 100644 (file)
@@ -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);
                }