From 10219484bc0510fbffa4d359f33ba0635900ce22 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 12 Aug 2024 20:59:20 -0500 Subject: store callbacks as plain objects --- src/slskd/Core/EventBus.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src') 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. /// - private ConcurrentDictionary>> Subscriptions { get; } = new(); + private ConcurrentDictionary> Subscriptions { get; } = new(); /// /// 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)(data)) + .ContinueWith(task => + { + Log.Error(task.Exception, "Subscriber {Name} for {Type} encountered an error: {Message}", subscriber.Key, typeof(T), task.Exception.Message); + }))); } /// @@ -100,13 +103,13 @@ public class EventBus Subscriptions.AddOrUpdate( key: typeof(T), - addValue: new ConcurrentDictionary>( - new Dictionary> { [subscriber] = (Func)callback }), + addValue: new ConcurrentDictionary( + new Dictionary { [subscriber] = callback }), updateValueFactory: (_, subscribers) => { subscribers.AddOrUpdate( key: subscriber, - addValue: (Func)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)callback; + return callback; }); return subscribers; -- cgit v1.2.3