Skip to content

Commit 73c3ab1

Browse files
committed
Adding SendEventAsync overload which takes a predicate against IServerSentEventsClient as a parameter (#23)
1 parent 0e19ca7 commit 73c3ab1

File tree

2 files changed

+184
-1
lines changed

2 files changed

+184
-1
lines changed

Lib.AspNetCore.ServerSentEvents/IServerSentEventsService.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ public interface IServerSentEventsService
8080
/// <returns>The task object representing the asynchronous operation.</returns>
8181
Task SendEventAsync(string text);
8282

83+
/// <summary>
84+
/// Sends event to all clients.
85+
/// </summary>
86+
/// <param name="text">The simple text event.</param>
87+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
88+
/// <returns>The task object representing the asynchronous operation.</returns>
89+
Task SendEventAsync(string text, Func<IServerSentEventsClient, bool> clientPredicate);
90+
8391
/// <summary>
8492
/// Sends event to clients in group.
8593
/// </summary>
@@ -88,6 +96,15 @@ public interface IServerSentEventsService
8896
/// <returns>The task object representing the asynchronous operation.</returns>
8997
Task SendEventAsync(string groupName, string text);
9098

99+
/// <summary>
100+
/// Sends event to clients in group.
101+
/// </summary>
102+
/// <param name="groupName">The group name.</param>
103+
/// <param name="text">The simple text event.</param>
104+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
105+
/// <returns>The task object representing the asynchronous operation.</returns>
106+
Task SendEventAsync(string groupName, string text, Func<IServerSentEventsClient, bool> clientPredicate);
107+
91108
/// <summary>
92109
/// Sends event to all clients.
93110
/// </summary>
@@ -96,6 +113,15 @@ public interface IServerSentEventsService
96113
/// <returns>The task object representing the asynchronous operation.</returns>
97114
Task SendEventAsync(string text, CancellationToken cancellationToken);
98115

116+
/// <summary>
117+
/// Sends event to all clients.
118+
/// </summary>
119+
/// <param name="text">The simple text event.</param>
120+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
121+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
122+
/// <returns>The task object representing the asynchronous operation.</returns>
123+
Task SendEventAsync(string text, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken);
124+
99125
/// <summary>
100126
/// Sends event to clients in group.
101127
/// </summary>
@@ -105,13 +131,31 @@ public interface IServerSentEventsService
105131
/// <returns>The task object representing the asynchronous operation.</returns>
106132
Task SendEventAsync(string groupName, string text, CancellationToken cancellationToken);
107133

134+
/// <summary>
135+
/// Sends event to clients in group.
136+
/// </summary>
137+
/// <param name="groupName">The group name.</param>
138+
/// <param name="text">The simple text event.</param>
139+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
140+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
141+
/// <returns>The task object representing the asynchronous operation.</returns>
142+
Task SendEventAsync(string groupName, string text, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken);
143+
108144
/// <summary>
109145
/// Sends event to all clients.
110146
/// </summary>
111147
/// <param name="serverSentEvent">The event.</param>
112148
/// <returns>The task object representing the asynchronous operation.</returns>
113149
Task SendEventAsync(ServerSentEvent serverSentEvent);
114150

151+
/// <summary>
152+
/// Sends event to all clients.
153+
/// </summary>
154+
/// <param name="serverSentEvent">The event.</param>
155+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
156+
/// <returns>The task object representing the asynchronous operation.</returns>
157+
Task SendEventAsync(ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate);
158+
115159
/// <summary>
116160
/// Sends event to clients in group.
117161
/// </summary>
@@ -120,6 +164,15 @@ public interface IServerSentEventsService
120164
/// <returns>The task object representing the asynchronous operation.</returns>
121165
Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent);
122166

167+
/// <summary>
168+
/// Sends event to clients in group.
169+
/// </summary>
170+
/// <param name="groupName">The group name.</param>
171+
/// <param name="serverSentEvent">The event.</param>
172+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
173+
/// <returns>The task object representing the asynchronous operation.</returns>
174+
Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate);
175+
123176
/// <summary>
124177
/// Sends event to all clients.
125178
/// </summary>
@@ -128,6 +181,15 @@ public interface IServerSentEventsService
128181
/// <returns>The task object representing the asynchronous operation.</returns>
129182
Task SendEventAsync(ServerSentEvent serverSentEvent, CancellationToken cancellationToken);
130183

184+
/// <summary>
185+
/// Sends event to all clients.
186+
/// </summary>
187+
/// <param name="serverSentEvent">The event.</param>
188+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
189+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
190+
/// <returns>The task object representing the asynchronous operation.</returns>
191+
Task SendEventAsync(ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken);
192+
131193
/// <summary>
132194
/// Sends event to clients in group.
133195
/// </summary>
@@ -137,6 +199,16 @@ public interface IServerSentEventsService
137199
/// <returns>The task object representing the asynchronous operation.</returns>
138200
Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent, CancellationToken cancellationToken);
139201

202+
/// <summary>
203+
/// Sends event to clients in group.
204+
/// </summary>
205+
/// <param name="groupName">The group name.</param>
206+
/// <param name="serverSentEvent">The event.</param>
207+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
208+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
209+
/// <returns>The task object representing the asynchronous operation.</returns>
210+
Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken);
211+
140212
/// <summary>
141213
/// Method which is called when client is establishing the connection. The base implementation raises the <see cref="ClientConnected"/> event.
142214
/// </summary>

Lib.AspNetCore.ServerSentEvents/ServerSentEventsService.cs

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ public Task SendEventAsync(string text)
160160
return SendAsync(ServerSentEventsHelper.GetEventBytes(text), CancellationToken.None);
161161
}
162162

163+
/// <summary>
164+
/// Sends event to all clients.
165+
/// </summary>
166+
/// <param name="text">The simple text event.</param>
167+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
168+
/// <returns>The task object representing the asynchronous operation.</returns>
169+
public Task SendEventAsync(string text, Func<IServerSentEventsClient, bool> clientPredicate)
170+
{
171+
return SendAsync(ServerSentEventsHelper.GetEventBytes(text), clientPredicate, CancellationToken.None);
172+
}
173+
163174
/// <summary>
164175
/// Sends event to clients in group.
165176
/// </summary>
@@ -171,6 +182,18 @@ public Task SendEventAsync(string groupName, string text)
171182
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(text), CancellationToken.None);
172183
}
173184

185+
/// <summary>
186+
/// Sends event to clients in group.
187+
/// </summary>
188+
/// <param name="groupName">The group name.</param>
189+
/// <param name="text">The simple text event.</param>
190+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
191+
/// <returns>The task object representing the asynchronous operation.</returns>
192+
public Task SendEventAsync(string groupName, string text, Func<IServerSentEventsClient, bool> clientPredicate)
193+
{
194+
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(text), clientPredicate, CancellationToken.None);
195+
}
196+
174197
/// <summary>
175198
/// Sends event to all clients.
176199
/// </summary>
@@ -182,6 +205,18 @@ public Task SendEventAsync(string text, CancellationToken cancellationToken)
182205
return SendAsync(ServerSentEventsHelper.GetEventBytes(text), cancellationToken);
183206
}
184207

208+
/// <summary>
209+
/// Sends event to all clients.
210+
/// </summary>
211+
/// <param name="text">The simple text event.</param>
212+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
213+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
214+
/// <returns>The task object representing the asynchronous operation.</returns>
215+
public Task SendEventAsync(string text, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
216+
{
217+
return SendAsync(ServerSentEventsHelper.GetEventBytes(text), clientPredicate, cancellationToken);
218+
}
219+
185220
/// <summary>
186221
/// Sends event to clients in group.
187222
/// </summary>
@@ -194,6 +229,19 @@ public Task SendEventAsync(string groupName, string text, CancellationToken canc
194229
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(text), cancellationToken);
195230
}
196231

232+
/// <summary>
233+
/// Sends event to clients in group.
234+
/// </summary>
235+
/// <param name="groupName">The group name.</param>
236+
/// <param name="text">The simple text event.</param>
237+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
238+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
239+
/// <returns>The task object representing the asynchronous operation.</returns>
240+
public Task SendEventAsync(string groupName, string text, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
241+
{
242+
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(text), clientPredicate, cancellationToken);
243+
}
244+
197245
/// <summary>
198246
/// Sends event to all clients.
199247
/// </summary>
@@ -204,6 +252,17 @@ public Task SendEventAsync(ServerSentEvent serverSentEvent)
204252
return SendAsync(ServerSentEventsHelper.GetEventBytes(serverSentEvent), CancellationToken.None);
205253
}
206254

255+
/// <summary>
256+
/// Sends event to all clients.
257+
/// </summary>
258+
/// <param name="serverSentEvent">The event.</param>
259+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
260+
/// <returns>The task object representing the asynchronous operation.</returns>
261+
public Task SendEventAsync(ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate)
262+
{
263+
return SendAsync(ServerSentEventsHelper.GetEventBytes(serverSentEvent), clientPredicate, CancellationToken.None);
264+
}
265+
207266
/// <summary>
208267
/// Sends event to clients in group.
209268
/// </summary>
@@ -215,6 +274,18 @@ public Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent)
215274
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(serverSentEvent), CancellationToken.None);
216275
}
217276

277+
/// <summary>
278+
/// Sends event to clients in group.
279+
/// </summary>
280+
/// <param name="groupName">The group name.</param>
281+
/// <param name="serverSentEvent">The event.</param>
282+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
283+
/// <returns>The task object representing the asynchronous operation.</returns>
284+
public Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate)
285+
{
286+
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(serverSentEvent), clientPredicate, CancellationToken.None);
287+
}
288+
218289
/// <summary>
219290
/// Sends event to all clients.
220291
/// </summary>
@@ -226,6 +297,18 @@ public Task SendEventAsync(ServerSentEvent serverSentEvent, CancellationToken ca
226297
return SendAsync(ServerSentEventsHelper.GetEventBytes(serverSentEvent), cancellationToken);
227298
}
228299

300+
/// <summary>
301+
/// Sends event to all clients.
302+
/// </summary>
303+
/// <param name="serverSentEvent">The event.</param>
304+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
305+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
306+
/// <returns>The task object representing the asynchronous operation.</returns>
307+
public Task SendEventAsync(ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
308+
{
309+
return SendAsync(ServerSentEventsHelper.GetEventBytes(serverSentEvent), clientPredicate, cancellationToken);
310+
}
311+
229312
/// <summary>
230313
/// Sends event to clients in group.
231314
/// </summary>
@@ -238,6 +321,19 @@ public Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent, Ca
238321
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(serverSentEvent), cancellationToken);
239322
}
240323

324+
/// <summary>
325+
/// Sends event to clients in group.
326+
/// </summary>
327+
/// <param name="groupName">The group name.</param>
328+
/// <param name="serverSentEvent">The event.</param>
329+
/// <param name="clientPredicate">The function to test each client for a condition.</param>
330+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
331+
/// <returns>The task object representing the asynchronous operation.</returns>
332+
public Task SendEventAsync(string groupName, ServerSentEvent serverSentEvent, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
333+
{
334+
return SendAsync(groupName, ServerSentEventsHelper.GetEventBytes(serverSentEvent), clientPredicate, cancellationToken);
335+
}
336+
241337
/// <summary>
242338
/// Method which is called when client is establishing the connection. The base implementation raises the <see cref="ClientConnected"/> event.
243339
/// </summary>
@@ -316,6 +412,11 @@ internal Task SendAsync(ServerSentEventBytes serverSentEventBytes, CancellationT
316412
return SendAsync(_clients.Values, serverSentEventBytes, cancellationToken);
317413
}
318414

415+
internal Task SendAsync(ServerSentEventBytes serverSentEventBytes, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
416+
{
417+
return SendAsync(_clients.Values.Where(clientPredicate), serverSentEventBytes, cancellationToken);
418+
}
419+
319420
internal Task SendAsync(string groupName, ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken)
320421
{
321422
if (_groups.ContainsKey(groupName))
@@ -326,7 +427,17 @@ internal Task SendAsync(string groupName, ServerSentEventBytes serverSentEventBy
326427
return Task.CompletedTask;
327428
}
328429

329-
internal Task SendAsync(ICollection<IServerSentEventsClient> clients, ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken)
430+
internal Task SendAsync(string groupName, ServerSentEventBytes serverSentEventBytes, Func<IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
431+
{
432+
if (_groups.ContainsKey(groupName))
433+
{
434+
return SendAsync(_groups[groupName].Values.Where(clientPredicate), serverSentEventBytes, cancellationToken);
435+
}
436+
437+
return Task.CompletedTask;
438+
}
439+
440+
internal Task SendAsync(IEnumerable<IServerSentEventsClient> clients, ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken)
330441
{
331442
List<Task> clientsTasks = null;
332443

0 commit comments

Comments
 (0)