using System; using System.Globalization; namespace ElectronNET.API { /// /// Generic Event Consumers for Electron Modules /// internal class Events { private static Events _events; private static object _syncRoot = new object(); private TextInfo _ti = 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 void On(string moduleName, string eventName, Action fn) => On(moduleName, eventName, _ => fn()); /// /// 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 void On(string moduleName, string eventName, Action fn) { var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed"; var subscriber = $"register-{moduleName}-on-event"; BridgeConnector.On(listener, fn); BridgeConnector.Emit(subscriber, eventName, listener); } /// /// 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 void Once(string moduleName, string eventName, Action fn) => Once(moduleName, eventName, _ => fn()); /// /// 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 void Once(string moduleName, string eventName, Action fn) { var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed"; var subscriber = $"register-{moduleName}-once-event"; BridgeConnector.Once(listener, fn); BridgeConnector.Emit(subscriber, eventName, listener); } } }