Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a few changes to function and parameter names #3

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ interface ActionStateProducer<Action : Any, State : Any> : StateProducer<State>
/**
* Syntactic sugar for creating a [Mutation]
*/
inline fun <State> mutation(noinline mutation: State.() -> State): Mutation<State> = mutation
inline fun <State> mutationOf(noinline mutation: State.() -> State): Mutation<State> = mutation

/**
* Identity [Mutation] function; semantically a no op [Mutation]
*/
fun <State : Any> identity(): Mutation<State> = mutation { this }
fun <State : Any> identity(): Mutation<State> = mutationOf { this }

/**
* Combines two [Mutation] instances into a single [Mutation]
*/
operator fun <T : Any> Mutation<T>.plus(other: Mutation<T>) = mutation<T> inner@{
operator fun <T : Any> Mutation<T>.plus(other: Mutation<T>) = mutationOf<T> inner@{
val result = this@plus(this@inner)
other.invoke(result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ typealias SuspendingStateHolder<State> = StateHolder<suspend () -> State>
fun <Action : Any, State : Any> CoroutineScope.actionStateFlowProducer(
initialState: State,
started: SharingStarted = SharingStarted.WhileSubscribed(DEFAULT_STOP_TIMEOUT_MILLIS),
mutationFlows: List<Flow<Mutation<State>>> = listOf(),
inputs: List<Flow<Mutation<State>>> = listOf(),
stateTransform: (Flow<State>) -> Flow<State> = { it },
actionTransform: SuspendingStateHolder<State>.(Flow<Action>) -> Flow<Mutation<State>> = { emptyFlow() }
): ActionStateProducer<Action, StateFlow<State>> = ActionStateFlowProducer(
coroutineScope = this,
initialState = initialState,
started = started,
mutationFlows = mutationFlows,
inputs = inputs,
stateTransform = stateTransform,
actionTransform = actionTransform
)
Expand All @@ -67,7 +67,7 @@ private class ActionStateFlowProducer<Action : Any, State : Any>(
coroutineScope: CoroutineScope,
initialState: State,
started: SharingStarted,
mutationFlows: List<Flow<Mutation<State>>>,
inputs: List<Flow<Mutation<State>>>,
stateTransform: (Flow<State>) -> Flow<State> = { it },
actionTransform: SuspendingStateHolder<State>.(Flow<Action>) -> Flow<Mutation<State>>
) : ActionStateProducer<Action, StateFlow<State>>,
Expand All @@ -86,7 +86,7 @@ private class ActionStateFlowProducer<Action : Any, State : Any>(
initialState = initialState,
started = started,
stateTransform = stateTransform,
mutationFlows = mutationFlows + actionTransform(stateReader, actions.receiveAsFlow())
inputs = inputs + actionTransform(stateReader, actions.receiveAsFlow())
)

override val accept: (Action) -> Unit = { action ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.tunjid.mutator.coroutines

import com.tunjid.mutator.Mutation
import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.flatMapConcat
Expand All @@ -33,7 +33,7 @@ import kotlinx.coroutines.flow.mapLatest
inline fun <T, State> Flow<T>.mapToMutation(
crossinline mapper: State.(T) -> State
): Flow<Mutation<State>> =
map { mutation { mapper(it) } }
map { mutationOf { mapper(it) } }

/**
* Maps the latest emission of [T] into a single mutation of [State]
Expand All @@ -42,7 +42,7 @@ inline fun <T, State> Flow<T>.mapToMutation(
inline fun <T, State> Flow<T>.mapLatestToMutation(
crossinline mapper: State.(T) -> State
): Flow<Mutation<State>> =
mapLatest { mutation { mapper(it) } }
mapLatest { mutationOf { mapper(it) } }

/**
* Maps each emission of [T] into multiple mutations of [State]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import kotlinx.coroutines.launch
fun <State : Any> CoroutineScope.stateFlowProducer(
initialState: State,
started: SharingStarted = SharingStarted.WhileSubscribed(),
mutationFlows: List<Flow<Mutation<State>>>
inputs: List<Flow<Mutation<State>>>
) = StateFlowProducer(
scope = this,
initialState = initialState,
started = started,
mutationFlows = mutationFlows
inputs = inputs
)

/**
Expand All @@ -48,14 +48,14 @@ class StateFlowProducer<State : Any> internal constructor(
private val scope: CoroutineScope,
initialState: State,
started: SharingStarted = SharingStarted.WhileSubscribed(),
mutationFlows: List<Flow<Mutation<State>>>
inputs: List<Flow<Mutation<State>>>
) : StateProducer<StateFlow<State>> {
private val parallelExecutedTasks = MutableSharedFlow<Flow<Mutation<State>>>()

override val state = scope.produceState(
initialState = initialState,
started = started,
mutationFlows = mutationFlows + parallelExecutedTasks.flatMapMerge(
inputs = inputs + parallelExecutedTasks.flatMapMerge(
concurrency = Int.MAX_VALUE,
transform = { it }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

/**
* Produces a [StateFlow] by merging [mutationFlows] and reducing them into an
* Produces a [StateFlow] by merging [inputs] and reducing them into an
* [initialState] state within [this] [CoroutineScope]
*/
fun <State : Any> CoroutineScope.produceState(
initialState: State,
started: SharingStarted,
stateTransform: (Flow<State>) -> Flow<State> = { it },
mutationFlows: List<Flow<Mutation<State>>>
inputs: List<Flow<Mutation<State>>>
): StateFlow<State> {
// Set the seed for the state
var seed = initialState
Expand All @@ -47,7 +47,7 @@ fun <State : Any> CoroutineScope.produceState(
return stateTransform(
flow {
emitAll(
merge(*mutationFlows.toTypedArray())
merge(*inputs.toTypedArray())
// Reduce into the seed so if resubscribed, the last value of state is persisted
// when the flow pipeline is started again
.reduceInto(seed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.tunjid.mutator.coroutines

import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
Expand Down Expand Up @@ -210,14 +210,14 @@ class FlowMutationStreamKtTest {
when (val type = type()) {
is Split.Action.Regular -> type.flow
.map {
mutation { copy(regularCount = regularCount + 1) }
mutationOf { copy(regularCount = regularCount + 1) }
}

is Split.Action.Delayed -> type.flow
.map {
// Delay when processing this action
delay(1000)
mutation { copy(delayedCount = delayedCount + 1) }
mutationOf { copy(delayedCount = delayedCount + 1) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.tunjid.mutator.coroutines

import com.tunjid.mutator.Mutation
import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf

data class State(
val count: Double = 0.0
Expand All @@ -41,8 +41,8 @@ sealed class DoubleAction : Action() {

val Action.mutation: Mutation<State>
get() = when (this) {
is IntAction.Add -> mutation { copy(count = count + value) }
is IntAction.Subtract -> mutation { copy(count = count - value) }
is DoubleAction.Divide -> mutation { copy(count = count / value) }
is DoubleAction.Multiply -> mutation { copy(count = count * value) }
is IntAction.Add -> mutationOf { copy(count = count + value) }
is IntAction.Subtract -> mutationOf { copy(count = count - value) }
is DoubleAction.Divide -> mutationOf { copy(count = count / value) }
is DoubleAction.Multiply -> mutationOf { copy(count = count * value) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.tunjid.mutator.coroutines

import app.cash.turbine.test
import com.tunjid.mutator.Mutation
import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf
import com.tunjid.mutator.plus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -60,7 +60,7 @@ class StateProductionKtTest {
val state = scope.produceState(
initialState = State(),
started = SharingStarted.WhileSubscribed(),
mutationFlows = listOf(
inputs = listOf(
eventMutations
)
)
Expand All @@ -79,7 +79,7 @@ class StateProductionKtTest {
val state = scope.produceState(
initialState = State(),
started = SharingStarted.WhileSubscribed(),
mutationFlows = listOf(
inputs = listOf(
eventMutations
)
)
Expand All @@ -105,14 +105,14 @@ class StateProductionKtTest {
val state = scope.produceState(
initialState = State(),
started = SharingStarted.WhileSubscribed(),
mutationFlows = listOf(
inputs = listOf(
eventMutations,
flow {
delay(1000)
emit(mutation { copy(count = 3.0) })
emit(mutationOf { copy(count = 3.0) })

delay(1000)
emit(mutation { copy(count = 7.0) })
emit(mutationOf { copy(count = 7.0) })
}
)
)
Expand All @@ -135,13 +135,13 @@ class StateProductionKtTest {

@Test
fun test_state_change_addition() {
val additionMutation = mutation<State> {
val additionMutation = mutationOf<State> {
copy(count = count + 1)
} +
mutation {
mutationOf {
copy(count = count + 1)
} +
mutation {
mutationOf {
copy(count = count + 1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import com.tunjid.mutator.demo.udfvisualizer.Event
import com.tunjid.mutator.demo.udfvisualizer.Marble
import com.tunjid.mutator.demo.udfvisualizer.UDFVisualizer
import com.tunjid.mutator.demo.udfvisualizer.udfVisualizerStateHolder
import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.currentCoroutineContext
Expand Down Expand Up @@ -70,16 +70,16 @@ class Snail10StateHolder(
private val speed: Flow<Speed> = scope.speedFlow()

private val speedChanges: Flow<Mutation<Snail10State>> = speed
.map { mutation { copy(speed = it) } }
.map { mutationOf { copy(speed = it) } }

private val progressChanges: Flow<Mutation<Snail10State>> = speed
.toInterval()
.map { mutation { copy(progress = (progress + 1) % 100) } }
.map { mutationOf { copy(progress = (progress + 1) % 100) } }

private val stateProducer = scope.stateFlowProducer(
initialState = Snail10State(),
started = SharingStarted.WhileSubscribed(),
mutationFlows = listOf(
inputs = listOf(
speedChanges,
progressChanges,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.tunjid.mutator.demo.udfvisualizer.Marble
import com.tunjid.mutator.demo.udfvisualizer.Event
import com.tunjid.mutator.demo.udfvisualizer.UDFVisualizer
import com.tunjid.mutator.demo.udfvisualizer.udfVisualizerStateHolder
import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -86,17 +86,17 @@ class Snail11StateHolder(
private val speed: Flow<Speed> = scope.speedFlow()

private val speedChanges: Flow<Mutation<Snail11State>> = speed
.map { mutation { copy(speed = it) } }
.map { mutationOf { copy(speed = it) } }

private val progressChanges: Flow<Mutation<Snail11State>> = speed
.toInterval()
.map { mutation { copy(progress = (progress + 1) % 100) } }
.map { mutationOf { copy(progress = (progress + 1) % 100) } }


private val mutator = scope.actionStateFlowProducer<Action, Snail11State>(
initialState = Snail11State(),
started = SharingStarted.WhileSubscribed(),
mutationFlows = listOf(
inputs = listOf(
speedChanges,
progressChanges
),
Expand All @@ -117,25 +117,25 @@ class Snail11StateHolder(

private fun Flow<Action.SetColor>.colorMutations(): Flow<Mutation<Snail11State>> =
mapLatest {
mutation { copy(colorIndex = it.index) }
mutationOf { copy(colorIndex = it.index) }
}

private fun Flow<Action.SetProgress>.progressMutations(): Flow<Mutation<Snail11State>> =
mapLatest {
mutation { copy(progress = it.progress) }
mutationOf { copy(progress = it.progress) }
}

private fun Flow<Action.SetMode>.modeMutations(): Flow<Mutation<Snail11State>> =
flatMapLatest { (isDark, startColors) ->
flow {
emit(mutation { copy(isDark = isDark) })
emit(mutationOf { copy(isDark = isDark) })
emitAll(
interpolateColors(
startColors = startColors.map(Color::argb).toIntArray(),
endColors = MutedColors.colors(isDark).map(Color::argb).toIntArray()
)
.map { (progress, colors) ->
mutation {
mutationOf {
copy(
colorInterpolationProgress = progress,
colors = colors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.tunjid.mutator.demo.udfvisualizer.Marble
import com.tunjid.mutator.demo.udfvisualizer.Event
import com.tunjid.mutator.demo.udfvisualizer.UDFVisualizer
import com.tunjid.mutator.demo.udfvisualizer.udfVisualizerStateHolder
import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -62,11 +62,11 @@ class Snail5StateHolder(
private val speed: Flow<Speed> = scope.speedFlow()

private val speedChanges: Flow<Mutation<Snail5State>> = speed
.map { mutation { copy(speed = it) } }
.map { mutationOf { copy(speed = it) } }

private val progressChanges: Flow<Mutation<Snail5State>> = speed
.toInterval()
.map { mutation { copy(progress = (progress + 1) % 100) } }
.map { mutationOf { copy(progress = (progress + 1) % 100) } }

private val changeEvents = MutableSharedFlow<Mutation<Snail5State>>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.tunjid.mutator.demo.udfvisualizer.Marble
import com.tunjid.mutator.demo.udfvisualizer.Event
import com.tunjid.mutator.demo.udfvisualizer.UDFVisualizer
import com.tunjid.mutator.demo.udfvisualizer.udfVisualizerStateHolder
import com.tunjid.mutator.mutation
import com.tunjid.mutator.mutationOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -61,11 +61,11 @@ class Snail6StateHolder(
private val speed: Flow<Speed> = scope.speedFlow()

private val speedChanges: Flow<Mutation<Snail6State>> = speed
.map { mutation { copy(speed = it) } }
.map { mutationOf { copy(speed = it) } }

private val progressChanges: Flow<Mutation<Snail6State>> = speed
.toInterval()
.map { mutation { copy(progress = (progress + 1) % 100) } }
.map { mutationOf { copy(progress = (progress + 1) % 100) } }

private val changeEvents = MutableSharedFlow<Mutation<Snail6State>>()

Expand Down
Loading
Loading