2025-11-09 15:15:52 +01:00
|
|
|
using ElectronNET.API.Serialization;
|
2019-01-05 02:17:31 +01:00
|
|
|
using System;
|
2025-11-09 12:05:07 +01:00
|
|
|
using System.Text.Json;
|
2019-01-05 02:17:31 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
{
|
2019-05-15 23:56:53 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Allows you to execute native JavaScript/TypeScript code from the host process.
|
|
|
|
|
///
|
2025-11-15 08:05:31 +01:00
|
|
|
/// It is only possible if the Electron.NET CLI has previously added an
|
2019-05-15 23:56:53 +02:00
|
|
|
/// ElectronHostHook directory:
|
|
|
|
|
/// <c>electronize add HostHook</c>
|
|
|
|
|
/// </summary>
|
2019-01-05 02:17:31 +01:00
|
|
|
public sealed class HostHook
|
|
|
|
|
{
|
|
|
|
|
private static HostHook _electronHostHook;
|
|
|
|
|
private static object _syncRoot = new object();
|
2025-11-09 03:50:24 +01:00
|
|
|
private string oneCallguid = Guid.NewGuid().ToString();
|
2019-01-05 02:17:31 +01:00
|
|
|
|
2025-11-09 03:50:24 +01:00
|
|
|
internal HostHook()
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-01-05 02:17:31 +01:00
|
|
|
|
|
|
|
|
internal static HostHook Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_electronHostHook == null)
|
|
|
|
|
{
|
|
|
|
|
lock (_syncRoot)
|
|
|
|
|
{
|
|
|
|
|
if (_electronHostHook == null)
|
|
|
|
|
{
|
|
|
|
|
_electronHostHook = new HostHook();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _electronHostHook;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 23:56:53 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Execute native JavaScript/TypeScript code.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="socketEventName">Socket name registered on the host.</param>
|
|
|
|
|
/// <param name="arguments">Optional parameters.</param>
|
2019-01-05 02:17:31 +01:00
|
|
|
public void Call(string socketEventName, params dynamic[] arguments)
|
|
|
|
|
{
|
2025-11-15 08:05:31 +01:00
|
|
|
BridgeConnector.Socket.Once<string>(socketEventName + "Error" + oneCallguid, (result) => { Electron.Dialog.ShowErrorBox("Host Hook Exception", result); });
|
2019-01-11 02:42:32 +01:00
|
|
|
|
2019-01-11 10:00:00 +01:00
|
|
|
BridgeConnector.Socket.Emit(socketEventName, arguments, oneCallguid);
|
2019-01-05 02:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 23:56:53 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Execute native JavaScript/TypeScript code.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Results from the executed host code.</typeparam>
|
|
|
|
|
/// <param name="socketEventName">Socket name registered on the host.</param>
|
|
|
|
|
/// <param name="arguments">Optional parameters.</param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-05 02:17:31 +01:00
|
|
|
public Task<T> CallAsync<T>(string socketEventName, params dynamic[] arguments)
|
|
|
|
|
{
|
2025-11-10 10:40:51 +01:00
|
|
|
var tcs = new TaskCompletionSource<T>();
|
2019-01-05 02:17:31 +01:00
|
|
|
string guid = Guid.NewGuid().ToString();
|
|
|
|
|
|
2025-11-10 10:40:51 +01:00
|
|
|
BridgeConnector.Socket.Once<string>(socketEventName + "Error" + guid, (result) =>
|
2019-01-11 02:42:32 +01:00
|
|
|
{
|
2025-11-09 15:46:33 +01:00
|
|
|
Electron.Dialog.ShowErrorBox("Host Hook Exception", result);
|
2025-11-10 10:40:51 +01:00
|
|
|
tcs.SetException(new Exception($"Host Hook Exception {result}"));
|
2019-01-11 02:42:32 +01:00
|
|
|
});
|
|
|
|
|
|
2025-11-10 10:40:51 +01:00
|
|
|
BridgeConnector.Socket.Once<JsonElement>(socketEventName + "Complete" + guid, (result) =>
|
2019-01-05 02:17:31 +01:00
|
|
|
{
|
2019-01-11 10:00:00 +01:00
|
|
|
BridgeConnector.Socket.Off(socketEventName + "Error" + guid);
|
2020-06-12 13:32:26 +02:00
|
|
|
T data = default;
|
2019-01-05 02:17:31 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-11-09 15:15:52 +01:00
|
|
|
data = result.Deserialize<T>(ElectronJson.Options);
|
2019-01-05 02:17:31 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
2025-11-10 10:40:51 +01:00
|
|
|
tcs.SetException(exception);
|
2019-01-05 02:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-10 10:40:51 +01:00
|
|
|
tcs.SetResult(data);
|
2019-01-05 02:17:31 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
BridgeConnector.Socket.Emit(socketEventName, arguments, guid);
|
|
|
|
|
|
2025-11-10 10:40:51 +01:00
|
|
|
return tcs.Task;
|
2019-01-05 02:17:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 08:05:31 +01:00
|
|
|
}
|