Skip to content

Commit 526d4fe

Browse files
authored
#863 Refactor cron occurrence serialization logic (#864)
Centralize serialization for cron occurrence notifications by adding SerializeOccurrence method. Use source generator context for known types and camelCase options as fallback. Simplifies Add/UpdateCronOccurrenceAsync methods.
1 parent a4600eb commit 526d4fe

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/TickerQ.Dashboard/Hubs/TickerQNotificationHubSender.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ internal class TickerQNotificationHubSender : ITickerQNotificationHubSender
1616
private readonly Timer _timeTickerUpdateTimer;
1717
private int _hasPendingTimeTickerUpdate;
1818
private static readonly TimeSpan TimeTickerUpdateDebounce = TimeSpan.FromMilliseconds(100);
19-
19+
private static readonly JsonSerializerOptions CamelCaseOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
20+
2021
public TickerQNotificationHubSender(IHubContext<TickerQNotificationHub> hubContext)
2122
{
2223
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
@@ -95,16 +96,23 @@ public async Task UpdateNodeHeartBeatAsync(JsonElement nodeHeartBeat)
9596

9697
public async Task AddCronOccurrenceAsync(Guid groupId, object occurrence)
9798
{
98-
var json = JsonSerializer.SerializeToElement((CronTickerOccurrenceEntity<CronTickerEntity>)occurrence, DashboardJsonSerializerContext.Default.CronTickerOccurrenceEntityCronTickerEntity);
99+
var json = SerializeOccurrence(occurrence);
99100
await _hubContext.Clients.Group(groupId.ToString()).SendAsync("AddCronOccurrenceNotification", json);
100101
}
101102

102103
public async Task UpdateCronOccurrenceAsync(Guid groupId, object occurrence)
103104
{
104-
var json = JsonSerializer.SerializeToElement((CronTickerOccurrenceEntity<CronTickerEntity>)occurrence, DashboardJsonSerializerContext.Default.CronTickerOccurrenceEntityCronTickerEntity);
105+
var json = SerializeOccurrence(occurrence);
105106
await _hubContext.Clients.Group(groupId.ToString()).SendAsync("UpdateCronOccurrenceNotification", json);
106107
}
107108

109+
private static JsonElement SerializeOccurrence(object occurrence)
110+
{
111+
if (occurrence is CronTickerOccurrenceEntity<CronTickerEntity> typed)
112+
return JsonSerializer.SerializeToElement(typed, DashboardJsonSerializerContext.Default.CronTickerOccurrenceEntityCronTickerEntity);
113+
return JsonSerializer.SerializeToElement(occurrence, CamelCaseOptions);
114+
}
115+
108116
public Task UpdateTimeTickerFromInternalFunctionContext<TTimeTicker>(InternalFunctionContext internalFunctionContext)
109117
where TTimeTicker : TimeTickerEntity<TTimeTicker>, new()
110118
{

0 commit comments

Comments
 (0)