diff --git a/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt b/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt index df3fdefe..227a938c 100644 --- a/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt +++ b/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt @@ -34,6 +34,7 @@ object FlowRuntime { val runtimeFactory = { FormulaRuntime( + dispatcher = , threadChecker = threadChecker, formula = formula, onOutput = this::trySendBlocking, diff --git a/formula/build.gradle.kts b/formula/build.gradle.kts index 0ec95f5a..ca06586d 100644 --- a/formula/build.gradle.kts +++ b/formula/build.gradle.kts @@ -25,6 +25,7 @@ tasks.withType().all { dependencies { implementation(libs.kotlin) + implementation(libs.coroutines) testImplementation(project(":formula-test")) testImplementation(project(":formula-rxjava3")) diff --git a/formula/src/main/java/com/instacart/formula/FormulaRuntime.kt b/formula/src/main/java/com/instacart/formula/FormulaRuntime.kt index d69b0d17..441de3cb 100644 --- a/formula/src/main/java/com/instacart/formula/FormulaRuntime.kt +++ b/formula/src/main/java/com/instacart/formula/FormulaRuntime.kt @@ -4,12 +4,14 @@ import com.instacart.formula.internal.FormulaManager import com.instacart.formula.internal.FormulaManagerImpl import com.instacart.formula.internal.ManagerDelegate import com.instacart.formula.internal.ThreadChecker +import kotlinx.coroutines.MainCoroutineDispatcher import java.util.LinkedList /** * Takes a [Formula] and creates an Observable from it. */ class FormulaRuntime( + private val dispatcher: MainCoroutineDispatcher, private val threadChecker: ThreadChecker, private val formula: IFormula, private val onOutput: (Output) -> Unit, @@ -60,7 +62,14 @@ class FormulaRuntime( this.key = formula.key(input) if (initialization) { - manager = FormulaManagerImpl(this, implementation, input, loggingType = formula::class, inspector = inspector) + manager = FormulaManagerImpl( + mainCoroutineDispatcher = dispatcher, + delegate = this, + formula = implementation, + initialInput = input, + loggingType = formula::class, + inspector = inspector, + ) run() hasInitialFinished = true diff --git a/formula/src/main/java/com/instacart/formula/internal/ChildrenManager.kt b/formula/src/main/java/com/instacart/formula/internal/ChildrenManager.kt index 33c3c368..4a73be6e 100644 --- a/formula/src/main/java/com/instacart/formula/internal/ChildrenManager.kt +++ b/formula/src/main/java/com/instacart/formula/internal/ChildrenManager.kt @@ -110,9 +110,10 @@ internal class ChildrenManager( val childFormulaHolder = children.findOrInit(key) { val implementation = formula.implementation() FormulaManagerImpl( - delegate, - implementation, - input, + mainCoroutineDispatcher = delegate.mainCoroutineDispatcher, + delegate = delegate, + formula = implementation, + initialInput = input, loggingType = formula::class, inspector = inspector ) diff --git a/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt b/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt index f1c2a998..83764a51 100644 --- a/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt +++ b/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt @@ -7,6 +7,7 @@ import com.instacart.formula.IFormula import com.instacart.formula.Inspector import com.instacart.formula.Snapshot import com.instacart.formula.Transition +import kotlinx.coroutines.MainCoroutineDispatcher import java.util.LinkedList import kotlin.reflect.KClass @@ -18,6 +19,7 @@ import kotlin.reflect.KClass * a state change, it will rerun [Formula.evaluate]. */ internal class FormulaManagerImpl( + internal val mainCoroutineDispatcher: MainCoroutineDispatcher, private val delegate: ManagerDelegate, private val formula: Formula, initialInput: Input, diff --git a/formula/src/main/java/com/instacart/formula/internal/ListenerImpl.kt b/formula/src/main/java/com/instacart/formula/internal/ListenerImpl.kt index 3cf42ca6..c583143d 100644 --- a/formula/src/main/java/com/instacart/formula/internal/ListenerImpl.kt +++ b/formula/src/main/java/com/instacart/formula/internal/ListenerImpl.kt @@ -2,6 +2,9 @@ package com.instacart.formula.internal import com.instacart.formula.Listener import com.instacart.formula.Transition +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.MainCoroutineDispatcher +import kotlinx.coroutines.launch /** * Note: this class is not a data class because equality is based on instance and not [key]. @@ -15,11 +18,14 @@ internal class ListenerImpl(internal var key: Any) : Liste internal lateinit var transition: Transition override fun invoke(event: EventT) { - // TODO: log if null listener (it might be due to formula removal or due to callback removal) - val manager = manager ?: return - - val deferredTransition = DeferredTransition(this, transition, event) - manager.onPendingTransition(deferredTransition) + val dispatcher = manager?.mainCoroutineDispatcher ?: return + GlobalScope.launch(dispatcher.immediate) { + // TODO: log if null listener (it might be due to formula removal or due to callback removal) + val manager = manager ?: return@launch + + val deferredTransition = DeferredTransition(this@ListenerImpl, transition, event) + manager.onPendingTransition(deferredTransition) + } } fun disable() {