Search code examples
rxjsrx-javaobservablerx-java2reactivex

Start emitting after another observable emitted its first item


I have this simple usecase that I can't seem to figure out.

Given 2 observables, A and B

A starts emitting items after B emits it's first item, and B can continue to emit items, which should no longer affect A.

I need this because A needs the first value emitted by B to start emitting.

I've tried both skipUntil and startWith to no avail.


Solution

  • I think flatMap can help you do what you want:

    Observable<Integer> createA(Observable<Integer> B) {
        return B.take(1).flatMap(b -> {
            /* replace with your Observable A implementation here */
            BehaviorSubject<Integer> subjectA = BehaviorSubject.create();
            return subjectA;
        });
    }
    

    This method returns an Observable A, which begins emitting only after it has been initialized with the first value emitted by Observable B.