aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-08-12 20:59:20 -0500
committerJP Dillingham <jp@dillingham.me>2024-08-12 20:59:20 -0500
commit10219484bc0510fbffa4d359f33ba0635900ce22 (patch)
tree813dcf2781cc5ab32a922eee49ae5ef440bd4a71 /src/slskd
parent0f9b9601d96f347e4c011a16a3669f053ae21889 (diff)
downloadslskd-10219484bc0510fbffa4d359f33ba0635900ce22.tar.gz
store callbacks as plain objects
Diffstat (limited to 'src/slskd')
-rw-r--r--src/slskd/Core/EventBus.cs23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/slskd/Core/EventBus.cs b/src/slskd/Core/EventBus.cs
index 9076f947..5ac6b0a5 100644
--- a/src/slskd/Core/EventBus.cs
+++ b/src/slskd/Core/EventBus.cs
@@ -54,7 +54,7 @@ public class EventBus
/// Note that the value is a dictionary to prevent multiple subscriptions from the same subscriber, and to
/// support unsubscribing.
/// </remarks>
- private ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, Task>>> Subscriptions { get; } = new();
+ private ConcurrentDictionary<Type, ConcurrentDictionary<string, object>> Subscriptions { get; } = new();
/// <summary>
/// Raises an event.
@@ -72,12 +72,15 @@ public class EventBus
return;
}
+ Log.Debug("{Count} subscribers for {Type}", subscribers.Count, typeof(T));
+
// we don't care about any of these tasks; contractually we are only obligated to invoke them
- _ = subscribers.Select(subscriber =>
- Task.Run(() => subscriber.Value(data)).ContinueWith(task =>
- {
- Log.Error(task.Exception, "Subscriber {Name} for {Type} encountered an error: {Message}", subscriber.Key, typeof(T), task.Exception.Message);
- }, continuationOptions: TaskContinuationOptions.OnlyOnFaulted));
+ _ = Task.WhenAll(subscribers.Select(subscriber =>
+ Task.Run(() => (subscriber.Value as Func<T, Task>)(data))
+ .ContinueWith(task =>
+ {
+ Log.Error(task.Exception, "Subscriber {Name} for {Type} encountered an error: {Message}", subscriber.Key, typeof(T), task.Exception.Message);
+ })));
}
/// <summary>
@@ -100,13 +103,13 @@ public class EventBus
Subscriptions.AddOrUpdate(
key: typeof(T),
- addValue: new ConcurrentDictionary<string, Func<object, Task>>(
- new Dictionary<string, Func<object, Task>> { [subscriber] = (Func<object, Task>)callback }),
+ addValue: new ConcurrentDictionary<string, object>(
+ new Dictionary<string, object> { [subscriber] = callback }),
updateValueFactory: (_, subscribers) =>
{
subscribers.AddOrUpdate(
key: subscriber,
- addValue: (Func<object, Task>)callback,
+ addValue: callback,
updateValueFactory: (_, existingSubscription) =>
{
if (existingSubscription is not null)
@@ -114,7 +117,7 @@ public class EventBus
Log.Debug("Warning! {Type} subscriber {Name} attempted to create a redundant subscription. The existing subscription was overwritten.", typeof(T), subscriber);
}
- return (Func<object, Task>)callback;
+ return callback;
});
return subscribers;