-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationSystem.cs
More file actions
368 lines (312 loc) · 12.1 KB
/
NotificationSystem.cs
File metadata and controls
368 lines (312 loc) · 12.1 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using PatternKit.Generators.Observer;
namespace PatternKit.Examples.ObserverGeneratorDemo;
/// <summary>
/// A notification message to be sent.
/// </summary>
/// <param name="RecipientId">ID of the recipient.</param>
/// <param name="Message">The notification message.</param>
/// <param name="Priority">Priority level (0=low, 1=normal, 2=high).</param>
public record Notification(string RecipientId, string Message, int Priority);
/// <summary>
/// Result of attempting to send a notification.
/// </summary>
/// <param name="Success">Whether the send was successful.</param>
/// <param name="Channel">Which channel was used (Email, SMS, Push).</param>
/// <param name="Error">Error message if failed.</param>
public record NotificationResult(bool Success, string Channel, string? Error = null);
/// <summary>
/// Observable event for notifications with async support.
/// Demonstrates async handlers and PublishAsync.
/// </summary>
[Observer(typeof(Notification),
Threading = ObserverThreadingPolicy.Locking,
Exceptions = ObserverExceptionPolicy.Continue,
GenerateAsync = true)]
public partial class NotificationPublished
{
partial void OnSubscriberError(Exception ex)
{
Console.WriteLine($"❌ Notification handler error: {ex.Message}");
}
}
/// <summary>
/// Observable event for notification results.
/// Uses Aggregate exception policy to collect all failures.
/// </summary>
[Observer(typeof(NotificationResult),
Threading = ObserverThreadingPolicy.Locking,
Exceptions = ObserverExceptionPolicy.Aggregate)]
public partial class NotificationSent
{
}
/// <summary>
/// Multi-channel notification system with async handlers.
/// </summary>
public class NotificationSystem
{
private readonly NotificationPublished _notificationPublished = new();
private readonly NotificationSent _notificationSent = new();
private readonly Random _random = new();
/// <summary>
/// Subscribes to notifications with a synchronous handler.
/// </summary>
public IDisposable Subscribe(Action<Notification> handler) =>
_notificationPublished.Subscribe(handler);
/// <summary>
/// Subscribes to notifications with an async handler.
/// </summary>
public IDisposable SubscribeAsync(Func<Notification, ValueTask> handler) =>
_notificationPublished.Subscribe(handler);
/// <summary>
/// Subscribes to notification send results.
/// </summary>
public IDisposable OnNotificationSent(Action<NotificationResult> handler) =>
_notificationSent.Subscribe(handler);
/// <summary>
/// Sends a notification through all registered channels asynchronously.
/// </summary>
public async Task SendAsync(Notification notification, CancellationToken cancellationToken = default)
{
Console.WriteLine($"\n📤 Sending notification (Priority: {notification.Priority})...");
await _notificationPublished.PublishAsync(notification, cancellationToken);
}
/// <summary>
/// Reports that a notification was sent through a channel.
/// </summary>
public void ReportSent(NotificationResult result)
{
_notificationSent.Publish(result);
}
/// <summary>
/// Simulates sending an email (async operation).
/// </summary>
public async Task<NotificationResult> SendEmailAsync(Notification notification)
{
await Task.Delay(100); // Simulate network delay
// Simulate random failures (20% chance)
if (_random.NextDouble() < 0.2)
{
return new NotificationResult(false, "Email", "SMTP server unavailable");
}
Console.WriteLine($" ✉️ Email sent to {notification.RecipientId}");
return new NotificationResult(true, "Email");
}
/// <summary>
/// Simulates sending an SMS (async operation).
/// </summary>
public async Task<NotificationResult> SendSmsAsync(Notification notification)
{
await Task.Delay(80); // Simulate network delay
// High priority only
if (notification.Priority < 2)
{
return new NotificationResult(false, "SMS", "Priority too low for SMS");
}
Console.WriteLine($" 📱 SMS sent to {notification.RecipientId}");
return new NotificationResult(true, "SMS");
}
/// <summary>
/// Simulates sending a push notification (async operation).
/// </summary>
public async Task<NotificationResult> SendPushAsync(Notification notification)
{
await Task.Delay(50); // Simulate network delay
Console.WriteLine($" 🔔 Push notification sent to {notification.RecipientId}");
return new NotificationResult(true, "Push");
}
}
/// <summary>
/// Demonstrates async handlers with PublishAsync.
/// </summary>
public static class AsyncNotificationDemo
{
public static async Task RunAsync()
{
Console.WriteLine("=== Async Notification System ===\n");
var system = new NotificationSystem();
// Subscribe email channel (async handler)
using var emailSub = system.SubscribeAsync(async notification =>
{
var result = await system.SendEmailAsync(notification);
system.ReportSent(result);
});
// Subscribe SMS channel (async handler)
using var smsSub = system.SubscribeAsync(async notification =>
{
var result = await system.SendSmsAsync(notification);
system.ReportSent(result);
});
// Subscribe push channel (async handler)
using var pushSub = system.SubscribeAsync(async notification =>
{
var result = await system.SendPushAsync(notification);
system.ReportSent(result);
});
// Subscribe to results to track success/failure
var successCount = 0;
var failureCount = 0;
using var resultSub = system.OnNotificationSent(result =>
{
if (result.Success)
{
successCount++;
}
else
{
failureCount++;
Console.WriteLine($" ⚠️ {result.Channel} failed: {result.Error}");
}
});
// Send notifications with different priorities
var notifications = new[]
{
new Notification("user123", "Welcome to our service!", 1),
new Notification("user456", "Your order has shipped", 1),
new Notification("user789", "URGENT: Security alert", 2),
new Notification("user999", "Daily digest available", 0)
};
foreach (var notification in notifications)
{
await system.SendAsync(notification);
await Task.Delay(200); // Space out notifications
}
Console.WriteLine($"\n📊 Results: {successCount} successful, {failureCount} failed");
}
}
/// <summary>
/// Demonstrates exception handling with different policies.
/// </summary>
public static class ExceptionHandlingDemo
{
public static void Run()
{
Console.WriteLine("\n=== Exception Handling Demo ===\n");
// Demo 1: Continue policy (default) - all handlers run despite errors
Console.WriteLine("1. Continue Policy (fault-tolerant):");
DemoContinuePolicy();
// Demo 2: Aggregate policy - collect all errors
Console.WriteLine("\n2. Aggregate Policy (collect all errors):");
DemoAggregatePolicy();
}
private static void DemoContinuePolicy()
{
var notification = new NotificationPublished();
// Handler 1: Works fine
notification.Subscribe(n =>
Console.WriteLine(" ✅ Handler 1: Success"));
// Handler 2: Throws exception
notification.Subscribe(n =>
{
Console.WriteLine(" ❌ Handler 2: Throwing exception...");
throw new InvalidOperationException("Handler 2 failed");
});
// Handler 3: Also works fine
notification.Subscribe(n =>
Console.WriteLine(" ✅ Handler 3: Success (ran despite Handler 2 error)"));
notification.Publish(new Notification("test", "Test message", 1));
Console.WriteLine(" ℹ️ All handlers attempted, errors logged via OnSubscriberError");
}
private static void DemoAggregatePolicy()
{
var results = new NotificationSent();
// Handler 1: Throws
results.Subscribe(r =>
{
Console.WriteLine(" ❌ Validator 1: Failed");
throw new InvalidOperationException("Validation 1 failed");
});
// Handler 2: Also throws
results.Subscribe(r =>
{
Console.WriteLine(" ❌ Validator 2: Failed");
throw new ArgumentException("Validation 2 failed");
});
// Handler 3: Would succeed
results.Subscribe(r =>
Console.WriteLine(" ✅ Validator 3: Success"));
try
{
results.Publish(new NotificationResult(true, "Test"));
Console.WriteLine(" ℹ️ No exception thrown (shouldn't reach here)");
}
catch (AggregateException ex)
{
Console.WriteLine($" 🔥 AggregateException caught with {ex.InnerExceptions.Count} errors:");
foreach (var inner in ex.InnerExceptions)
{
Console.WriteLine($" - {inner.GetType().Name}: {inner.Message}");
}
}
}
}
/// <summary>
/// Demonstrates mixing sync and async handlers.
/// </summary>
public static class MixedHandlersDemo
{
public static async Task RunAsync()
{
Console.WriteLine("\n=== Mixed Sync/Async Handlers Demo ===\n");
var notification = new NotificationPublished();
// Sync handler
notification.Subscribe(n =>
Console.WriteLine($" 🔹 Sync handler: {n.Message}"));
// Async handler
notification.Subscribe(async n =>
{
await Task.Delay(50);
Console.WriteLine($" 🔸 Async handler: {n.Message}");
});
// Another sync handler
notification.Subscribe(n =>
Console.WriteLine($" 🔹 Sync handler 2: Priority={n.Priority}"));
Console.WriteLine("Publishing with Publish (sync):");
notification.Publish(new Notification("user", "Hello World", 1));
// Note: async handlers run fire-and-forget with Publish
await Task.Delay(100); // Wait for async handlers
Console.WriteLine("\nPublishing with PublishAsync (awaits async handlers):");
await notification.PublishAsync(new Notification("user", "Goodbye World", 2));
Console.WriteLine("\nNote: PublishAsync waits for all async handlers to complete.");
}
}
/// <summary>
/// Demonstrates cancellation token support in async handlers.
/// </summary>
public static class CancellationDemo
{
public static async Task RunAsync()
{
Console.WriteLine("\n=== Cancellation Demo ===\n");
var notification = new NotificationPublished();
var processedCount = 0;
// Long-running async handler
// Note: Cancellation is checked between handlers, not during handler execution
notification.Subscribe(async n =>
{
Console.WriteLine(" ⏳ Starting long operation...");
await Task.Delay(100); // Shorter delay for demo
processedCount++;
Console.WriteLine(" ✅ Long operation completed");
});
// Quick handler
notification.Subscribe(async n =>
{
await Task.Delay(10);
processedCount++;
Console.WriteLine(" ✅ Quick operation completed");
});
using var cts = new CancellationTokenSource(50); // Cancel after 50ms - before first handler completes
try
{
await notification.PublishAsync(
new Notification("user", "Test", 1),
cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("\n ℹ️ PublishAsync was cancelled");
}
Console.WriteLine($"\n Handlers completed: {processedCount}/2");
Console.WriteLine(" Note: Cancellation is checked between handler invocations, not during execution");
}
}