NimaAra / Easy.MessageHub

No need for .NET Events! A thread-safe, high performance & easy to use cross platform implementation of the Event Aggregator Pattern.
MIT License
258 stars 41 forks source link

Add an async publisher #26

Closed per-ove closed 8 months ago

per-ove commented 8 months ago

We use this a lot and are very happy with it. But there are an issue we encounter running publish from a GUI.

The publisher has no way to know many or how time consuming each action is. and sometimes this are blocking our main thread.

We have a suggestion to add a publishAsync. like the example below. What do you think?

Code snippet *****

public async Task PublishAsync<T>(T message)
{
    var localSubscriptions = _subscriptions.GetTheLatestSubscriptions();
    var msgType = typeof(T);
#if NET_STANDARD
    var msgTypeInfo = msgType.GetTypeInfo();
#endif
     _globalHandler?.Invoke(msgType, message);

      // ReSharper disable once ForCanBeConvertedToForeach | Performance Critical
      await Task.Run(() =>
      {
           for (var idx = 0; idx < localSubscriptions.Count; idx++)
               {
                    var subscription = localSubscriptions[idx];
#if NET_STANDARD
                   if (!subscription.Type.GetTypeInfo().IsAssignableFrom(msgTypeInfo)) { continue; }
#else
                   if (!subscription.Type.IsAssignableFrom(msgType)) { continue; }
#endif
                   try
                   {
                        subscription.Handle(message);
                   }
                   catch (Exception e)
                   {
                       _globalErrorHandler?.Invoke(subscription.Token, e);
                   }
              }
          }).ConfigureAwait(false);
}    
NimaAra commented 8 months ago

Thank you for your interest in the library. Similar issues have been raised around async support. This library is in-memory and there is no IO involved therefore it is synchronous by design. The introduction of async will nullify the performance benefits of using the library.

The fix is simple as I have explained here.

per-ove commented 8 months ago

As I understand, the publish method are not fire and forget as it not return until all subscriptions are finished. I know I can fix this when invoking the publish method but it's easier to have a non blocking method in the library.

NimaAra commented 8 months ago

This library is meant to be in-memory, CPU bound and therefore synchronous (just like native .NET events) therefore any long running consumer should offload the work as soon as received (which is again the general pattern with .NET events).

You definitely do not want to be creating tasks like you are doing in the proposed snippet; Instead, you can wrap the MessageHub and use System.Threading.Channels which will nicely take care of offloading your publish/subscribe.

per-ove commented 8 months ago

Thank you for a good answer.