update intervall prevent spam better performance

This commit is contained in:
choco
2025-10-12 21:07:32 +02:00
parent 3f80467180
commit 6c0d00dc39

View File

@@ -24,6 +24,9 @@ public class DownloadUi : WindowMediatorSubscriberBase
private readonly ConcurrentDictionary<GameObjectHandler, bool> _uploadingPlayers = new();
private readonly NotificationService _notificationService;
private bool _notificationDismissed = true;
private int _lastDownloadStateHash = 0;
private DateTime _lastNotificationUpdate = DateTime.MinValue;
private const int MinUpdateIntervalMs = 1000;
public DownloadUi(ILogger<DownloadUi> logger, DalamudUtilService dalamudUtilService, LightlessConfigService configService,
PairProcessingLimiter pairProcessingLimiter, FileUploadManager fileTransferManager, LightlessMediator mediator, UiSharedService uiShared,
@@ -128,16 +131,18 @@ public class DownloadUi : WindowMediatorSubscriberBase
if (useNotifications)
{
// Use notification system
// Use notification system - only update when data actually changes
if (_currentDownloads.Any())
{
UpdateDownloadNotification(limiterSnapshot);
UpdateDownloadNotificationIfChanged(limiterSnapshot);
_notificationDismissed = false;
}
else if (!_notificationDismissed)
{
_notificationService.DismissPairDownloadNotification();
_notificationDismissed = true;
_lastDownloadStateHash = 0;
_lastNotificationUpdate = DateTime.MinValue;
}
}
else
@@ -298,20 +303,34 @@ public class DownloadUi : WindowMediatorSubscriberBase
};
}
private void UpdateDownloadNotification(PairProcessingLimiterSnapshot limiterSnapshot)
private void UpdateDownloadNotificationIfChanged(PairProcessingLimiterSnapshot limiterSnapshot)
{
var downloadStatus = new List<(string playerName, float progress, string status)>();
var downloadStatus = new List<(string playerName, float progress, string status)>(_currentDownloads.Count);
var hashCode = new HashCode();
foreach (var item in _currentDownloads.ToList())
foreach (var item in _currentDownloads)
{
var dlSlot = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.WaitingForSlot);
var dlQueue = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.WaitingForQueue);
var dlProg = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.Downloading);
var dlDecomp = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.Decompressing);
var totalFiles = item.Value.Sum(c => c.Value.TotalFiles);
var transferredFiles = item.Value.Sum(c => c.Value.TransferredFiles);
var totalBytes = item.Value.Sum(c => c.Value.TotalBytes);
var transferredBytes = item.Value.Sum(c => c.Value.TransferredBytes);
var dlSlot = 0;
var dlQueue = 0;
var dlProg = 0;
var dlDecomp = 0;
long totalBytes = 0;
long transferredBytes = 0;
// Single pass through the dictionary to count everything - avoid multiple LINQ iterations
foreach (var entry in item.Value)
{
var fileStatus = entry.Value;
switch (fileStatus.DownloadStatus)
{
case DownloadStatus.WaitingForSlot: dlSlot++; break;
case DownloadStatus.WaitingForQueue: dlQueue++; break;
case DownloadStatus.Downloading: dlProg++; break;
case DownloadStatus.Decompressing: dlDecomp++; break;
}
totalBytes += fileStatus.TotalBytes;
transferredBytes += fileStatus.TransferredBytes;
}
var progress = totalBytes > 0 ? (float)transferredBytes / totalBytes : 0f;
@@ -323,13 +342,30 @@ public class DownloadUi : WindowMediatorSubscriberBase
else status = "completed";
downloadStatus.Add((item.Key.Name, progress, status));
// Build hash from meaningful state
hashCode.Add(item.Key.Name);
hashCode.Add(transferredBytes);
hashCode.Add(totalBytes);
hashCode.Add(status);
}
// Pass queue waiting count separately, show notification if there are downloads or queue items
var queueWaiting = limiterSnapshot.IsEnabled ? limiterSnapshot.Waiting : 0;
if (downloadStatus.Any() || queueWaiting > 0)
hashCode.Add(queueWaiting);
var currentHash = hashCode.ToHashCode();
var now = DateTime.UtcNow;
var timeSinceLastUpdate = (now - _lastNotificationUpdate).TotalMilliseconds;
// Only update notification if state has changed AND at least 1 second has passed
if (currentHash != _lastDownloadStateHash && timeSinceLastUpdate >= MinUpdateIntervalMs)
{
_notificationService.ShowPairDownloadNotification(downloadStatus, queueWaiting);
_lastDownloadStateHash = currentHash;
_lastNotificationUpdate = now;
if (downloadStatus.Count > 0 || queueWaiting > 0)
{
_notificationService.ShowPairDownloadNotification(downloadStatus, queueWaiting);
}
}
}