Skip to content

New experimental operator to switch the types on the completion of empty Observables. #3056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

abersnaze
Copy link
Contributor

For the issue #3037 a new operator that let consumer of an empty Observable tie additional work to follow after the completion.

@davidmoten
Copy link
Collaborator

Seems like an ok idea to me but switchEmpty and switchIfEmpty are confusing to distinguish though I like them being close together alphabetically for discoverability.

@NiteshKant
Copy link
Member

aah @abersnaze I did not see that you have the changes here for #3037. I have created a PR #3060 for it too, although it contains an additional operator mergeError

@NiteshKant
Copy link
Member

I implemented switchEmpty, as you call it, as a switchMap().concatWith(), is there a reason why that does not work?

@abersnaze
Copy link
Contributor Author

I tried to reuse the switchIfEmpty and merge the switchEmpty functionality but couldn't get the generics to work. I decided to copy and edit because switchIfEmpty looked complicated enough with backpressure support.

***************
*** 65,79 ****

          @Override
          public void onCompleted() {
!             if (!empty) {
!                 child.onCompleted();
!             } else if (!child.isUnsubscribed()) {
                  subscribeToAlternate();
              }
          }
--- 64,76 ----

          @Override
          public void onCompleted() {
!             if (!child.isUnsubscribed()) {
                  subscribeToAlternate();
              }
          }
***************
*** 85,92 ****

          @Override
          public void onNext(T t) {
!             empty = false;
!             child.onNext(t);
              arbiter.produced(1);
          }
      }
--- 82,88 ----

          @Override
          public void onNext(T t) {
!             child.onError(new RuntimeException("switchEmpty used on a non empty observable. Possible fix is to add .ignoreElements() before .switchEmpty()."));
              arbiter.produced(1);
          }
      }

The overhead of the switchMap and concatWith seem pretty high for such a constrained use case.

@NiteshKant
Copy link
Member

@abersnaze I updated my PR with the things we discussed offline and it now has custom operator implementations, optimized for this usecase where the source Observable is never expected to emit any item.

@benjchristensen
Copy link
Member

Closing out so we can merge these efforts with #3060 after agreeing upon naming: #3037 (comment)

@abersnaze abersnaze deleted the switchEmpty branch August 12, 2015 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants