Skip to content

Commit

Permalink
fix(reducer): ReducerFactory improve definition searching
Browse files Browse the repository at this point in the history
  • Loading branch information
mcatta committed Jul 10, 2023
1 parent feb8a0b commit c592f83
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/commonMain/kotlin/dev/mcatta/polpetta/ReducerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public abstract class ReducerFactory<A : Action, S : State, E : SideEffect>(
sideEffectFactory: SideEffectFactory<E>
) {

private val _reducerDefinition: MutableMap<KClass<out State>, MutableList<ReducerFactoryBuilder<A, S>>> =
mutableMapOf()
private val _reducerDefinition: MutableList<ReducerFactoryBuilder<A, S>> = mutableListOf()
private val _sideEffectFactory = sideEffectFactory

/**
Expand All @@ -30,10 +29,9 @@ public abstract class ReducerFactory<A : Action, S : State, E : SideEffect>(
internal fun <FromState : S> getReducer(
action: A,
fromState: FromState
): Reducer<S>? =
_reducerDefinition[fromState::class]
?.firstOrNull { item -> item.kClassAction == action::class }
?.build(action)
): Reducer<S>? = _reducerDefinition
.firstOrNull { item -> item.kClassAction == action::class && item.kClassFromState.isInstance(fromState) }
?.build(action)

/**
* Define a [Reducer]'s body for the defined action [A]
Expand All @@ -58,9 +56,10 @@ public abstract class ReducerFactory<A : Action, S : State, E : SideEffect>(
kClassFromState: KClass<FromState>,
block: suspend SideEffectFactory<E>.(RA, StateModifier<FromState>) -> S
) {
_reducerDefinition.getOrPut(kClassFromState) { mutableListOf() }.add(
_reducerDefinition.add(
ReducerFactoryBuilder(
kClassAction = kClassAction,
kClassFromState = kClassFromState,
handler = { action ->
@Suppress("UNCHECKED_CAST")
(reducer { state -> block(_sideEffectFactory, action as RA, state as StateModifier<FromState>) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import kotlin.reflect.KClass
* Reducer Factory Builder used to wrap the Reduce function based on an action
*
* @param kClassAction action type
* @param kClassFromState input state type
* @param handler reducer
*/
internal class ReducerFactoryBuilder<A : Action, S : State>(
val kClassAction: KClass<out A>,
val kClassFromState: KClass<out S>,
val handler: (A) -> Reducer<S>
) {
fun build(action: A): Reducer<S> = handler.invoke(action)
Expand Down
29 changes: 29 additions & 0 deletions src/commonTest/kotlin/dev/mcatta/polpetta/StateStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,35 @@ internal class StateStoreTest {
}
}

@Test
fun `Test action based on a Generic State`() = runTest(context = testScope.coroutineContext) {
// Given
val testStore = object : StateStore<TestAction, TestState, Nothing>(
coroutineScope = testScope,
initialState = TestState.Count(0),
reducerFactory = {
on<TestAction.Increase, TestState.Count> { _, stateModifier ->
stateModifier.transform { copy(counter = counter + 1) }
}

on<TestAction.ToString, TestState> { _, stateModifier ->
stateModifier.transform { TestState.Result(this.toString()) }
}
}
) {}

testStore.stateFlow.test {
// When
testStore.dispatchAction(TestAction.Increase)
testStore.dispatchAction(TestAction.ToString)

// Then
assertIs<TestState.Count>(awaitItem())
assertIs<TestState.Count>(awaitItem())
assertIs<TestState.Result>(awaitItem())
}
}

@Test
fun `Test Intents execution and side effect`() = runTest(context = testScope.coroutineContext) {
// Given
Expand Down

0 comments on commit c592f83

Please sign in to comment.