From dcaac996af2fafe470ead0105eba0f179e7e5887 Mon Sep 17 00:00:00 2001 From: Simnico99 Date: Mon, 18 Dec 2023 11:38:40 -0500 Subject: [PATCH] Fixed Events --- Test/Program.cs | 12 ------- Test/Test.csproj | 14 -------- ZirconNet.sln | 6 ---- src/ZirconNet.Core/Events/WeakEventBase.cs | 42 ++++++++-------------- 4 files changed, 15 insertions(+), 59 deletions(-) delete mode 100644 Test/Program.cs delete mode 100644 Test/Test.csproj diff --git a/Test/Program.cs b/Test/Program.cs deleted file mode 100644 index ec57fbc..0000000 --- a/Test/Program.cs +++ /dev/null @@ -1,12 +0,0 @@ -using ZirconNet.Core.Events; - -await Task.Delay(1000); - -var test = new WeakEvent(); -test.Subscribe((x) => Console.WriteLine($"Hello {x}!")); -test.Publish(1); - -await Task.Delay(1000); - -Console.ReadLine(); -await Task.Delay(1000); diff --git a/Test/Test.csproj b/Test/Test.csproj deleted file mode 100644 index 110baef..0000000 --- a/Test/Test.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - Exe - net8.0 - enable - enable - - - - - - - diff --git a/ZirconNet.sln b/ZirconNet.sln index 8a98376..0902f27 100644 --- a/ZirconNet.sln +++ b/ZirconNet.sln @@ -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 @@ -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 diff --git a/src/ZirconNet.Core/Events/WeakEventBase.cs b/src/ZirconNet.Core/Events/WeakEventBase.cs index 607c710..320896c 100644 --- a/src/ZirconNet.Core/Events/WeakEventBase.cs +++ b/src/ZirconNet.Core/Events/WeakEventBase.cs @@ -20,10 +20,9 @@ public WeakEventBase(bool isAsync = false) public void SubscribeInternal(TDelegate handler) where TDelegate : Delegate { - var weakHandler = new WeakReference(handler); lock (_syncRoot) { - _handlers.Add(weakHandler); + _handlers.Add(new WeakReference(handler)); } } @@ -38,46 +37,35 @@ public void UnsubscribeInternal(TDelegate handler) public void PublishInternal(T data) { - var toInvoke = new List(); - - 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 handler, T data) { - switch (handler) + if (handler.TryGetTarget(out var target)) { - case Action action: - action(data); - break; - case Func func: - func(data); - break; + switch (target) + { + case Action action: + action(data); + break; + case Func func: + func(); + break; + } } } }