Skip to content

Commit

Permalink
Add coroutines main thread to formula-core.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Jan 2, 2024
1 parent 62c935f commit 4e587d7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object FlowRuntime {

val runtimeFactory = {
FormulaRuntime(
dispatcher = ,
threadChecker = threadChecker,
formula = formula,
onOutput = this::trySendBlocking,
Expand Down
1 change: 1 addition & 0 deletions formula/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {

dependencies {
implementation(libs.kotlin)
implementation(libs.coroutines)

testImplementation(project(":formula-test"))
testImplementation(project(":formula-rxjava3"))
Expand Down
11 changes: 10 additions & 1 deletion formula/src/main/java/com/instacart/formula/FormulaRuntime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Output> from it.
*/
class FormulaRuntime<Input : Any, Output : Any>(
private val dispatcher: MainCoroutineDispatcher,
private val threadChecker: ThreadChecker,
private val formula: IFormula<Input, Output>,
private val onOutput: (Output) -> Unit,
Expand Down Expand Up @@ -60,7 +62,14 @@ class FormulaRuntime<Input : Any, Output : Any>(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,6 +19,7 @@ import kotlin.reflect.KClass
* a state change, it will rerun [Formula.evaluate].
*/
internal class FormulaManagerImpl<Input, State, Output>(
internal val mainCoroutineDispatcher: MainCoroutineDispatcher,
private val delegate: ManagerDelegate,
private val formula: Formula<Input, State, Output>,
initialInput: Input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -15,11 +18,14 @@ internal class ListenerImpl<Input, State, EventT>(internal var key: Any) : Liste
internal lateinit var transition: Transition<Input, State, EventT>

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() {
Expand Down

0 comments on commit 4e587d7

Please sign in to comment.