Skip to content

Commit 6fbb448

Browse files
Merge pull request #95 from Extrys/master
Abstract Signals Bugs Fixed
2 parents 2e93607 + 9ff2ac8 commit 6fbb448

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

Documentation/Signals.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ When event driven program is abused, it is possible to find yourself in "callbac
482482

483483
## <a id="abstract-signals"></a>Abstract Signals
484484

485-
One of the problems of the signals is that when subscribe to their concrete types
486-
you are coupling your concrete signal types to the subscribers
485+
One of the problems of the signals is that when you subscribe to their types you are coupling your concrete signal types to the subscribers
487486

488487
For example, Lets say I have a player and i want to save the game when i finish a level.
489488
Ok easy, I create ``SignalLevelCompleted`` and then I subscribe it to my ``SaveGameSystem``

UnityProject/Assets/Plugins/Zenject/OptionalExtras/Signals/Internal/SignalDeclaration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public IObservable<object> Stream
4141
}
4242
#endif
4343

44+
public List<SignalSubscription> Subscriptions => _subscriptions;
45+
4446
public int TickPriority
4547
{
4648
get; private set;

UnityProject/Assets/Plugins/Zenject/OptionalExtras/Signals/Main/SignalBus.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Zenject
1111
public class SignalBus : ILateDisposable
1212
{
1313
readonly SignalSubscription.Pool _subscriptionPool;
14-
readonly Dictionary<BindingId, SignalDeclaration> _localDeclarationMap;
14+
readonly Dictionary<BindingId, SignalDeclaration> _localDeclarationMap = new Dictionary<BindingId, SignalDeclaration>();
1515
readonly SignalBus _parentBus;
1616
readonly Dictionary<SignalSubscriptionId, SignalSubscription> _subscriptionMap = new Dictionary<SignalSubscriptionId, SignalSubscription>();
1717
readonly ZenjectSettings.SignalSettings _settings;
@@ -35,7 +35,14 @@ public SignalBus(
3535
_signalDeclarationFactory = signalDeclarationFactory;
3636
_container = container;
3737

38-
_localDeclarationMap = signalDeclarations.ToDictionary(x => x.BindingId, x => x);
38+
signalDeclarations.ForEach(x =>
39+
{
40+
if (!_localDeclarationMap.ContainsKey(x.BindingId))
41+
{
42+
_localDeclarationMap.Add(x.BindingId, x);
43+
}
44+
else _localDeclarationMap[x.BindingId].Subscriptions.AllocFreeAddRange(x.Subscriptions);
45+
});
3946
_parentBus = parentBus;
4047
}
4148

@@ -57,15 +64,15 @@ public void AbstractFireId<TSignal>(object identifier, TSignal signal)
5764
{
5865
// Do this before creating the signal so that it throws if the signal was not declared
5966
Type signalType = typeof(TSignal);
67+
InternalFire(signalType, signal, identifier, true);
6068
var declaration = GetDeclaration(signalType, identifier, true);
6169
declaration.Fire(signal);
6270

6371
Type[] interfaces = signalType.GetInterfaces();
6472
int numOfInterfaces = interfaces.Length;
6573
for (int i = 0; i < numOfInterfaces; i++)
6674
{
67-
declaration = GetDeclaration(interfaces[i], identifier, true);
68-
declaration.Fire(signal);
75+
InternalFire(interfaces[i], signal, identifier, true);
6976
}
7077
}
7178

@@ -213,7 +220,7 @@ public IObservable<TSignal> GetStream<TSignal>()
213220

214221
public IObservable<object> GetStreamId(Type signalType, object identifier)
215222
{
216-
return GetDeclaration(signalType, identifier, true).Stream;
223+
return GetDeclaration(new BindingId(signalType, identifier)).Stream;
217224
}
218225

219226
public IObservable<object> GetStream(Type signalType)

0 commit comments

Comments
 (0)