mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-02-04 05:34:51 +00:00
ElectronNET.API: Re-add moved types
This commit is contained in:
15
src/ElectronNET.API/Bridge/BridgeConnector.cs
Normal file
15
src/ElectronNET.API/Bridge/BridgeConnector.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma warning disable IDE0130 // Namespace does not match folder structure
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
internal static class BridgeConnector
|
||||
{
|
||||
public static SocketIoFacade Socket
|
||||
{
|
||||
get
|
||||
{
|
||||
return ElectronNetRuntime.GetSocket();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
src/ElectronNET.API/Bridge/Events.cs
Normal file
101
src/ElectronNET.API/Bridge/Events.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
#pragma warning disable IDE0130 // Namespace does not match folder structure
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// Generic Event Consumers for Electron Modules
|
||||
/// </summary>
|
||||
internal class Events
|
||||
{
|
||||
private static Events _events;
|
||||
private static readonly object SyncRoot = new();
|
||||
private readonly TextInfo _textInfo = new CultureInfo("en-US", false).TextInfo;
|
||||
|
||||
private Events()
|
||||
{ }
|
||||
|
||||
public static Events Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_events == null)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
if (_events == null)
|
||||
{
|
||||
_events = new Events();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _events;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to an unmapped electron event.
|
||||
/// </summary>
|
||||
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
|
||||
/// <param name="eventName">The name of the event</param>
|
||||
/// <param name="action">The event handler</param>
|
||||
public async Task On(string moduleName, string eventName, Action action)
|
||||
{
|
||||
var listener = $"{moduleName}{_textInfo.ToTitleCase(eventName)}Completed";
|
||||
var subscriber = $"register-{moduleName}-on-event";
|
||||
|
||||
BridgeConnector.Socket.On(listener, action);
|
||||
await BridgeConnector.Socket.Emit(subscriber, eventName, listener).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to an unmapped electron event.
|
||||
/// </summary>
|
||||
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
|
||||
/// <param name="eventName">The name of the event</param>
|
||||
/// <param name="action">The event handler</param>
|
||||
public async Task On<T>(string moduleName, string eventName, Action<T> action)
|
||||
{
|
||||
var listener = $"{moduleName}{_textInfo.ToTitleCase(eventName)}Completed";
|
||||
var subscriber = $"register-{moduleName}-on-event";
|
||||
|
||||
BridgeConnector.Socket.On(listener, action);
|
||||
await BridgeConnector.Socket.Emit(subscriber, eventName, listener).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to an unmapped electron event.
|
||||
/// </summary>
|
||||
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
|
||||
/// <param name="eventName">The name of the event</param>
|
||||
/// <param name="fn">The event handler</param>
|
||||
public async Task Once(string moduleName, string eventName, Action action)
|
||||
{
|
||||
var listener = $"{moduleName}{_textInfo.ToTitleCase(eventName)}Completed";
|
||||
var subscriber = $"register-{moduleName}-once-event";
|
||||
BridgeConnector.Socket.Once(listener, action);
|
||||
await BridgeConnector.Socket.Emit(subscriber, eventName, listener).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to an unmapped electron event.
|
||||
/// </summary>
|
||||
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
|
||||
/// <param name="eventName">The name of the event</param>
|
||||
/// <param name="action">The event handler</param>
|
||||
public async Task Once<T>(string moduleName, string eventName, Action<T> action)
|
||||
{
|
||||
var listener = $"{moduleName}{_textInfo.ToTitleCase(eventName)}Completed";
|
||||
var subscriber = $"register-{moduleName}-once-event";
|
||||
BridgeConnector.Socket.Once(listener, action);
|
||||
await BridgeConnector.Socket.Emit(subscriber, eventName, listener).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
115
src/ElectronNET.API/Bridge/SocketIOFacade.cs
Normal file
115
src/ElectronNET.API/Bridge/SocketIOFacade.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
#pragma warning disable IDE0130 // Namespace does not match folder structure
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace ElectronNET.API;
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using SocketIO.Serializer.NewtonsoftJson;
|
||||
using SocketIO = SocketIOClient.SocketIO;
|
||||
|
||||
internal class SocketIoFacade
|
||||
{
|
||||
private readonly SocketIO _socket;
|
||||
|
||||
public SocketIoFacade(string uri)
|
||||
{
|
||||
_socket = new SocketIO(uri);
|
||||
var jsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
DefaultValueHandling = DefaultValueHandling.Ignore
|
||||
});
|
||||
|
||||
_socket.Serializer = jsonSerializer;
|
||||
}
|
||||
|
||||
public event EventHandler BridgeDisconnected;
|
||||
|
||||
public event EventHandler BridgeConnected;
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
_socket.OnError += (sender, e) =>
|
||||
{
|
||||
Console.WriteLine($"BridgeConnector Error: {sender} {e}");
|
||||
};
|
||||
|
||||
_socket.OnConnected += (_, _) =>
|
||||
{
|
||||
Console.WriteLine("BridgeConnector connected!");
|
||||
this.BridgeConnected?.Invoke(this, EventArgs.Empty);
|
||||
};
|
||||
|
||||
_socket.OnDisconnected += (_, _) =>
|
||||
{
|
||||
Console.WriteLine("BridgeConnector disconnected!");
|
||||
this.BridgeDisconnected?.Invoke(this, EventArgs.Empty);
|
||||
};
|
||||
|
||||
_socket.ConnectAsync().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public void On(string eventName, Action action)
|
||||
{
|
||||
_socket.On(eventName, _ =>
|
||||
{
|
||||
Task.Run(action);
|
||||
});
|
||||
}
|
||||
|
||||
public void On<T>(string eventName, Action<T> action)
|
||||
{
|
||||
_socket.On(eventName, response =>
|
||||
{
|
||||
var value = response.GetValue<T>();
|
||||
Task.Run(() => action(value));
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Remove this method when SocketIoClient supports object deserialization
|
||||
public void On(string eventName, Action<object> action)
|
||||
{
|
||||
_socket.On(eventName, response =>
|
||||
{
|
||||
var value = response.GetValue<object>();
|
||||
////Console.WriteLine($"Called Event {eventName} - data {value}");
|
||||
Task.Run(() => action(value));
|
||||
});
|
||||
}
|
||||
|
||||
public void Once(string eventName, Action action)
|
||||
{
|
||||
_socket.On(eventName, _ =>
|
||||
{
|
||||
_socket.Off(eventName);
|
||||
Task.Run(action);
|
||||
});
|
||||
}
|
||||
|
||||
public void Once<T>(string eventName, Action<T> action)
|
||||
{
|
||||
_socket.On(eventName, (socketIoResponse) =>
|
||||
{
|
||||
_socket.Off(eventName);
|
||||
Task.Run(() => action(socketIoResponse.GetValue<T>()));
|
||||
});
|
||||
}
|
||||
|
||||
public void Off(string eventName)
|
||||
{
|
||||
_socket.Off(eventName);
|
||||
}
|
||||
|
||||
public async Task Emit(string eventName, params object[] args)
|
||||
{
|
||||
await _socket.EmitAsync(eventName, args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public void DisposeSocket()
|
||||
{
|
||||
_socket.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
using System;
|
||||
namespace ElectronNET.Converter;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ElectronNET.API.Entities;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace ElectronNET.API.Converter;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user