|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using Quobject.EngineIoClientDotNet.ComponentEmitter; |
| 4 | + |
| 5 | +namespace ElectronNET.API |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Generic Event Consumers for Electron Modules |
| 9 | + /// </summary> |
| 10 | + internal class Events |
| 11 | + { |
| 12 | + private static Events _events; |
| 13 | + private static object _syncRoot = new object(); |
| 14 | + private TextInfo _ti = new CultureInfo("en-US", false).TextInfo; |
| 15 | + private Events() |
| 16 | + { |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + public static Events Instance |
| 21 | + { |
| 22 | + get |
| 23 | + { |
| 24 | + if (_events == null) |
| 25 | + { |
| 26 | + lock (_syncRoot) |
| 27 | + { |
| 28 | + if (_events == null) |
| 29 | + { |
| 30 | + _events = new Events(); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return _events; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Subscribe to an unmapped electron event. |
| 41 | + /// </summary> |
| 42 | + /// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param> |
| 43 | + /// <param name="eventName">The name of the event</param> |
| 44 | + /// <param name="fn">The event handler</param> |
| 45 | + public void On(string moduleName, string eventName, Action fn) |
| 46 | + => On(moduleName, eventName, new ListenerImpl(fn)); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Subscribe to an unmapped electron event. |
| 50 | + /// </summary> |
| 51 | + /// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param> |
| 52 | + /// <param name="eventName">The name of the event</param> |
| 53 | + /// <param name="fn">The event handler</param> |
| 54 | + public void On(string moduleName, string eventName, Action<object> fn) |
| 55 | + => On(moduleName, eventName, new ListenerImpl(fn)); |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Subscribe to an unmapped electron event. |
| 59 | + /// </summary> |
| 60 | + /// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param> |
| 61 | + /// <param name="eventName">The name of the event</param> |
| 62 | + /// <param name="fn">The event handler</param> |
| 63 | + private void On(string moduleName, string eventName, IListener fn) |
| 64 | + { |
| 65 | + var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed"; |
| 66 | + var subscriber = $"register-{moduleName}-on-event"; |
| 67 | + |
| 68 | + BridgeConnector.Socket.On(listener, fn); |
| 69 | + BridgeConnector.Socket.Emit(subscriber, eventName, listener); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Subscribe to an unmapped electron event. |
| 74 | + /// </summary> |
| 75 | + /// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param> |
| 76 | + /// <param name="eventName">The name of the event</param> |
| 77 | + /// <param name="fn">The event handler</param> |
| 78 | + public void Once(string moduleName, string eventName, Action fn) |
| 79 | + => Once(moduleName, eventName, new ListenerImpl(fn)); |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Subscribe to an unmapped electron event. |
| 83 | + /// </summary> |
| 84 | + /// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param> |
| 85 | + /// <param name="eventName">The name of the event</param> |
| 86 | + /// <param name="fn">The event handler</param> |
| 87 | + public void Once(string moduleName, string eventName, Action<object> fn) |
| 88 | + => Once(moduleName, eventName, new ListenerImpl(fn)); |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Subscribe to an unmapped electron event. |
| 92 | + /// </summary> |
| 93 | + /// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param> |
| 94 | + /// <param name="eventName">The name of the event</param> |
| 95 | + /// <param name="fn">The event handler</param> |
| 96 | + private void Once(string moduleName, string eventName, IListener fn) |
| 97 | + { |
| 98 | + var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed"; |
| 99 | + var subscriber = $"register-{moduleName}-once-event"; |
| 100 | + BridgeConnector.Socket.Once(listener, fn); |
| 101 | + BridgeConnector.Socket.Emit(subscriber, eventName, listener); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | +} |
0 commit comments