/
IBlockingEvent.cs
193 lines (173 loc) · 9.5 KB
/
IBlockingEvent.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Threading.Tasks;
namespace AGS.API
{
/// <summary>
/// This allows you to subscribe to an event with the option of claiming the event, so that other subscribers in the list will not receive the event.
///
/// <example>
/// <code language="lang-csharp">
/// myEvent.Subscribe(onEvent1);
/// myEvent.Subscribe(onEvent2);
///
/// void onEvent1(ref ClaimEventToken token)
/// {
/// token.Claimed = true; //We claimed the event, "onEvent2" will not get called.
/// }
/// </code>
/// </example>
/// </summary>
public delegate void ClaimableCallback(ref ClaimEventToken token);
/// <summary>
/// This allows you to subscribe to an event with the option of claiming the event, so that other subscribers in the list will not receive the event.
///
/// For an example- <see cref="ClaimableCallback"/>.
/// </summary>
public delegate void ClaimableCallbackWithArgs<TEventArgs>(TEventArgs args, ref ClaimEventToken token);
/// <summary>
/// In a scenario where an event has multiple subscribers, the callback priority affects which subscriber gets the callback first.
/// High priority subscribers will receive the event before normal priority (the default) subscribers, and low priority will receive the event last.
///
/// Note: For 2 subscribers with the same callback priority, there's no guarantee regarding who gets the event first.
/// </summary>
public enum CallbackPriority : byte
{
/// <summary>
/// Low priority- will receive the event last.
/// </summary>
Low,
/// <summary>
/// Normal priority.
/// </summary>
Normal,
/// <summary>
/// High priority- will receive the event first.
/// </summary>
High,
}
/// <summary>
/// Represents an event which can be subscribed and invoked synchronously.
/// An event is a notification for something that has happened.
/// Interested parties can subscribe to the event and be notified when it triggers (https://en.wikipedia.org/wiki/Event-driven_programming).
/// </summary>
public interface IBlockingEvent<TEventArgs> : IDisposable
{
/// <summary>
/// Gets the number of subscribers to the event.
/// </summary>
/// <value>The subscribers count.</value>
int SubscribersCount { get; }
/// <summary>
/// Subscribe the specified callback to the event.
/// Once subscribed, whenever the event happens this callback will be called.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Subscribe(Action<TEventArgs> callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Subscribe the specified callback to the event.
/// Once subscribed, whenever the event happens this callback will be called.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Subscribe(Action callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Unsubscribe the specified callback from the event.
/// This will stops notifications to call this callback.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Unsubscribe(Action<TEventArgs> callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Unsubscribe the specified callback from the event.
/// This will stops notifications to call this callback.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Unsubscribe(Action callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Subscribe the specified callback to the event.
/// Once subscribed, whenever the event happens this callback will be called.
///
/// In addition, this specific overload allows you to claim the event so that subscribers which follow you on the list will not receive the event.
/// For an example- <see cref="ClaimableCallback"/>.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Subscribe(ClaimableCallbackWithArgs<TEventArgs> callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Unsubscribe the specified callback from the event.
/// This will stops notifications to call this callback.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Unsubscribe(ClaimableCallbackWithArgs<TEventArgs> callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Asynchronously wait until the event fires and the specific condition applies.
/// </summary>
/// <returns>The task to be awaited.</returns>
/// <param name="condition">The condition we are waiting to apply before moving on.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
Task WaitUntilAsync(Predicate<TEventArgs> condition, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Invoke the event synchronously (i.e will wait for all subscribers to process the event before moving on).
/// </summary>
/// <param name="args">Event arguments which can be used to provide additional data.</param>
void Invoke(TEventArgs args);
}
/// <summary>
/// Represents an event which can be subscribed and invoked synchronously.
/// An event is a notification for something that has happened.
/// Interested parties can subscribe to the event and be notified when it triggers (https://en.wikipedia.org/wiki/Event-driven_programming).
/// </summary>
public interface IBlockingEvent : IDisposable
{
/// <summary>
/// Gets the number of subscribers to the event.
/// </summary>
/// <value>The subscribers count.</value>
int SubscribersCount { get; }
/// <summary>
/// Subscribe the specified callback to the event.
/// Once subscribed, whenever the event happens this callback will be called.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Subscribe(Action callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Unsubscribe the specified callback from the event.
/// This will stops notifications to call this callback.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Unsubscribe(Action callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Subscribe the specified callback to the event.
/// Once subscribed, whenever the event happens this callback will be called.
///
/// In addition, this specific overload allows you to claim the event so that subscribers which follow you on the list will not receive the event.
/// For an example- <see cref="ClaimableCallback"/>.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Subscribe(ClaimableCallback callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Unsubscribe the specified callback from the event.
/// This will stops notifications to call this callback.
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
void Unsubscribe(ClaimableCallback callback, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Asynchronously wait until the event fires and the specific condition applies.
/// </summary>
/// <returns>The task to be awaited.</returns>
/// <param name="condition">The condition we are waiting to apply before moving on.</param>
/// <param name="priority">The callback priority (determines the order in which the subscribers get the events).</param>
Task WaitUntilAsync(Func<bool> condition, CallbackPriority priority = CallbackPriority.Normal);
/// <summary>
/// Invoke the event synchronously (i.e will wait for all subscribers to process the event before moving on).
/// </summary>
void Invoke();
}
}