From ec4739d30f04f699fd93905c55adf66ff5d84fa5 Mon Sep 17 00:00:00 2001 From: emmano Date: Mon, 13 Sep 2021 13:57:03 -0500 Subject: [PATCH] coroutine-flow-support: Default scope to MainScope. (EO) --- .../com/instacart/formula/coroutines/FlowStream.kt | 8 +++----- .../instacart/formula/stopwatch/StopwatchFormula.kt | 11 ++--------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowStream.kt b/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowStream.kt index ebcd9844f..c34b74ce0 100644 --- a/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowStream.kt +++ b/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowStream.kt @@ -3,12 +3,10 @@ package com.instacart.formula.coroutines import com.instacart.formula.Cancelable import com.instacart.formula.Stream import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.MainScope import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import kotlinx.coroutines.runBlocking /** * Formula [Stream] adapter to enable coroutine's Flow use. @@ -26,7 +24,7 @@ interface FlowStream : Stream { * ``` */ inline fun fromFlow( - scope: CoroutineScope, + scope: CoroutineScope = MainScope(), crossinline create: () -> Flow ): Stream { return object : FlowStream { @@ -53,7 +51,7 @@ interface FlowStream : Stream { * @param key Used to distinguish this [Stream] from other streams. */ inline fun fromFlow( - scope: CoroutineScope, + scope: CoroutineScope = MainScope(), key: Any?, crossinline create: () -> Flow ): Stream { diff --git a/samples/stopwatch-coroutines/src/main/java/com/instacart/formula/stopwatch/StopwatchFormula.kt b/samples/stopwatch-coroutines/src/main/java/com/instacart/formula/stopwatch/StopwatchFormula.kt index eef77dece..4d9052ab2 100644 --- a/samples/stopwatch-coroutines/src/main/java/com/instacart/formula/stopwatch/StopwatchFormula.kt +++ b/samples/stopwatch-coroutines/src/main/java/com/instacart/formula/stopwatch/StopwatchFormula.kt @@ -4,24 +4,17 @@ import com.instacart.formula.Evaluation import com.instacart.formula.Formula import com.instacart.formula.FormulaContext import com.instacart.formula.coroutines.FlowStream -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.MainScope import kotlinx.coroutines.delay import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.flow.flowOf -import kotlinx.coroutines.flow.onEach import java.util.concurrent.TimeUnit -class StopwatchFormula(private val scope: CoroutineScope = MainScope()) : Formula { +class StopwatchFormula : Formula { data class State( val timePassedInMillis: Long, val isRunning: Boolean ) - private val analytics = StopwatchAnalytics() - override fun initialState(input: Unit): State = State( timePassedInMillis = 0, isRunning = true @@ -38,7 +31,7 @@ class StopwatchFormula(private val scope: CoroutineScope = MainScope()) : Formul ), updates = context.updates { if (state.isRunning) { - val incrementTimePassed = FlowStream.fromFlow(scope) { + val incrementTimePassed = FlowStream.fromFlow { flow { while(true) {