reactive: Sample never completes if the sampler sequence completes before the sampled sequence

Is this expected behavior? It looks to me that if sampler terminates, then there is a guarantee that there will be no more values coming out of Sample, and that it could be safely terminated. I was surprised to find that it never terminates. Is there some problematic scenario where the current behavior would be useful?

To reproduce:

var data = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(0.5)).Take(10);
var sampler = Observable.Timer(TimeSpan.FromSeconds(1));
var result = data.Sample(sampler).Materialize();
result.Subscribe(Console.WriteLine);
Console.ReadLine();

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 31 (16 by maintainers)

Commits related to this issue

Most upvoted comments

@RxDave cool, I never noticed DefaultIfEmpty. However, it looks like you still need to couple it with a TakeLast(1), because if the sequence is non-empty it just starts streaming all the elements from the beginning, which is not what you want if you want to “TakeUntil the end”.

However, after checking just now I think I found an even better alternative: LastOrDefaultAsync returns the last element of an observable sequence or a default value if no such element exists. I think I’ll use this one from now on, nice!

I think this is due to the data observable never completing, and is by design. Here’s a snapshot of how it looks as an Rx marble diagram:

Note where the sequence terminates…