sdgfsdh sdgfsdh - 21 days ago 8
C# Question

Is there an extension like StartWith but for the end of an observable sequence?

I want to append an extra "closing" item to my observable sequence. Is there a Reactive LINQ extension that is like

StartWith
but for the end of an observable sequence?

Here is an approximation of what I want, although it doesn't seem to fire the last element:

public static IObservable<TSource> EndWith<TSource>(this IObservable<TSource> source, TSource element)
{
return source.Concat(Observable.Return(element));
}

Answer

Your 'approximation' is the correct answer. Concat will add element to the end of your source stream, but only if:

  1. Source completes
  2. Source does not error

Perhaps if you aren't getting it, your source stream isn't actually terminating?

It might be better if you show us more of your code etc. There isn't an 'out of the box' API method (like StartsWith) that does this for you, but using Concat + Observable.Return is a pretty reasonable way to do it yourself.

Comments