-
-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathEvents.cs
More file actions
99 lines (87 loc) · 3.84 KB
/
Events.cs
File metadata and controls
99 lines (87 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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="action">The action.</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);
}
}
}