rxdart icon indicating copy to clipboard operation
rxdart copied to clipboard

switchIfEmpty: close stream when errored

Open bytepoets-mzi opened this issue 5 years ago • 4 comments

Hi,

the behavior of switchIfEmpty doesn't handle exceptions in the stream: when an exception is thrown, you receive an error but you also get the fallback value. I'm not too much into ReactiveX, but the following code works in RxJava:

val observer = TestObserver<Int>()
Observable
        .error<Int>(RuntimeException())
        .switchIfEmpty(Observable.just(1))
        .subscribe(observer)

observer.assertNoValues()
observer.assertError(RuntimeException::class.java)

But it doesn't work in rxdart.

PS: Is there an assert(false) in Dart? expect(true, false) does the job, but …

bytepoets-mzi avatar Sep 17 '19 15:09 bytepoets-mzi

Codecov Report

Merging #330 into master will decrease coverage by 0.09%. The diff coverage is 100%.

@@            Coverage Diff            @@
##           master     #330     +/-   ##
=========================================
- Coverage   93.61%   93.52%   -0.1%     
=========================================
  Files          63       63             
  Lines        2145     2146      +1     
=========================================
- Hits         2008     2007      -1     
- Misses        137      139      +2

codecov-io avatar Sep 17 '19 15:09 codecov-io

Hi there,

In Dart Streams, you specify whether or not your subscription terminates when an Error occurs, by default, the Stream just keeps going after an Error is thrown:

So if you do not want the fallback to take place, provide cancelOnError true when using listen.

frankpepermans avatar Oct 04 '19 09:10 frankpepermans

It's actually a really interesting question -- what does "Empty" mean in terms of Dart Streams. If an error is emitted, does that mean the Stream is no longer "empty"?

From one perspective -- yes! Because the stream emitted SOMETHING, just not a data event.

From another perspective -- no! Because it hasn't emitted any data events.

IMO, I like this change. I could see that emitting an error event does not mean the Stream is completely empty and that is has some kind of event. Therefore, I'm 👍 on making this change.

What do you think, Frank?

brianegan avatar Oct 14 '19 11:10 brianegan

Tough one!

Been thinking about it, and could't decide pro or con. It doesn't help that in Dart, a Stream just can go on after an Error (or not, if you flag it to cancel).

How about we instead provide switchIfEmpty({bool ignoreErrors = false})?

frankpepermans avatar Nov 07 '19 14:11 frankpepermans