Skip to content

Commit

Permalink
Replace ConcurrentList with BCL collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 7, 2023
1 parent 11ac3a7 commit 4e01804
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 290 deletions.
6 changes: 3 additions & 3 deletions Server/Components/Devices/ChatCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ private async Task HandleChatMessageReceived(ChatReceivedMessage message)

if (message.DidDisconnect)
{
session.ChatHistory.Add(new ChatHistoryItem()
session.ChatHistory.Enqueue(new ChatHistoryItem()
{
Message = $"{Session.DeviceName} disconnected.",
Origin = ChatHistoryItemOrigin.System
});
}
else
{
session.ChatHistory.Add(new ChatHistoryItem()
session.ChatHistory.Enqueue(new ChatHistoryItem()
{
Message = message.MessageText,
Origin = ChatHistoryItemOrigin.Device
Expand Down Expand Up @@ -110,7 +110,7 @@ private async Task EvaluateInputKeypress(KeyboardEventArgs args)

await CircuitConnection.SendChat(_inputText, $"{Session.DeviceId}");

Session.ChatHistory.Add(new ChatHistoryItem()
Session.ChatHistory.Enqueue(new ChatHistoryItem()
{
Origin = ChatHistoryItemOrigin.Self,
Message = _inputText
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Devices/ChatFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private async Task HandleChatMessageReceived(ChatReceivedMessage message)
IsExpanded = true
};

newChat.ChatHistory.Add(new ChatHistoryItem()
newChat.ChatHistory.Enqueue(new ChatHistoryItem()
{
Message = message.MessageText,
Origin = ChatHistoryItemOrigin.Device
Expand Down
31 changes: 26 additions & 5 deletions Server/Services/ToastService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Remotely.Server.Enums;
using Remotely.Server.Models;
using Remotely.Shared.Primitives;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Timers;

namespace Remotely.Server.Services;
Expand All @@ -10,7 +13,7 @@ public interface IToastService
{
event EventHandler OnToastsChanged;

ConcurrentList<Toast> Toasts { get; }
IEnumerable<Toast> Toasts { get; }
void ShowToast(
string message,
int expirationMillisecond = 3000,
Expand All @@ -26,8 +29,20 @@ void ShowToast2(

public class ToastService : IToastService
{
private readonly object _lock = new();
private readonly List<Toast> _toasts = new();
public event EventHandler? OnToastsChanged;
public ConcurrentList<Toast> Toasts { get; } = new();

public IEnumerable<Toast> Toasts
{
get
{
lock (_lock)
{
return _toasts.ToArray();
}
}
}

public void ShowToast(string message,
int expirationMillisecond = 3000,
Expand All @@ -46,7 +61,10 @@ public void ShowToast(string message,
TimeSpan.FromMilliseconds(expirationMillisecond),
styleOverrides);

Toasts.Add(toastModel);
lock (_lock)
{
_toasts.Add(toastModel);
}

OnToastsChanged?.Invoke(this, EventArgs.Empty);

Expand All @@ -56,7 +74,10 @@ public void ShowToast(string message,
};
removeToastTimer.Elapsed += (s, e) =>
{
Toasts.Remove(toastModel);
lock (_lock)
{
_toasts.Remove(toastModel);
}
OnToastsChanged?.Invoke(this, EventArgs.Empty);
removeToastTimer.Dispose();
};
Expand Down
198 changes: 0 additions & 198 deletions Shared/Primitives/ConcurrentList.cs

This file was deleted.

6 changes: 3 additions & 3 deletions Shared/ViewModels/ChatSession.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Remotely.Shared.Primitives;
using System;
using System;
using System.Collections.Concurrent;

namespace Remotely.Shared.ViewModels;

public class ChatSession
{
public ConcurrentList<ChatHistoryItem> ChatHistory { get; } = new();
public ConcurrentQueue<ChatHistoryItem> ChatHistory { get; } = new();
public string? DeviceId { get; set; }
public string? DeviceName { get; set; }
public string ExpandedClass => IsExpanded ? "expanded" : "";
Expand Down
80 changes: 0 additions & 80 deletions Tests/Shared.Tests/ConcurrentListTests.cs

This file was deleted.

0 comments on commit 4e01804

Please sign in to comment.