Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to transform an IObservable<T> to an IAsyncEnumerable<T>

I have an observable IObservable<T> / ISubject<T> and want to return that by using SignalR. SignalR has the concept of async streams where you have to return an IAsyncEnumerable<T>.

How can I transform an IObservable<T> to an IAsyncEnumerable<T>?

like image 607
Rookian Avatar asked Nov 18 '19 09:11

Rookian


People also ask

How do I convert an IEnumerable to iobservable?

If one has an IEnumerable of unknown type, there are two ways one can "convert it to IObservable": Copy all of the data in the object to a new collection which implements IObservable. If this is done, only changes made to the new collection will be reported. Changes made to the original will not.

What is the difference between iobservable and iobserver<T>?

The IObservable<T> interface represents the class that sends notifications (the provider); the IObserver<T> interface represents the class that receives them (the observer). T represents the class that provides the notification information. In some push-based notifications, the IObserver<T> implementation and T can represent the same type.

How does the iobservable<T> interface work?

The IObservable<T> interface does not make any assumptions about the number of observers or the order in which notifications are sent. The provider sends the following three kinds of notifications to the observer by calling IObserver<T> methods: The current data.

How can I use iasyncenumerable<t> on the client?

The way you could leverage IAsyncEnumerable<T> on the client is if that client is invoking some kind of streaming or making multiple requests to a server for a unique list of results (paging). Thanks for contributing an answer to Stack Overflow!


1 Answers

There is a ToAsyncEnumerable extension method in the System.Linq.Async assembly, available as a NuGet package here.

public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(
    this IObservable<TSource> source);
like image 149
Theodor Zoulias Avatar answered Oct 10 '22 19:10

Theodor Zoulias