prototype of electron host hook service and api implementation

This commit is contained in:
Gregor Biswanger
2019-01-05 02:17:31 +01:00
parent cad371c221
commit 63c2bcdf7c
46 changed files with 2875 additions and 504 deletions

View File

@@ -67,8 +67,7 @@ namespace ElectronNET.API
BridgeConnector.Socket.Emit("showOpenDialog",
JObject.FromObject(browserWindow, _jsonSerializer),
JObject.FromObject(options, _jsonSerializer),
guid);
JObject.FromObject(options, _jsonSerializer), guid);
return taskCompletionSource.Task;
}

View File

@@ -59,5 +59,8 @@
/// Perform copy and paste operations on the system clipboard.
/// </summary>
public static Clipboard Clipboard { get { return Clipboard.Instance; } }
public static HostHook HostHook { get { return HostHook.Instance; } }
}
}

View File

@@ -0,0 +1,93 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Threading.Tasks;
namespace ElectronNET.API
{
public sealed class HostHook
{
private static HostHook _electronHostHook;
private static object _syncRoot = new object();
internal HostHook() { }
internal static HostHook Instance
{
get
{
if (_electronHostHook == null)
{
lock (_syncRoot)
{
if (_electronHostHook == null)
{
_electronHostHook = new HostHook();
}
}
}
return _electronHostHook;
}
}
public void Call(string socketEventName, params dynamic[] arguments)
{
BridgeConnector.Socket.Emit(socketEventName, arguments);
}
public Task<T> CallAsync<T>(string socketEventName, params dynamic[] arguments)
{
var taskCompletionSource = new TaskCompletionSource<T>();
string guid = Guid.NewGuid().ToString();
BridgeConnector.Socket.On(socketEventName + "Complete" + guid, (result) =>
{
BridgeConnector.Socket.Off(socketEventName + "Complete" + guid);
T data;
try
{
if (result.GetType().IsValueType || result is string)
{
data = (T)result;
}
else
{
var token = JToken.Parse(result.ToString());
if (token is JArray)
{
data = token.ToObject<T>();
}
else if (token is JObject)
{
data = token.ToObject<T>();
}
else
{
data = (T)result;
}
}
}
catch (Exception exception)
{
throw new InvalidCastException("Return value does not match with the generic type.", exception);
}
taskCompletionSource.SetResult(data);
});
BridgeConnector.Socket.Emit(socketEventName, arguments, guid);
return taskCompletionSource.Task;
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}