0

Using Rx observable stream, is there an effective way to get the first item, complete the source observable, but throw an exception if the second item arrives? Sounds like not a good situation to use Rx for, but is there a clever way to handle it?

Jaewan Kim
  • 45
  • 5
  • 1
    No, there's not a good way to do this. You cannot complete the source observable after one value and then expect another one to come. If you complete the observable then that's it - it's completed. – Enigmativity Jul 30 '17 at 12:10
  • I suspected as much. Does not fit the pattern. However, I was thinking about a pattern in which you get a first item and stash somewhere, and wait for a timeout before deciding to emit the first item if the second item does not arrive by then. I just see too many cases where you are pulling the entire result set of a database call before deciding. This pattern would not be useful with a hot observable, but somewhat cold ones like database cursor. – Jaewan Kim Jul 30 '17 at 18:13
  • That should be fairly easy to workout. – Enigmativity Jul 31 '17 at 00:53
  • This is getting close `var query = source.Publish(ss => ss.Take(1).Concat(Observable.Amb(Observable.Timer(TimeSpan.FromSeconds(1.0)).Select(x => -1), ss.Take(1).Concat(Observable.Throw(new Exception())))));`. – Enigmativity Jul 31 '17 at 01:14

1 Answers1

0

As far as I understand, this is a violation of the IObservable Contract, that clearly specifies that no extra items should arrive after either error or complete. So basically, you are checking for a situation that cannot (or should not) arise.

marsop
  • 323
  • 4
  • 19