Skip to content

Commit 5e98164

Browse files
committed
Fixed weak events.
1 parent 8b4a521 commit 5e98164

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

ZirconNet.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1616
EndProject
1717
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZirconNet.Console", "src\ZirconNet.Console\ZirconNet.Console.csproj", "{B4263499-9A0D-40D5-B38D-EFAD661A72B9}"
1818
EndProject
19-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZirconNet.Microsoft.DependencyInjection", "src\ZirconNet.Microsoft.DependencyInjection\ZirconNet.Microsoft.DependencyInjection.csproj", "{60BD3E3D-6311-4DE2-81B3-FAC2BEE2C0E8}"
19+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZirconNet.Microsoft.DependencyInjection", "src\ZirconNet.Microsoft.DependencyInjection\ZirconNet.Microsoft.DependencyInjection.csproj", "{60BD3E3D-6311-4DE2-81B3-FAC2BEE2C0E8}"
2020
EndProject
2121
Global
2222
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/ZirconNet.Core/Events/WeakEventBase.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,54 @@
22
// This software is distributed under the MIT license and its code is open-source and free for use, modification, and distribution.
33
// </copyright>
44

5-
using System.Collections.Concurrent;
65
using ZirconNet.Core.Extensions;
76

87
namespace ZirconNet.Core.Events;
98

109
public abstract class WeakEventBase
1110
{
12-
private readonly ConcurrentDictionary<Type, List<Delegate>> _eventRegistrations = new();
11+
private readonly List<(Type, Delegate)> _eventRegistrations = new();
12+
private readonly object _lock = new();
1313

1414
protected virtual Subscription SubscribeInternal<T>(T action)
1515
where T : Delegate
1616
{
17-
if (action is null)
17+
lock (_lock)
1818
{
19-
throw new ArgumentNullException(nameof(action));
20-
}
19+
if (action is null)
20+
{
21+
throw new ArgumentNullException(nameof(action));
22+
}
2123

22-
_eventRegistrations.AddOrUpdate(typeof(T), new List<Delegate> { action }, (key, value) =>
23-
{
24-
value.Add(action);
25-
return value;
26-
});
24+
_eventRegistrations.Add((typeof(T), action));
2725

28-
return new Subscription(() => _eventRegistrations.AddOrUpdate(typeof(T), new List<Delegate>(), (key, value) =>
29-
{
30-
value.Remove(action);
31-
return value;
32-
}));
26+
return new Subscription(() =>
27+
{
28+
lock (_lock)
29+
{
30+
_eventRegistrations.Remove((typeof(T), action));
31+
}
32+
});
33+
}
3334
}
3435

3536
protected virtual void PublishInternal<T>(T? data)
3637
{
3738
Task.Run(() =>
3839
{
39-
if (_eventRegistrations.TryGetValue(typeof(T), out var actions))
40+
lock (_lock)
4041
{
41-
foreach (var action in actions)
42+
foreach (var eventRegistration in _eventRegistrations)
4243
{
43-
((Action<T?>)action)(data);
44+
if (eventRegistration.Item2 is Action<T?> action)
45+
{
46+
action(data);
47+
}
48+
49+
if (eventRegistration.Item2 is Func<T?> func)
50+
{
51+
func();
52+
}
4453
}
4554
}
4655
}).Forget();

0 commit comments

Comments
 (0)