#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;
///
/// Generic Event Consumers for Electron Modules
///
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;
}
}
///
/// Subscribe to an unmapped electron event.
///
/// The name of the module, e.g. app, dock, etc...
/// The name of the event
/// The event handler
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);
}
///
/// Subscribe to an unmapped electron event.
///
/// The name of the module, e.g. app, dock, etc...
/// The name of the event
/// The event handler
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);
}
/// Subscribe to an unmapped electron event.
/// The name of the module, e.g. app, dock, etc...
/// The name of the event
/// The action.
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);
}
///
/// Subscribe to an unmapped electron event.
///
/// The name of the module, e.g. app, dock, etc...
/// The name of the event
/// The event handler
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);
}
}
}