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 async version #6

Closed sharok closed 7 years ago

sharok commented 7 years ago

Thanks for the library. I use async a lot in my project, so it would be great to have async methods.

sharok commented 7 years ago

Some example:

messageHub.SubscribeAsync<CoolData>(CoolDataUpdated);

private async Task CoolDataUpdated(CoolData data)
{
    await DoSmthWithCoolDataAsync(data);     
}

Now, I have to use Wait.

NimaAra commented 7 years ago

messageHub.Subscribe is CPU bound and therefore does not require an async implementation; However you can certainly modify your handler method to offload the received message to one or more consumer threads.

I cannot think of a valid use-case for adding async overloads to the library.

sharok commented 7 years ago

I don't completely understand what is CPU bound in this context. I will google about it. But, if you think that async overloads don't needed, then maybe add a overloads for different async handles, in order to avoid using Wait. So, I belive adding Func<Task> delegate helps me avoid Wait method and allows use await. BTW, I use library in the ASP.NET Core project.

NimaAra commented 7 years ago

Inside your CoolDataUpdated method pass the payload to another method with the async Task signature, there you can await it with no problem.

Also in your example, you do not need to include the Task as the return type. The following works with no problem:

MessageHub.Instance.Subscribe<string>(Handle);

private async void Handle(string payload)
{
    await Task.Delay(TimeSpan.FromMilliseconds(100));
    Console.WriteLine(payload);
}

I am unable to understand why you would need to use such pattern.

sharok commented 7 years ago

It's a bad idea to use async void especially in the ASP.NET Core. You can read answers from this Stack Overflow question.

NimaAra commented 7 years ago

None of those points apply to your example hence why I said "in your example" either way there are many ways to skin the cat you intend to skin. So either use the example I have provided or offload to another method.