Skip to content

Commit 5d223d5

Browse files
kiapanahiJamesNK
andauthored
Minor improvements in examples codebase: Mailer (#1312)
Co-authored-by: James Newton-King <[email protected]>
1 parent e0b8392 commit 5d223d5

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

examples/Mailer/Client/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static async Task Main(string[] args)
7575

7676
private static string GetMailboxName(string[] args)
7777
{
78-
if (args.Length != 1)
78+
if (args.Length < 1)
7979
{
8080
Console.WriteLine("No mailbox name provided. Using default name. Usage: dotnet run <name>.");
8181
return "DefaultMailbox";

examples/Mailer/Server/MailQueue.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,9 @@
2525

2626
namespace Server
2727
{
28-
public class Mail
29-
{
30-
public Mail(int id, string content)
31-
{
32-
Id = id;
33-
Content = content;
34-
}
28+
public record Mail(int Id, string Content);
3529

36-
public int Id { get; }
37-
public string Content { get; }
38-
}
30+
public record MailQueueChangeState(int TotalCount, int ForwardedCount, MailboxMessage.Types.Reason Reason);
3931

4032
public class MailQueue
4133
{
@@ -44,15 +36,15 @@ public class MailQueue
4436
private int _forwardedMailCount;
4537

4638
public string Name { get; }
47-
public event Func<(int totalCount, int newCount, MailboxMessage.Types.Reason reason), Task>? Changed;
39+
public event Func<MailQueueChangeState, Task>? Changed;
4840

4941
public MailQueue(string name)
5042
{
5143
Name = name;
5244

5345
_incomingMail = Channel.CreateUnbounded<Mail>();
5446

55-
Task.Run(async () =>
47+
_ = Task.Run(async () =>
5648
{
5749
var random = new Random();
5850

@@ -83,7 +75,7 @@ public bool TryForwardMail([NotNullWhen(true)] out Mail? message)
8375

8476
private void OnChange(MailboxMessage.Types.Reason reason)
8577
{
86-
Changed?.Invoke((_totalMailCount, _forwardedMailCount, reason));
78+
Changed?.Invoke(new MailQueueChangeState(_totalMailCount, _forwardedMailCount, reason));
8779
}
8880
}
8981
}

examples/Mailer/Server/MailQueueRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Server
2222
{
2323
public class MailQueueRepository
2424
{
25-
private ConcurrentDictionary<string, MailQueue> _mailQueues = new ConcurrentDictionary<string, MailQueue>();
25+
private readonly ConcurrentDictionary<string, MailQueue> _mailQueues = new();
2626

2727
public MailQueue GetMailQueue(string name)
2828
{

examples/Mailer/Server/Services/MailerService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public async override Task Mailbox(
6969

7070
_logger.LogInformation($"{mailboxName} disconnected");
7171

72-
async Task ReportChanges((int totalCount, int fowardCount, MailboxMessage.Types.Reason reason) state)
72+
async Task ReportChanges(MailQueueChangeState state)
7373
{
7474
await responseStream.WriteAsync(new MailboxMessage
7575
{
76-
Forwarded = state.fowardCount,
77-
New = state.totalCount - state.fowardCount,
78-
Reason = state.reason
76+
Forwarded = state.ForwardedCount,
77+
New = state.TotalCount - state.ForwardedCount,
78+
Reason = state.Reason
7979
});
8080
}
8181
}

0 commit comments

Comments
 (0)