ReactiveCommand.CreateFromTask vs ReactiveCommand.CreateFromObservable
Asked Answered
P

1

7

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.

Paddlefish answered 21/2, 2017 at 18:31 Comment(0)
I
6

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

Interrelated answered 21/2, 2017 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.