Skip to content

Commit 63c2bcd

Browse files
prototype of electron host hook service and api implementation
1 parent cad371c commit 63c2bcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2875
-504
lines changed

ElectronNET.API/Dialog.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public Task<string[]> ShowOpenDialogAsync(BrowserWindow browserWindow, OpenDialo
6767

6868
BridgeConnector.Socket.Emit("showOpenDialog",
6969
JObject.FromObject(browserWindow, _jsonSerializer),
70-
JObject.FromObject(options, _jsonSerializer),
71-
guid);
70+
JObject.FromObject(options, _jsonSerializer), guid);
7271

7372
return taskCompletionSource.Task;
7473
}

ElectronNET.API/Electron.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,8 @@ public static class Electron
5959
/// Perform copy and paste operations on the system clipboard.
6060
/// </summary>
6161
public static Clipboard Clipboard { get { return Clipboard.Instance; } }
62+
63+
64+
public static HostHook HostHook { get { return HostHook.Instance; } }
6265
}
6366
}

ElectronNET.API/HostHook.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using Newtonsoft.Json.Serialization;
4+
using System;
5+
using System.Threading.Tasks;
6+
7+
namespace ElectronNET.API
8+
{
9+
public sealed class HostHook
10+
{
11+
private static HostHook _electronHostHook;
12+
private static object _syncRoot = new object();
13+
14+
internal HostHook() { }
15+
16+
internal static HostHook Instance
17+
{
18+
get
19+
{
20+
if (_electronHostHook == null)
21+
{
22+
lock (_syncRoot)
23+
{
24+
if (_electronHostHook == null)
25+
{
26+
_electronHostHook = new HostHook();
27+
}
28+
}
29+
}
30+
31+
return _electronHostHook;
32+
}
33+
}
34+
35+
public void Call(string socketEventName, params dynamic[] arguments)
36+
{
37+
BridgeConnector.Socket.Emit(socketEventName, arguments);
38+
}
39+
40+
public Task<T> CallAsync<T>(string socketEventName, params dynamic[] arguments)
41+
{
42+
var taskCompletionSource = new TaskCompletionSource<T>();
43+
string guid = Guid.NewGuid().ToString();
44+
45+
BridgeConnector.Socket.On(socketEventName + "Complete" + guid, (result) =>
46+
{
47+
BridgeConnector.Socket.Off(socketEventName + "Complete" + guid);
48+
T data;
49+
50+
try
51+
{
52+
if (result.GetType().IsValueType || result is string)
53+
{
54+
data = (T)result;
55+
}
56+
else
57+
{
58+
var token = JToken.Parse(result.ToString());
59+
if (token is JArray)
60+
{
61+
data = token.ToObject<T>();
62+
}
63+
else if (token is JObject)
64+
{
65+
data = token.ToObject<T>();
66+
}
67+
else
68+
{
69+
data = (T)result;
70+
}
71+
}
72+
}
73+
catch (Exception exception)
74+
{
75+
throw new InvalidCastException("Return value does not match with the generic type.", exception);
76+
}
77+
78+
taskCompletionSource.SetResult(data);
79+
});
80+
81+
BridgeConnector.Socket.Emit(socketEventName, arguments, guid);
82+
83+
return taskCompletionSource.Task;
84+
}
85+
86+
private JsonSerializer _jsonSerializer = new JsonSerializer()
87+
{
88+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
89+
NullValueHandling = NullValueHandling.Ignore,
90+
DefaultValueHandling = DefaultValueHandling.Ignore
91+
};
92+
}
93+
}

ElectronNET.Host/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
bin

ElectronNET.Host/.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"type": "node",
99
"request": "launch",
1010
"name": "Launch Electron App",
11-
"program": "${workspaceFolder}\\main.js",
12-
"runtimeExecutable": "${workspaceFolder}\\node_modules\\.bin\\electron",
11+
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
12+
"program": "${workspaceFolder}/main.js",
1313
"sourceMaps": true
1414
}
1515
]

0 commit comments

Comments
 (0)