Search code examples
observablerx-swift

Listening to observable until or till another observable fires


I have a hot observable C which I want to listen to until another observable B fires (i.e. generates onNext event). B is a cold observable and I do not want to "wake up" it until yet another observable A fires.

In another words:

  • I am listening to C.
  • I am listening to A and as soon as it fires, I start listening to (thus waking up) B
  • As soon as B fires, I stop listening to C

This is my code:

    let b = Observable<Int>.create { s in
        print("b got awoken") // I want to see this line only after A fires
        s.onNext(666)
        s.onCompleted()
        return Disposables.create()
    }

    let c = Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
    c
        .takeUntil(b) // I do not want to listen to C after B has fired
        .subscribe(onNext: { print("C: \($0)") })
        .disposed(by: _bag)

    let a = PublishSubject<Int>()
    a
        .do(onNext:{ print("A: \($0)") })
        .flatMap {_ in return b } // After A fires, I "wake up" (or subsribe to) B
        .subscribe(onNext:{ print("B: \($0)") })
        .disposed(by: _bag)

    // Some delay to let C generate some events and listen to them
    DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
        print("anext 1")
        a.onNext(1)
    }

and this is the output:

b got awoken
anext 1
A: 1
b got awoken
B: 666

this is the desired output:

C: 0
C: 1
C: 2
C: 3
anext 1
A: 1
b got awoken
B: 666

How shall I manage these three observables to achieve the desired behaviour?


Solution

  • If I understand the question...

    func example(a: Observable<Int>, b: @escaping () -> Observable<Int>, c: Observable<Int>) -> Observable<Int> {
        return c.takeUntil(a.flatMap { _ in b() })
    }
    

    The above will subscribe to both a and c, then emit c's values until a, then b emits.