Skip to content

Commit

Permalink
Remove inline functions from RxAction. (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux authored Mar 18, 2024
1 parent fe5cce8 commit 09d7adf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.FragmentFlowState
import com.instacart.formula.android.FragmentKey
import com.instacart.formula.android.BackCallback
import com.instacart.formula.android.FragmentEnvironment
import com.instacart.formula.rxjava3.toObservable
import com.instacart.formula.test.TestKey
import com.instacart.formula.test.TestKeyWithId
import com.instacart.formula.test.TestFragmentActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@ interface RxAction<Event : Any> : Action<Event> {
* }
* ```
*/
inline fun <Event : Any> fromObservable(
crossinline create: () -> Observable<Event>
fun <Event : Any> fromObservable(
create: () -> Observable<Event>
): Action<Event> {
return object : RxAction<Event> {

override fun observable(): Observable<Event> {
return create()
}

override fun key(): Any = Unit
}
return fromObservable(Unit, create)
}

/**
Expand All @@ -45,18 +38,11 @@ interface RxAction<Event : Any> : Action<Event> {
*
* @param key Used to distinguish this [Action] from other actions.
*/
inline fun <Event : Any> fromObservable(
fun <Event : Any> fromObservable(
key: Any?,
crossinline create: () -> Observable<Event>
create: () -> Observable<Event>
): Action<Event> {
return object : RxAction<Event> {

override fun observable(): Observable<Event> {
return create()
}

override fun key(): Any? = key
}
return RxActionImpl(key, create)
}
}

Expand All @@ -67,3 +53,16 @@ interface RxAction<Event : Any> : Action<Event> {
return Cancelable(disposable::dispose)
}
}

private data class RxActionImpl<Event : Any>(
private val key: Any?,
private val factory: () -> Observable<Event>
): RxAction<Event> {
override fun observable(): Observable<Event> {
return factory()
}

override fun key(): Any? {
return key
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.instacart.formula.rxjava3

import com.instacart.formula.RuntimeConfig
import com.instacart.formula.test.CountingInspector
import com.instacart.formula.types.InputIdentityFormula
import io.reactivex.rxjava3.core.Observable
import org.junit.Test

class RxJavaRuntimeTest {

@Test fun `toObservable with unit input and no config`() {
val formula = InputIdentityFormula<Unit>()
formula.toObservable().test().assertValues(Unit)
}

@Test fun `toObservable with unit input and config`() {
val inspector = CountingInspector()
val config = RuntimeConfig(inspector = inspector)
val formula = InputIdentityFormula<Unit>()
formula.toObservable(config).test().assertValues(Unit)
inspector.assertEvaluationCount(1)
}

@Test fun `toObservable with integer input and no config`() {
val formula = InputIdentityFormula<Int>()
formula.toObservable(0).test().assertValues(0)
}

@Test fun `toObservable with observable input and no config`() {
val formula = InputIdentityFormula<Int>()
formula.toObservable(Observable.just(0, 1, 2)).test().assertValues(0, 1, 2)
}
}

0 comments on commit 09d7adf

Please sign in to comment.