Make dashboard change notifications async and robust
Refactored DashboardChangeNotifier and IDashboardChangeNotifier to use async notification with improved error handling and logging. Updated SqlDashboardStorage to call the new async notification method after dashboard changes, ensuring non-blocking and reliable client updates.
This commit is contained in:
@@ -6,14 +6,23 @@ namespace DbFirst.API.Dashboards;
|
||||
public class DashboardChangeNotifier : IDashboardChangeNotifier
|
||||
{
|
||||
private readonly IHubContext<DashboardsHub> _hubContext;
|
||||
private readonly ILogger<DashboardChangeNotifier> _logger;
|
||||
|
||||
public DashboardChangeNotifier(IHubContext<DashboardsHub> hubContext)
|
||||
public DashboardChangeNotifier(IHubContext<DashboardsHub> hubContext, ILogger<DashboardChangeNotifier> logger)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void NotifyChanged()
|
||||
public async Task NotifyChangedAsync()
|
||||
{
|
||||
_ = _hubContext.Clients.All.SendAsync("DashboardsChanged");
|
||||
try
|
||||
{
|
||||
await _hubContext.Clients.All.SendAsync("DashboardsChanged");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to notify dashboard clients.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ namespace DbFirst.API.Dashboards;
|
||||
|
||||
public interface IDashboardChangeNotifier
|
||||
{
|
||||
void NotifyChanged();
|
||||
Task NotifyChangedAsync();
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage
|
||||
|
||||
connection.Open();
|
||||
command.ExecuteNonQuery();
|
||||
_notifier?.NotifyChanged();
|
||||
_ = _notifier?.NotifyChangedAsync();
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage
|
||||
throw new ArgumentException($"Dashboard '{dashboardId}' not found.");
|
||||
}
|
||||
|
||||
_notifier?.NotifyChanged();
|
||||
_ = _notifier?.NotifyChangedAsync();
|
||||
}
|
||||
|
||||
public void DeleteDashboard(string dashboardId)
|
||||
@@ -133,6 +133,6 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage
|
||||
|
||||
connection.Open();
|
||||
command.ExecuteNonQuery();
|
||||
_notifier?.NotifyChanged();
|
||||
_ = _notifier?.NotifyChangedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user