From 408f83e401064dd40de71e8320ea44af1f1dbb3e Mon Sep 17 00:00:00 2001 From: agracio Date: Wed, 5 Nov 2025 14:14:37 +0000 Subject: [PATCH] refactoring dotnet events --- src/ElectronNET.API/API/AutoUpdater.cs | 134 ++---------------- src/ElectronNET.API/Common/ApiEventManager.cs | 5 - 2 files changed, 14 insertions(+), 125 deletions(-) diff --git a/src/ElectronNET.API/API/AutoUpdater.cs b/src/ElectronNET.API/API/AutoUpdater.cs index da1001f..e1f5e06 100644 --- a/src/ElectronNET.API/API/AutoUpdater.cs +++ b/src/ElectronNET.API/API/AutoUpdater.cs @@ -5,6 +5,8 @@ using Newtonsoft.Json.Serialization; using System; using System.Collections.Generic; using System.Threading.Tasks; +using ElectronNET.Common; +// ReSharper disable InconsistentNaming namespace ElectronNET.API { @@ -286,26 +288,8 @@ namespace ElectronNET.API /// public event Action OnError { - add - { - if (_error == null) - { - BridgeConnector.Socket.On("autoUpdater-error" + GetHashCode(), (message) => - { - _error(message.ToString()); - }); - - BridgeConnector.Socket.Emit("register-autoUpdater-error-event", GetHashCode()); - } - _error += value; - } - remove - { - _error -= value; - - if (_error == null) - BridgeConnector.Socket.Off("autoUpdater-error" + GetHashCode()); - } + add => ApiEventManager.AddEventWithSuffix("autoUpdater-error", GetHashCode(), _error, value, (args) => args.ToString()); + remove => ApiEventManager.RemoveEvent("autoUpdater-error", GetHashCode(), _error, value); } private event Action _error; @@ -315,26 +299,8 @@ namespace ElectronNET.API /// public event Action OnCheckingForUpdate { - add - { - if (_checkingForUpdate == null) - { - BridgeConnector.Socket.On("autoUpdater-checking-for-update" + GetHashCode(), () => - { - _checkingForUpdate(); - }); - - BridgeConnector.Socket.Emit("register-autoUpdater-checking-for-update-event", GetHashCode()); - } - _checkingForUpdate += value; - } - remove - { - _checkingForUpdate -= value; - - if (_checkingForUpdate == null) - BridgeConnector.Socket.Off("autoUpdater-checking-for-update" + GetHashCode()); - } + add => ApiEventManager.AddEventWithSuffix("autoUpdater-checking-for-update", GetHashCode(), _checkingForUpdate, value); + remove => ApiEventManager.RemoveEvent("autoUpdater-checking-for-update", GetHashCode(), _checkingForUpdate, value); } private event Action _checkingForUpdate; @@ -345,26 +311,8 @@ namespace ElectronNET.API /// public event Action OnUpdateAvailable { - add - { - if (_updateAvailable == null) - { - BridgeConnector.Socket.On("autoUpdater-update-available" + GetHashCode(), (updateInfo) => - { - _updateAvailable(JObject.Parse(updateInfo.ToString()).ToObject()); - }); - - BridgeConnector.Socket.Emit("register-autoUpdater-update-available-event", GetHashCode()); - } - _updateAvailable += value; - } - remove - { - _updateAvailable -= value; - - if (_updateAvailable == null) - BridgeConnector.Socket.Off("autoUpdater-update-available" + GetHashCode()); - } + add => ApiEventManager.AddEventWithSuffix("autoUpdater-update-available", GetHashCode(), _updateAvailable, value, (args) => JObject.Parse(args.ToString()).ToObject()); + remove => ApiEventManager.RemoveEvent("autoUpdater-update-available", GetHashCode(), _updateAvailable, value); } private event Action _updateAvailable; @@ -374,26 +322,8 @@ namespace ElectronNET.API /// public event Action OnUpdateNotAvailable { - add - { - if (_updateNotAvailable == null) - { - BridgeConnector.Socket.On("autoUpdater-update-not-available" + GetHashCode(), (updateInfo) => - { - _updateNotAvailable(JObject.Parse(updateInfo.ToString()).ToObject()); - }); - - BridgeConnector.Socket.Emit("register-autoUpdater-update-not-available-event", GetHashCode()); - } - _updateNotAvailable += value; - } - remove - { - _updateNotAvailable -= value; - - if (_updateNotAvailable == null) - BridgeConnector.Socket.Off("autoUpdater-update-not-available" + GetHashCode()); - } + add => ApiEventManager.AddEventWithSuffix("autoUpdater-update-not-available", GetHashCode(), _updateNotAvailable, value, (args) => JObject.Parse(args.ToString()).ToObject()); + remove => ApiEventManager.RemoveEvent("autoUpdater-update-not-available", GetHashCode(), _updateNotAvailable, value); } private event Action _updateNotAvailable; @@ -403,26 +333,8 @@ namespace ElectronNET.API /// public event Action OnDownloadProgress { - add - { - if (_downloadProgress == null) - { - BridgeConnector.Socket.On("autoUpdater-download-progress" + GetHashCode(), (progressInfo) => - { - _downloadProgress(JObject.Parse(progressInfo.ToString()).ToObject()); - }); - - BridgeConnector.Socket.Emit("register-autoUpdater-download-progress-event", GetHashCode()); - } - _downloadProgress += value; - } - remove - { - _downloadProgress -= value; - - if (_downloadProgress == null) - BridgeConnector.Socket.Off("autoUpdater-download-progress" + GetHashCode()); - } + add => ApiEventManager.AddEventWithSuffix("autoUpdater-download-progress", GetHashCode(), _downloadProgress, value, (args) => JObject.Parse(args.ToString()).ToObject()); + remove => ApiEventManager.RemoveEvent("autoUpdater-download-progress", GetHashCode(), _downloadProgress, value); } private event Action _downloadProgress; @@ -432,26 +344,8 @@ namespace ElectronNET.API /// public event Action OnUpdateDownloaded { - add - { - if (_updateDownloaded == null) - { - BridgeConnector.Socket.On("autoUpdater-update-downloaded" + GetHashCode(), (updateInfo) => - { - _updateDownloaded(JObject.Parse(updateInfo.ToString()).ToObject()); - }); - - BridgeConnector.Socket.Emit("register-autoUpdater-update-downloaded-event", GetHashCode()); - } - _updateDownloaded += value; - } - remove - { - _updateDownloaded -= value; - - if (_updateDownloaded == null) - BridgeConnector.Socket.Off("autoUpdater-update-downloaded" + GetHashCode()); - } + add => ApiEventManager.AddEventWithSuffix("autoUpdater-update-downloaded", GetHashCode(), _updateDownloaded, value, (args) => JObject.Parse(args.ToString()).ToObject()); + remove => ApiEventManager.RemoveEvent("autoUpdater-update-downloaded", GetHashCode(), _updateDownloaded, value); } private event Action _updateDownloaded; diff --git a/src/ElectronNET.API/Common/ApiEventManager.cs b/src/ElectronNET.API/Common/ApiEventManager.cs index 687d141..c621698 100644 --- a/src/ElectronNET.API/Common/ApiEventManager.cs +++ b/src/ElectronNET.API/Common/ApiEventManager.cs @@ -7,11 +7,6 @@ namespace ElectronNET.Common; internal class ApiEventManager { - internal T Deserialize(Func action) - { - return action.Invoke(); - } - internal static void AddEvent(string eventName, object id, Action callback, Action value, string suffix = "") { if (callback == null)