Skip to content

Commit

Permalink
Fixed Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Simnico99 committed Dec 18, 2023
1 parent 54d12cc commit dcaac99
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 59 deletions.
12 changes: 0 additions & 12 deletions Test/Program.cs

This file was deleted.

14 changes: 0 additions & 14 deletions Test/Test.csproj

This file was deleted.

6 changes: 0 additions & 6 deletions ZirconNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZirconNet.Console", "src\Zi
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZirconNet.Microsoft.DependencyInjection", "src\ZirconNet.Microsoft.DependencyInjection\ZirconNet.Microsoft.DependencyInjection.csproj", "{60BD3E3D-6311-4DE2-81B3-FAC2BEE2C0E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{803712D0-E7F5-41D7-A036-FBE064290722}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -42,10 +40,6 @@ Global
{60BD3E3D-6311-4DE2-81B3-FAC2BEE2C0E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60BD3E3D-6311-4DE2-81B3-FAC2BEE2C0E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60BD3E3D-6311-4DE2-81B3-FAC2BEE2C0E8}.Release|Any CPU.Build.0 = Release|Any CPU
{803712D0-E7F5-41D7-A036-FBE064290722}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{803712D0-E7F5-41D7-A036-FBE064290722}.Debug|Any CPU.Build.0 = Debug|Any CPU
{803712D0-E7F5-41D7-A036-FBE064290722}.Release|Any CPU.ActiveCfg = Release|Any CPU
{803712D0-E7F5-41D7-A036-FBE064290722}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
42 changes: 15 additions & 27 deletions src/ZirconNet.Core/Events/WeakEventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ public WeakEventBase(bool isAsync = false)
public void SubscribeInternal<TDelegate>(TDelegate handler)
where TDelegate : Delegate
{
var weakHandler = new WeakReference<Delegate>(handler);
lock (_syncRoot)
{
_handlers.Add(weakHandler);
_handlers.Add(new WeakReference<Delegate>(handler));
}
}

Expand All @@ -38,46 +37,35 @@ public void UnsubscribeInternal<TDelegate>(TDelegate handler)

public void PublishInternal(T data)
{
var toInvoke = new List<Delegate>();

lock (_syncRoot)
{
_handlers.RemoveAll(wr => !wr.TryGetTarget(out _));
foreach (var weakReference in _handlers)
{
if (weakReference.TryGetTarget(out var handler))
{
toInvoke.Add(handler);
}
}
}

if (_isAsync)
{
foreach (var handler in toInvoke)
foreach (var handler in _handlers)
{
Task.Run(() => InvokeHandler(handler, data)).Forget();
Task.Run(() => InvokeHandler(handler, data)).Forget(true);
}
}
else
{
foreach (var handler in toInvoke)
foreach (var handler in _handlers)
{
InvokeHandler(handler, data);
}
}
}

private static void InvokeHandler(Delegate handler, T data)
private static void InvokeHandler(WeakReference<Delegate> handler, T data)
{
switch (handler)
if (handler.TryGetTarget(out var target))
{
case Action<T> action:
action(data);
break;
case Func<T, object> func:
func(data);
break;
switch (target)
{
case Action<T> action:
action(data);
break;
case Func<T> func:
func();
break;
}
}
}
}

0 comments on commit dcaac99

Please sign in to comment.