0

In v2.2.5 of the Rx.NET library, there is an operator named Waitthat is defined as so:

public virtual TSource Wait<TSource>(IObservable<TSource> source) 

Neither the class library reference on MSDN nor this page mention this operator.

From looking at its implementation, which is a bit too cumbersome to follow, I am guessing it waits for the observable to produce all its elements and returns the last element if the observable had any elements, and if not, it returns the default(TSource). But I am not sure.

If this is correct, then how is it different from LastOrDefaultAsync?

What does it actually do?

3 Answers 3

1

The intellisense documentation seems pretty accurate

Waits for the observable sequence to complete and returns the last element of the sequence. If the sequence terminates with an OnError notification, the exception is thrown.

https://github.com/Reactive-Extensions/Rx.NET/blob/master/Rx.NET/Source/System.Reactive.Linq/Reactive/Linq/Observable.Blocking.cs#L493

So the operator will block the calling thread (YUCK!) until the sequence completes and then yield the last value.

LastOrDefaultAsync in contrast returns an IObservable<T> so is not blocking.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. You beat me to it. I immediately found the answer after posting the question.
1

The documentation for the methods are on the Observable class, not the query language implementation.

Waits for the observable sequence to complete and returns the last element of the sequence.
If the sequence terminates with an OnError notification, the exception is throw.

https://github.com/Reactive-Extensions/Rx.NET/blob/v2.2.5/Rx.NET/Source/System.Reactive.Linq/Reactive/Linq/Observable.Blocking.cs#L493

It's essentially a synonym of Last<TSource>().

Wait

Last

1 Comment

Thank you. I just realized after posting the question that I was looking at the wrong place.
0

The description of Wait provided in the question is not fully correct.

Here are the similarities between Wait and LastOrDefaultAsync:

  1. Both logically wait to receive all the values in the source observable. But as Lee Cambell points out in his answer, Wait blocks the current thread while LastOrDefaultAsync does not.

Here is the summary of differences between Wait and LastOrDefaultAsync:

  1. If there are no elements in the observable sequence, Wait throws an exception; LastOrDefault returns default(TSource).

  2. If an exception occurs during the observation of the observable, Wait reports the exception by invoking observer.OnError but then also throws the exception immediately afterwards; LastOrDefaultAsync only reports the exception by calling observer.OnError on all subscribed observers. However, on error, in both the cases, the observation is stopped.

The XML documentation that comes with the source code (or even with the binary distribution either via NuGet or through the MSI installer) for Rx explains thus:

Waits for the observable sequence to complete and returns the last element of the sequence. If the sequence terminates with an OnError notification, the exception is throw.

Exceptions

Throws ArgumentNullException if source is null.

Throws InvalidOperationException if the source sequence is empty.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.