diff options
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Events/EventBus.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Events/EventService.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Events/Types/Event.cs (renamed from src/slskd/Events/Types/EventRecord.cs) | 14 | ||||
| -rw-r--r-- | src/slskd/Events/Types/Events.cs | 6 |
4 files changed, 18 insertions, 18 deletions
diff --git a/src/slskd/Events/EventBus.cs b/src/slskd/Events/EventBus.cs index bc05e1b8..c3e418e7 100644 --- a/src/slskd/Events/EventBus.cs +++ b/src/slskd/Events/EventBus.cs @@ -71,12 +71,12 @@ public class EventBus /// <param name="data">The event data.</param> /// <typeparam name="T">The Type of the event.</typeparam> public virtual void Raise<T>(T data) - where T : Event + where T : BaseEvent { Log.Debug("Handling {Type}: {Data}", typeof(T), data); // save the event to the database before broadcasting to consumers - Events.Add(EventRecord.From<T>(data)); + Events.Add(Event.From<T>(data)); // broadcast the event in a fire-and-forget fashion // we don't need to wait for anything, just need to kick off the tasks diff --git a/src/slskd/Events/EventService.cs b/src/slskd/Events/EventService.cs index c10f0d32..759dc8cf 100644 --- a/src/slskd/Events/EventService.cs +++ b/src/slskd/Events/EventService.cs @@ -40,21 +40,21 @@ public class EventService private ILogger Log { get; } = Serilog.Log.ForContext<EventService>(); /// <summary> - /// Adds the specified event <paramref name="record"/>. + /// Adds the specified event <paramref name="event"/>. /// </summary> - /// <param name="record">The record to add.</param> + /// <param name="event">The record to add.</param> /// <exception cref="ArgumentNullException">Thrown when the specified record is null.</exception> - public virtual void Add(EventRecord record) + public virtual void Add(Event @event) { - if (record is null) + if (@event is null) { - throw new ArgumentNullException(nameof(record)); + throw new ArgumentNullException(nameof(@event)); } try { using var context = ContextFactory.CreateDbContext(); - context.Add(record); + context.Add(@event); context.SaveChanges(); } catch (Exception ex) diff --git a/src/slskd/Events/Types/EventRecord.cs b/src/slskd/Events/Types/Event.cs index 8ca0ff4d..5b57aa5d 100644 --- a/src/slskd/Events/Types/EventRecord.cs +++ b/src/slskd/Events/Types/Event.cs @@ -1,4 +1,4 @@ -// <copyright file="EventRecord.cs" company="slskd Team"> +// <copyright file="Event.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -28,7 +28,7 @@ using Microsoft.EntityFrameworkCore; [Index(nameof(Timestamp))] [Index(nameof(Type))] -public record EventRecord +public record Event { public DateTime Timestamp { get; init; } = DateTime.UtcNow; public string Type { get; private set; } @@ -46,7 +46,7 @@ public record EventRecord }; /// <summary> - /// Converts the specified event <paramref name="e"/> into an instance of <see cref="EventRecord"/> + /// Converts the specified event <paramref name="e"/> into an instance of <see cref="Event"/> /// for the purpose of database storage. /// </summary> /// <remarks> @@ -58,12 +58,12 @@ public record EventRecord /// <param name="e">The Event to convert.</param> /// <typeparam name="T">The specific type of the Event.</typeparam> /// <returns>The converted EventRecord.</returns> - public static EventRecord From<T>(Event e) - where T : Event + public static Event From<T>(BaseEvent e) + where T : BaseEvent { // this will be 'slskd.NameOfEvent'; we want to chop off 'slskd.' and 'Event' = 'NameOf' var type = e.GetType().Name.Split('.').TakeLast(1).First(); - type = type.Substring(0, type.Length - nameof(Event).Length); + type = type.Substring(0, type.Length - "Event".Length); // chop off "Event" suffix // construct the data for the record by serializing the event and removing redundant properties var json = JsonSerializer.Serialize(e as T, JsonSerializerOptions); @@ -73,7 +73,7 @@ public record EventRecord data.Remove(nameof(Id).ToLower()); data.Remove(nameof(Data).ToLower()); - return new EventRecord + return new Event { Timestamp = e.Timestamp, Type = type, diff --git a/src/slskd/Events/Types/Events.cs b/src/slskd/Events/Types/Events.cs index bcfdb8d3..416074e6 100644 --- a/src/slskd/Events/Types/Events.cs +++ b/src/slskd/Events/Types/Events.cs @@ -20,20 +20,20 @@ namespace slskd.Events; using System; using slskd.Transfers; -public record Event +public abstract record BaseEvent { public Guid Id { get; } = Guid.NewGuid(); public DateTime Timestamp { get; init; } = DateTime.UtcNow; } -public sealed record DownloadFileCompleteEvent : Event +public sealed record DownloadFileCompleteEvent : BaseEvent { public string LocalFilename { get; init; } public string RemoteFilename { get; init; } public Transfer Transfer { get; init; } } -public sealed record DownloadDirectoryCompleteEvent : Event +public sealed record DownloadDirectoryCompleteEvent : BaseEvent { public string LocalDirectoryName { get; init; } public string RemoteDirectoryName { get; init; } |
