Skip to content

How can I use OperatorConcat<T>? #3446

Closed
@fougere-mike

Description

@fougere-mike

I'm trying to write an operator then<R,T> which ignores all emissions from the source (except errors), and continues with a second Observable when completed.

Something like:

// Typical Case
Observable.just(1, 2, 3, 4, 5)
   .lift(new then<String, Integer>(Observable.just("Hello")))
   .subscribe(new Action1<String>() {
      @Override
      public void call(String s) {
         // Called once with "Hello"
      });

// Source Errors
Observable.<Integer>error(new RuntimeException())
   .lift(new then<String, Integer>(Observable.just("Hello")))  // <-- the second observable should never be subscribed to since the source error'd
   .subscribe(new Action1<String>() {
      @Override
      public void call(String s) {
         // Not Called

      }, new Action1<Throwable>() {
      @Override
      public void call(Throwable e) {
        System.out.println("Error: "+e); // Should be called with the RuntimeException from above

      }
   });

I've come up with an implementation using ignoreElements + map + concatWith:

    public static <R, T> Observable<? extends R> then(Observable<T> source, Observable<R> other) {
        return source
                .ignoreElements()
                .map(new Func1<T, R>() {
                    @Override
                    public R call(T integer) {
                        return null;
                    }
                }).concatWith(other);
    }

I'm quite new to writing custom operators, and I can't quite figure out how to translate that static function into an operator. I've written a few operators by composing the provided Operator* types, but I'm having trouble with this one.

Any help would be greatly appreciated :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions