Skip to content

Commit 93f457d

Browse files
committed
Introduce ApiBase and simplify method invocation
1 parent 4671b9b commit 93f457d

File tree

4 files changed

+228
-301
lines changed

4 files changed

+228
-301
lines changed

src/ElectronNET.API/API/ApiBase.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace ElectronNET.API
2+
{
3+
using System.Collections.Concurrent;
4+
using System.Runtime.CompilerServices;
5+
using ElectronNET.Common;
6+
7+
public abstract class ApiBase
8+
{
9+
internal const int PropertyTimeout = 1000;
10+
11+
private readonly string objectName;
12+
private readonly ConcurrentDictionary<string, string> methodMessageNames = new ConcurrentDictionary<string, string>();
13+
14+
public virtual int Id
15+
{
16+
get
17+
{
18+
return -1;
19+
}
20+
21+
// ReSharper disable once ValueParameterNotUsed
22+
protected set
23+
{
24+
}
25+
}
26+
27+
protected abstract string SocketEventCompleteSuffix { get; }
28+
29+
protected ApiBase()
30+
{
31+
this.objectName = this.GetType().Name.LowerFirst();
32+
}
33+
34+
protected void CallMethod0([CallerMemberName] string callerName = null)
35+
{
36+
var messageName = this.methodMessageNames.GetOrAdd(callerName, s => this.objectName + s);
37+
if (this.Id >= 0)
38+
{
39+
BridgeConnector.Socket.Emit(messageName, this.Id);
40+
}
41+
else
42+
{
43+
BridgeConnector.Socket.Emit(messageName);
44+
}
45+
}
46+
47+
protected void CallMethod1(object val1, [CallerMemberName] string callerName = null)
48+
{
49+
var messageName = this.methodMessageNames.GetOrAdd(callerName, s => this.objectName + s);
50+
if (this.Id >= 0)
51+
{
52+
BridgeConnector.Socket.Emit(messageName, this.Id, val1);
53+
}
54+
else
55+
{
56+
BridgeConnector.Socket.Emit(messageName, val1);
57+
}
58+
}
59+
60+
protected void CallMethod2(object val1, object val2, [CallerMemberName] string callerName = null)
61+
{
62+
var messageName = this.methodMessageNames.GetOrAdd(callerName, s => this.objectName + s);
63+
if (this.Id >= 0)
64+
{
65+
BridgeConnector.Socket.Emit(messageName, this.Id, val1, val2);
66+
}
67+
else
68+
{
69+
BridgeConnector.Socket.Emit(messageName, val1, val2);
70+
}
71+
}
72+
73+
protected void CallMethod3(object val1, object val2, object val3, [CallerMemberName] string callerName = null)
74+
{
75+
var messageName = this.methodMessageNames.GetOrAdd(callerName, s => this.objectName + s);
76+
if (this.Id >= 0)
77+
{
78+
BridgeConnector.Socket.Emit(messageName, this.Id, val1, val2, val3);
79+
}
80+
else
81+
{
82+
BridgeConnector.Socket.Emit(messageName, val1, val2, val3);
83+
}
84+
}
85+
}
86+
}

src/ElectronNET.API/API/App.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ namespace ElectronNET.API
1616
/// <summary>
1717
/// Control your application's event lifecycle.
1818
/// </summary>
19-
public sealed class App
19+
public sealed class App : ApiBase
2020
{
21+
protected override string SocketEventCompleteSuffix => "Completed";
22+
2123
/// <summary>
2224
/// Emitted when all windows have been closed.
2325
/// <para/>
@@ -430,7 +432,7 @@ internal static App Instance
430432
/// </summary>
431433
public void Quit()
432434
{
433-
BridgeConnector.Socket.Emit("appQuit");
435+
this.CallMethod0();
434436
}
435437

436438
/// <summary>
@@ -440,7 +442,7 @@ public void Quit()
440442
/// <param name="exitCode">Exits immediately with exitCode. exitCode defaults to 0.</param>
441443
public void Exit(int exitCode = 0)
442444
{
443-
BridgeConnector.Socket.Emit("appExit", exitCode);
445+
this.CallMethod1(exitCode);
444446
}
445447

446448
public void DisposeSocket()
@@ -460,7 +462,7 @@ public void DisposeSocket()
460462
/// </summary>
461463
public void Relaunch()
462464
{
463-
BridgeConnector.Socket.Emit("appRelaunch");
465+
this.CallMethod0();
464466
}
465467

466468
/// <summary>
@@ -478,7 +480,7 @@ public void Relaunch()
478480
/// <param name="relaunchOptions">Options for the relaunch.</param>
479481
public void Relaunch(RelaunchOptions relaunchOptions)
480482
{
481-
BridgeConnector.Socket.Emit("appRelaunch", JObject.FromObject(relaunchOptions, _jsonSerializer));
483+
this.CallMethod1(JObject.FromObject(relaunchOptions, _jsonSerializer));
482484
}
483485

484486
/// <summary>
@@ -487,7 +489,7 @@ public void Relaunch(RelaunchOptions relaunchOptions)
487489
/// </summary>
488490
public void Focus()
489491
{
490-
BridgeConnector.Socket.Emit("appFocus");
492+
this.CallMethod0();
491493
}
492494

493495
/// <summary>
@@ -498,23 +500,23 @@ public void Focus()
498500
/// </summary>
499501
public void Focus(FocusOptions focusOptions)
500502
{
501-
BridgeConnector.Socket.Emit("appFocus", JObject.FromObject(focusOptions, _jsonSerializer));
503+
this.CallMethod1(JObject.FromObject(focusOptions, _jsonSerializer));
502504
}
503505

504506
/// <summary>
505507
/// Hides all application windows without minimizing them.
506508
/// </summary>
507509
public void Hide()
508510
{
509-
BridgeConnector.Socket.Emit("appHide");
511+
this.CallMethod0();
510512
}
511513

512514
/// <summary>
513515
/// Shows application windows after they were hidden. Does not automatically focus them.
514516
/// </summary>
515517
public void Show()
516518
{
517-
BridgeConnector.Socket.Emit("appShow");
519+
this.CallMethod0();
518520
}
519521

520522
/// <summary>
@@ -550,7 +552,7 @@ public async Task<string> GetAppPathAsync(CancellationToken cancellationToken =
550552
/// <param name="path">A custom path for your logs. Must be absolute.</param>
551553
public void SetAppLogsPath(string path)
552554
{
553-
BridgeConnector.Socket.Emit("appSetAppLogsPath", path);
555+
this.CallMethod1(path);
554556
}
555557

556558
/// <summary>
@@ -596,7 +598,7 @@ public async Task<string> GetPathAsync(PathName pathName, CancellationToken canc
596598
/// </summary>
597599
public void SetPath(PathName name, string path)
598600
{
599-
BridgeConnector.Socket.Emit("appSetPath", name.GetDescription(), path);
601+
this.CallMethod2(name.GetDescription(), path);
600602
}
601603

602604
/// <summary>
@@ -659,15 +661,15 @@ public async Task<string> GetLocaleAsync(CancellationToken cancellationToken = d
659661
/// <param name="path">Path to add.</param>
660662
public void AddRecentDocument(string path)
661663
{
662-
BridgeConnector.Socket.Emit("appAddRecentDocument", path);
664+
this.CallMethod1(path);
663665
}
664666

665667
/// <summary>
666668
/// Clears the recent documents list.
667669
/// </summary>
668670
public void ClearRecentDocuments()
669671
{
670-
BridgeConnector.Socket.Emit("appClearRecentDocuments");
672+
this.CallMethod0();
671673
}
672674

673675
/// <summary>
@@ -975,7 +977,7 @@ public async Task<JumpListSettings> GetJumpListSettingsAsync(CancellationToken c
975977
/// <param name="categories">Array of <see cref="JumpListCategory"/> objects.</param>
976978
public void SetJumpList(JumpListCategory[] categories)
977979
{
978-
BridgeConnector.Socket.Emit("appSetJumpList", JArray.FromObject(categories, _jsonSerializer));
980+
this.CallMethod1(JArray.FromObject(categories, _jsonSerializer));
979981
}
980982

981983
/// <summary>
@@ -1035,7 +1037,7 @@ public async Task<bool> RequestSingleInstanceLockAsync(Action<string[], string>
10351037
/// </summary>
10361038
public void ReleaseSingleInstanceLock()
10371039
{
1038-
BridgeConnector.Socket.Emit("appReleaseSingleInstanceLock");
1040+
this.CallMethod0();
10391041
}
10401042

10411043
/// <summary>
@@ -1090,7 +1092,7 @@ public void SetUserActivity(string type, object userInfo)
10901092
/// </param>
10911093
public void SetUserActivity(string type, object userInfo, string webpageUrl)
10921094
{
1093-
BridgeConnector.Socket.Emit("appSetUserActivity", type, userInfo, webpageUrl);
1095+
this.CallMethod3(type, userInfo, webpageUrl);
10941096
}
10951097

10961098
/// <summary>
@@ -1122,15 +1124,15 @@ public async Task<string> GetCurrentActivityTypeAsync(CancellationToken cancella
11221124
/// </summary>
11231125
public void InvalidateCurrentActivity()
11241126
{
1125-
BridgeConnector.Socket.Emit("appInvalidateCurrentActivity");
1127+
this.CallMethod0();
11261128
}
11271129

11281130
/// <summary>
11291131
/// Marks the current <see href="https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/Handoff/HandoffFundamentals/HandoffFundamentals.html">Handoff</see> user activity as inactive without invalidating it.
11301132
/// </summary>
11311133
public void ResignCurrentActivity()
11321134
{
1133-
BridgeConnector.Socket.Emit("appResignCurrentActivity");
1135+
this.CallMethod0();
11341136
}
11351137

11361138
/// <summary>
@@ -1139,7 +1141,7 @@ public void ResignCurrentActivity()
11391141
/// <param name="id">Model Id.</param>
11401142
public void SetAppUserModelId(string id)
11411143
{
1142-
BridgeConnector.Socket.Emit("appSetAppUserModelId", id);
1144+
this.CallMethod1(id);
11431145
}
11441146

11451147
/// TODO: Check new parameter which is a function [App.ImportCertificate]
@@ -1365,7 +1367,7 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
13651367
/// <param name="loginSettings"></param>
13661368
public void SetLoginItemSettings(LoginSettings loginSettings)
13671369
{
1368-
BridgeConnector.Socket.Emit("appSetLoginItemSettings", JObject.FromObject(loginSettings, _jsonSerializer));
1370+
this.CallMethod1(JObject.FromObject(loginSettings, _jsonSerializer));
13691371
}
13701372

13711373
/// <summary>
@@ -1406,7 +1408,7 @@ public async Task<bool> IsAccessibilitySupportEnabledAsync(CancellationToken can
14061408
/// <param name="enabled">Enable or disable <see href="https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/the-accessibility-tree">accessibility tree</see> rendering.</param>
14071409
public void SetAccessibilitySupportEnabled(bool enabled)
14081410
{
1409-
BridgeConnector.Socket.Emit("appSetAccessibilitySupportEnabled", enabled);
1411+
this.CallMethod1(enabled);
14101412
}
14111413

14121414
/// <summary>
@@ -1415,7 +1417,7 @@ public void SetAccessibilitySupportEnabled(bool enabled)
14151417
/// </summary>
14161418
public void ShowAboutPanel()
14171419
{
1418-
BridgeConnector.Socket.Emit("appShowAboutPanel");
1420+
this.CallMethod0();
14191421
}
14201422

14211423
/// <summary>
@@ -1431,7 +1433,7 @@ public void ShowAboutPanel()
14311433
/// <param name="options">About panel options.</param>
14321434
public void SetAboutPanelOptions(AboutPanelOptions options)
14331435
{
1434-
BridgeConnector.Socket.Emit("appSetAboutPanelOptions", JObject.FromObject(options, _jsonSerializer));
1436+
this.CallMethod1(JObject.FromObject(options, _jsonSerializer));
14351437
}
14361438

14371439
/// <summary>

0 commit comments

Comments
 (0)