switchMap 中的相同 Observable

Identical Observable inside a switchMap

请看下面的代码:

class FooImplTest {

    @Test
    fun sample1() {
        val booleanSubject = BehaviorSubject.create<Boolean>()
        val intSubject = BehaviorSubject.create<Int>()
        val stringSubject = BehaviorSubject.create<String>()

        val combineSecondAndThird = Observable.combineLatest(intSubject, stringSubject,
            { t1, t2 -> Pair(t1, t2) })

        intSubject.onNext(1)
        stringSubject.onNext("my str")

        booleanSubject.onNext(true)

        val o1 = combineSecondAndThird
            .switchMap { pair ->
                val i = pair.first
                val s = pair.second

                booleanSubject.switchMap {
                    intSubject.map { secondVal2 -> Triple(i,
                        "$s:$it", secondVal2) }
                }
            }
            .test()
            .assertValue(Triple( 1, "my str:true", 1))

        intSubject.onNext(3)

        o1.assertValueAt(1, Triple( 3, "my str:true", 3))
            .dispose()
    }

}

我觉得没必要在booleanSubject.switchMap里面订阅intSubject:

booleanSubject.map { Triple(i,"$s:$it", i) }

我只是想知道是否存在任何边缘情况,原始代码会产生与简化代码不同的结果。

你为什么不把所有三个主题都组合成 combineLatest,就像下面的例子一样?

@Test
fun sample1() {
    val booleanSubject = BehaviorSubject.create<Boolean>()
    val intSubject = BehaviorSubject.create<Int>()
    val stringSubject = BehaviorSubject.create<String>()

    val combined = Observable.combineLatest(
        intSubject,
        stringSubject,
        booleanSubject,
        Function3<Int, String, Boolean, Triple<Int, String, Int>> { i, s, b ->
            Triple(i, "$s:$b", i)
        })

    intSubject.onNext(1)
    stringSubject.onNext("my str")

    booleanSubject.onNext(true)

    val test = combined
        .test()
        .assertValue(Triple(1, "my str:true", 1))

    intSubject.onNext(3)

    test.assertValueAt(1, Triple(3, "my str:true", 3))
        .dispose()
}

测试通过,和以前一样。当所有值都同步发出时,这应该无关紧要,因为 combineSecondAndThird#switchMap 应该总是首先被调用。