Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactiveCommand.CreateFromTask vs ReactiveCommand.CreateFromObservable

Tags:

reactiveui

I've seen some discussion going on around using Observables instead of tasks with async/await. I currently make use of CreateFromTask almost exclusively. I've been trying to understand the reasoning behind using CreateFromObservable instead of CreateFromTask.

And if so, what would be the best way to convert a CreateFromTask to a CreateFromObservable.

like image 750
Brian Jenkins Avatar asked Feb 21 '17 18:02

Brian Jenkins


1 Answers

CreateFromTask is really only there as a helper because we live in a predominantly Task based world :-) In a perfectly reactive world all the libraries you use would just expose Observables. Then you can just be end to end Observables. But seeing as how this isn't the case RxUI includes some helpers to easily pull Tasks into the Commands.

If you look here https://github.com/reactiveui/ReactiveUI/issues/1245

You'll see there's even a discussion to just get rid of these helpers.

if you look at the code for "CreateFromTask" all it does is call ToObservble() on the task to convert it to an Observable and then the code calls CreateFromObservable

https://github.com/reactiveui/ReactiveUI/blob/develop/src/ReactiveUI/ReactiveCommand.cs#L418

So to that question I would just say calling ToObservable on the Task is the best way to convert it. You'll need to include this using statement

using System.Reactive.Threading.Tasks

What I normally do is just wrap all my Task based libraries with a facade and expose them using ToObervable. If you're going with a reactive solutions it will just make life easier to work in the land of Observables opposed to mixing and matching.

The reasoning behind CreateFromObservable over CreateFromTask is that the library assumes your solution is primarily reactive so that's going to be the primary way it is going to expect things. All the other ways to create those Commands are really just helpers that eventually make there way to CreateFromObservable

like image 149
Shane Neuville Avatar answered Sep 29 '22 19:09

Shane Neuville