Skip to content

Commit 4592e11

Browse files
committed
PR fixes
1 parent 63af7b0 commit 4592e11

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Runtime/MessageBrokerService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public interface IMessageBrokerService
6262
/// <inheritdoc />
6363
public class MessageBrokerService : IMessageBrokerService
6464
{
65-
private readonly IDictionary<Type, IDictionary<object, object>> _subscriptions = new Dictionary<Type, IDictionary<object, object>>();
65+
private readonly IDictionary<Type, IDictionary<object, Delegate>> _subscriptions = new Dictionary<Type, IDictionary<object, Delegate>>();
6666

6767
private (bool, IMessage) _isPublishing;
6868

@@ -94,7 +94,7 @@ public void PublishSafe<T>(T message) where T : IMessage
9494
return;
9595
}
9696

97-
var subscriptionCopy = new object[subscriptionObjects.Count];
97+
var subscriptionCopy = new Delegate[subscriptionObjects.Count];
9898

9999
subscriptionObjects.Values.CopyTo(subscriptionCopy, 0);
100100

@@ -124,7 +124,7 @@ public void Subscribe<T>(Action<T> action) where T : IMessage
124124

125125
if (!_subscriptions.TryGetValue(type, out var subscriptionObjects))
126126
{
127-
subscriptionObjects = new Dictionary<object, object>();
127+
subscriptionObjects = new Dictionary<object, Delegate>();
128128
_subscriptions.Add(type, subscriptionObjects);
129129
}
130130

@@ -170,6 +170,11 @@ public void UnsubscribeAll(object subscriber = null)
170170
return;
171171
}
172172

173+
if (_isPublishing.Item1)
174+
{
175+
throw new InvalidOperationException($"Cannot unsubscribe from {subscriber} message while publishing " +
176+
$"{_isPublishing.Item2.GetType().Name} message. Use {nameof(PublishSafe)} instead!");
177+
}
173178
foreach (var subscriptionObjects in _subscriptions.Values)
174179
{
175180
subscriptionObjects.Remove(subscriber);

0 commit comments

Comments
 (0)