From 7124884b493a85d893540ce0e3dee634b97409b4 Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Fri, 20 Mar 2020 13:02:47 +0000 Subject: [PATCH] Optimize Maybe.switchIfEmpty(Single) --- .../com/badoo/reaktive/maybe/SwitchIfEmpty.kt | 22 +++++- .../reaktive/maybe/SwitchIfEmptySingleTest.kt | 77 +++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 reaktive/src/commonTest/kotlin/com/badoo/reaktive/maybe/SwitchIfEmptySingleTest.kt diff --git a/reaktive/src/commonMain/kotlin/com/badoo/reaktive/maybe/SwitchIfEmpty.kt b/reaktive/src/commonMain/kotlin/com/badoo/reaktive/maybe/SwitchIfEmpty.kt index ba6bb2f5d..2d7e87eae 100644 --- a/reaktive/src/commonMain/kotlin/com/badoo/reaktive/maybe/SwitchIfEmpty.kt +++ b/reaktive/src/commonMain/kotlin/com/badoo/reaktive/maybe/SwitchIfEmpty.kt @@ -6,7 +6,9 @@ import com.badoo.reaktive.base.SuccessCallback import com.badoo.reaktive.base.subscribeSafe import com.badoo.reaktive.disposable.Disposable import com.badoo.reaktive.single.Single -import com.badoo.reaktive.single.asMaybe +import com.badoo.reaktive.single.SingleCallbacks +import com.badoo.reaktive.single.SingleObserver +import com.badoo.reaktive.single.single fun Maybe.switchIfEmpty(other: Maybe): Maybe = maybe { emitter -> @@ -27,5 +29,19 @@ fun Maybe.switchIfEmpty(other: Maybe): Maybe = } fun Maybe.switchIfEmpty(other: Single): Single = - switchIfEmpty(other.asMaybe()) - .asSingleOrError() + single { emitter -> + subscribe( + object : MaybeObserver, SuccessCallback by emitter, ErrorCallback by emitter { + override fun onSubscribe(disposable: Disposable) { + emitter.setDisposable(disposable) + } + + override fun onComplete() { + other.subscribeSafe( + object : SingleObserver, Observer by this, SingleCallbacks by emitter { + } + ) + } + } + ) + } diff --git a/reaktive/src/commonTest/kotlin/com/badoo/reaktive/maybe/SwitchIfEmptySingleTest.kt b/reaktive/src/commonTest/kotlin/com/badoo/reaktive/maybe/SwitchIfEmptySingleTest.kt new file mode 100644 index 000000000..c96d7e3ef --- /dev/null +++ b/reaktive/src/commonTest/kotlin/com/badoo/reaktive/maybe/SwitchIfEmptySingleTest.kt @@ -0,0 +1,77 @@ +package com.badoo.reaktive.maybe + +import com.badoo.reaktive.single.singleOf +import com.badoo.reaktive.test.base.assertError +import com.badoo.reaktive.test.base.assertSubscribed +import com.badoo.reaktive.test.base.hasSubscribers +import com.badoo.reaktive.test.maybe.TestMaybe +import com.badoo.reaktive.test.single.TestSingle +import com.badoo.reaktive.test.single.assertSuccess +import com.badoo.reaktive.test.single.test +import kotlin.test.Test +import kotlin.test.assertFalse + +class SwitchIfEmptySingleTest : MaybeToSingleTests by MaybeToSingleTestsImpl({ switchIfEmpty(singleOf(0)) }) { + + private val upstream = TestMaybe() + private val other = TestSingle() + private val observer = upstream.switchIfEmpty(other).test() + + @Test + fun succeeds_WHEN_upstream_succeeded_with_non_null_value() { + upstream.onSuccess(0) + + observer.assertSuccess(0) + } + + @Test + fun succeeds_WHEN_upstream_succeeded_with_null_value() { + upstream.onSuccess(null) + + observer.assertSuccess(null) + } + + @Test + fun does_not_subscribe_to_other_IF_upstream_not_finished() { + assertFalse(other.hasSubscribers) + } + + @Test + fun does_not_subscribe_to_other_WHEN_upstream_succeeded() { + upstream.onSuccess(0) + + assertFalse(other.hasSubscribers) + } + + @Test + fun subscribes_to_other_WHEN_upstream_completed() { + upstream.onComplete() + + observer.assertSubscribed() + } + + @Test + fun succeeds_WHEN_upstream_completed_and_other_succeeded_with_non_null_value() { + upstream.onComplete() + other.onSuccess(0) + + observer.assertSuccess(0) + } + + @Test + fun succeeds_WHEN_upstream_completed_and_other_succeeded_with_null_value() { + upstream.onComplete() + other.onSuccess(null) + + observer.assertSuccess(null) + } + + @Test + fun produces_error_WHEN_upstream_completed_and_other_produced_error() { + val error = Exception() + upstream.onComplete() + other.onError(error) + + observer.assertError(error) + } +}