Skip to content

Commit

Permalink
coroutine-flow-support: Default scope to MainScope. (EO)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmano committed Sep 13, 2021
1 parent bb005d6 commit ec4739d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,7 +24,7 @@ interface FlowStream<Message> : Stream<Message> {
* ```
*/
inline fun <Message> fromFlow(
scope: CoroutineScope,
scope: CoroutineScope = MainScope(),
crossinline create: () -> Flow<Message>
): Stream<Message> {
return object : FlowStream<Message> {
Expand All @@ -53,7 +51,7 @@ interface FlowStream<Message> : Stream<Message> {
* @param key Used to distinguish this [Stream] from other streams.
*/
inline fun <Message> fromFlow(
scope: CoroutineScope,
scope: CoroutineScope = MainScope(),
key: Any?,
crossinline create: () -> Flow<Message>
): Stream<Message> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit, StopwatchFormula.State, StopwatchRenderModel> {
class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> {

data class State(
val timePassedInMillis: Long,
val isRunning: Boolean
)

private val analytics = StopwatchAnalytics()

override fun initialState(input: Unit): State = State(
timePassedInMillis = 0,
isRunning = true
Expand All @@ -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) {
Expand Down

0 comments on commit ec4739d

Please sign in to comment.