Skip to content

Commit 408f83e

Browse files
committed
refactoring dotnet events
1 parent 689a002 commit 408f83e

File tree

2 files changed

+14
-125
lines changed

2 files changed

+14
-125
lines changed

src/ElectronNET.API/API/AutoUpdater.cs

Lines changed: 14 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Threading.Tasks;
8+
using ElectronNET.Common;
9+
// ReSharper disable InconsistentNaming
810

911
namespace ElectronNET.API
1012
{
@@ -286,26 +288,8 @@ public Dictionary<string, string> RequestHeaders
286288
/// </summary>
287289
public event Action<string> OnError
288290
{
289-
add
290-
{
291-
if (_error == null)
292-
{
293-
BridgeConnector.Socket.On("autoUpdater-error" + GetHashCode(), (message) =>
294-
{
295-
_error(message.ToString());
296-
});
297-
298-
BridgeConnector.Socket.Emit("register-autoUpdater-error-event", GetHashCode());
299-
}
300-
_error += value;
301-
}
302-
remove
303-
{
304-
_error -= value;
305-
306-
if (_error == null)
307-
BridgeConnector.Socket.Off("autoUpdater-error" + GetHashCode());
308-
}
291+
add => ApiEventManager.AddEventWithSuffix("autoUpdater-error", GetHashCode(), _error, value, (args) => args.ToString());
292+
remove => ApiEventManager.RemoveEvent("autoUpdater-error", GetHashCode(), _error, value);
309293
}
310294

311295
private event Action<string> _error;
@@ -315,26 +299,8 @@ public event Action<string> OnError
315299
/// </summary>
316300
public event Action OnCheckingForUpdate
317301
{
318-
add
319-
{
320-
if (_checkingForUpdate == null)
321-
{
322-
BridgeConnector.Socket.On("autoUpdater-checking-for-update" + GetHashCode(), () =>
323-
{
324-
_checkingForUpdate();
325-
});
326-
327-
BridgeConnector.Socket.Emit("register-autoUpdater-checking-for-update-event", GetHashCode());
328-
}
329-
_checkingForUpdate += value;
330-
}
331-
remove
332-
{
333-
_checkingForUpdate -= value;
334-
335-
if (_checkingForUpdate == null)
336-
BridgeConnector.Socket.Off("autoUpdater-checking-for-update" + GetHashCode());
337-
}
302+
add => ApiEventManager.AddEventWithSuffix("autoUpdater-checking-for-update", GetHashCode(), _checkingForUpdate, value);
303+
remove => ApiEventManager.RemoveEvent("autoUpdater-checking-for-update", GetHashCode(), _checkingForUpdate, value);
338304
}
339305

340306
private event Action _checkingForUpdate;
@@ -345,26 +311,8 @@ public event Action OnCheckingForUpdate
345311
/// </summary>
346312
public event Action<UpdateInfo> OnUpdateAvailable
347313
{
348-
add
349-
{
350-
if (_updateAvailable == null)
351-
{
352-
BridgeConnector.Socket.On("autoUpdater-update-available" + GetHashCode(), (updateInfo) =>
353-
{
354-
_updateAvailable(JObject.Parse(updateInfo.ToString()).ToObject<UpdateInfo>());
355-
});
356-
357-
BridgeConnector.Socket.Emit("register-autoUpdater-update-available-event", GetHashCode());
358-
}
359-
_updateAvailable += value;
360-
}
361-
remove
362-
{
363-
_updateAvailable -= value;
364-
365-
if (_updateAvailable == null)
366-
BridgeConnector.Socket.Off("autoUpdater-update-available" + GetHashCode());
367-
}
314+
add => ApiEventManager.AddEventWithSuffix("autoUpdater-update-available", GetHashCode(), _updateAvailable, value, (args) => JObject.Parse(args.ToString()).ToObject<UpdateInfo>());
315+
remove => ApiEventManager.RemoveEvent("autoUpdater-update-available", GetHashCode(), _updateAvailable, value);
368316
}
369317

370318
private event Action<UpdateInfo> _updateAvailable;
@@ -374,26 +322,8 @@ public event Action<UpdateInfo> OnUpdateAvailable
374322
/// </summary>
375323
public event Action<UpdateInfo> OnUpdateNotAvailable
376324
{
377-
add
378-
{
379-
if (_updateNotAvailable == null)
380-
{
381-
BridgeConnector.Socket.On("autoUpdater-update-not-available" + GetHashCode(), (updateInfo) =>
382-
{
383-
_updateNotAvailable(JObject.Parse(updateInfo.ToString()).ToObject<UpdateInfo>());
384-
});
385-
386-
BridgeConnector.Socket.Emit("register-autoUpdater-update-not-available-event", GetHashCode());
387-
}
388-
_updateNotAvailable += value;
389-
}
390-
remove
391-
{
392-
_updateNotAvailable -= value;
393-
394-
if (_updateNotAvailable == null)
395-
BridgeConnector.Socket.Off("autoUpdater-update-not-available" + GetHashCode());
396-
}
325+
add => ApiEventManager.AddEventWithSuffix("autoUpdater-update-not-available", GetHashCode(), _updateNotAvailable, value, (args) => JObject.Parse(args.ToString()).ToObject<UpdateInfo>());
326+
remove => ApiEventManager.RemoveEvent("autoUpdater-update-not-available", GetHashCode(), _updateNotAvailable, value);
397327
}
398328

399329
private event Action<UpdateInfo> _updateNotAvailable;
@@ -403,26 +333,8 @@ public event Action<UpdateInfo> OnUpdateNotAvailable
403333
/// </summary>
404334
public event Action<ProgressInfo> OnDownloadProgress
405335
{
406-
add
407-
{
408-
if (_downloadProgress == null)
409-
{
410-
BridgeConnector.Socket.On("autoUpdater-download-progress" + GetHashCode(), (progressInfo) =>
411-
{
412-
_downloadProgress(JObject.Parse(progressInfo.ToString()).ToObject<ProgressInfo>());
413-
});
414-
415-
BridgeConnector.Socket.Emit("register-autoUpdater-download-progress-event", GetHashCode());
416-
}
417-
_downloadProgress += value;
418-
}
419-
remove
420-
{
421-
_downloadProgress -= value;
422-
423-
if (_downloadProgress == null)
424-
BridgeConnector.Socket.Off("autoUpdater-download-progress" + GetHashCode());
425-
}
336+
add => ApiEventManager.AddEventWithSuffix("autoUpdater-download-progress", GetHashCode(), _downloadProgress, value, (args) => JObject.Parse(args.ToString()).ToObject<ProgressInfo>());
337+
remove => ApiEventManager.RemoveEvent("autoUpdater-download-progress", GetHashCode(), _downloadProgress, value);
426338
}
427339

428340
private event Action<ProgressInfo> _downloadProgress;
@@ -432,26 +344,8 @@ public event Action<ProgressInfo> OnDownloadProgress
432344
/// </summary>
433345
public event Action<UpdateInfo> OnUpdateDownloaded
434346
{
435-
add
436-
{
437-
if (_updateDownloaded == null)
438-
{
439-
BridgeConnector.Socket.On("autoUpdater-update-downloaded" + GetHashCode(), (updateInfo) =>
440-
{
441-
_updateDownloaded(JObject.Parse(updateInfo.ToString()).ToObject<UpdateInfo>());
442-
});
443-
444-
BridgeConnector.Socket.Emit("register-autoUpdater-update-downloaded-event", GetHashCode());
445-
}
446-
_updateDownloaded += value;
447-
}
448-
remove
449-
{
450-
_updateDownloaded -= value;
451-
452-
if (_updateDownloaded == null)
453-
BridgeConnector.Socket.Off("autoUpdater-update-downloaded" + GetHashCode());
454-
}
347+
add => ApiEventManager.AddEventWithSuffix("autoUpdater-update-downloaded", GetHashCode(), _updateDownloaded, value, (args) => JObject.Parse(args.ToString()).ToObject<UpdateInfo>());
348+
remove => ApiEventManager.RemoveEvent("autoUpdater-update-downloaded", GetHashCode(), _updateDownloaded, value);
455349
}
456350

457351
private event Action<UpdateInfo> _updateDownloaded;

src/ElectronNET.API/Common/ApiEventManager.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ namespace ElectronNET.Common;
77

88
internal class ApiEventManager
99
{
10-
internal T Deserialize<T>(Func<T> action)
11-
{
12-
return action.Invoke();
13-
}
14-
1510
internal static void AddEvent(string eventName, object id, Action callback, Action value, string suffix = "")
1611
{
1712
if (callback == null)

0 commit comments

Comments
 (0)