diff --git a/CHANGELOG.md b/CHANGELOG.md
index 25ef062..c1d8e15 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,13 +4,16 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
-## [0.13.2] - 2024-11-13
+## [0.14.0] - 2024-11-15
**New**:
-- Added a constructor to *GameObjectPool* that allows to setup a costum instantiator
+- Added *PublishSafe* method to *IMessageBrokerService* to allow publishing messages safely in chase of chain subscriptions during publishing of a message
+
+**Changed**:
+- *Subscribe* and *Unsubscribe* throw an *InvalidOperationException* when being executed during a message being published
**Fixed**:
-- Fixed *ObjectPool* & *PoolService* tests that would block builds sometimes
+- CoroutineTests issues running when building released projects
## [0.13.1] - 2024-11-04
diff --git a/Runtime/MessageBrokerService.cs b/Runtime/MessageBrokerService.cs
index 68ce101..b62ae28 100644
--- a/Runtime/MessageBrokerService.cs
+++ b/Runtime/MessageBrokerService.cs
@@ -25,23 +25,32 @@ public interface IMessageBrokerService
/// Publish a message in the message broker.
/// If there is no object subscribing the message type, nothing will happen
///
+ ///
+ /// Use it there are a chain subscriptions during publishing
+ ///
void Publish(T message) where T : IMessage;
-
+
+ ///
+ /// Publish a message in the message broker.
+ /// If there is no object subscribing the message type, nothing will happen
+ ///
+ ///
+ /// This method can be slow and allocated extra memory if there are a lot of subscribers to the .
+ /// Use instead for faster iteration speed IF and ONLY IF there aren't chain subscriptions during publishing
+ ///
+ void PublishSafe(T message) where T : IMessage;
+
///
/// Subscribes to the message type.
/// Will invoke the every time the message of the subscribed type is published.
///
void Subscribe(Action action) where T : IMessage;
-
- ///
- /// Unsubscribe the from the message broker.
- ///
- void Unsubscribe(Action action) where T : IMessage;
-
+
///
- /// Unsubscribe all actions from the message broker from of the given message type.
+ /// Unsubscribe the action of from the in the message broker.
+ /// If is null then will unsubscribe from ALL subscribers currently subscribed to
///
- void Unsubscribe() where T : IMessage;
+ void Unsubscribe(object subscriber = null) where T : IMessage;
///
/// Unsubscribe from all messages.
@@ -53,7 +62,9 @@ public interface IMessageBrokerService
///
public class MessageBrokerService : IMessageBrokerService
{
- private readonly IDictionary> _subscriptions = new Dictionary>();
+ private readonly IDictionary> _subscriptions = new Dictionary>();
+
+ private (bool, IMessage) _isPublishing;
///
public void Publish(T message) where T : IMessage
@@ -63,18 +74,35 @@ public void Publish(T message) where T : IMessage
return;
}
- var subscriptionCopy = new IList[subscriptionObjects.Count];
-
+ _isPublishing = (true, message);
+
+ foreach (var subscription in subscriptionObjects)
+ {
+ var action = (Action)subscription.Value;
+
+ action(message);
+ }
+
+ _isPublishing = (false, message);
+ }
+
+ ///
+ public void PublishSafe(T message) where T : IMessage
+ {
+ if (!_subscriptions.TryGetValue(typeof(T), out var subscriptionObjects))
+ {
+ return;
+ }
+
+ var subscriptionCopy = new Delegate[subscriptionObjects.Count];
+
subscriptionObjects.Values.CopyTo(subscriptionCopy, 0);
for (var i = 0; i < subscriptionCopy.Length; i++)
{
- var actions = (List>) subscriptionCopy[i];
+ var action = (Action)subscriptionCopy[i];
- for (var index = 0; index < actions.Count; index++)
- {
- actions[index](message);
- }
+ action(message);
}
}
@@ -88,45 +116,44 @@ public void Subscribe(Action action) where T : IMessage
{
throw new ArgumentException("Subscribe static functions to a message is not supported!");
}
-
- if (!_subscriptions.TryGetValue(type, out var subscriptionObjects))
+ if(_isPublishing.Item1)
{
- subscriptionObjects = new Dictionary