Coder Social home page Coder Social logo

Add async version about easy.messagehub HOT 6 CLOSED

nimaara avatar nimaara commented on July 3, 2024
Add async version

from easy.messagehub.

Comments (6)

sharok avatar sharok commented on July 3, 2024

Some example:

messageHub.SubscribeAsync<CoolData>(CoolDataUpdated);

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

Now, I have to use Wait.

from easy.messagehub.

NimaAra avatar NimaAra commented on July 3, 2024

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.

from easy.messagehub.

sharok avatar sharok commented on July 3, 2024

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.

from easy.messagehub.

NimaAra avatar NimaAra commented on July 3, 2024

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.

from easy.messagehub.

sharok avatar sharok commented on July 3, 2024

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

from easy.messagehub.

NimaAra avatar NimaAra commented on July 3, 2024

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.

from easy.messagehub.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.