Skip to content

Commit

Permalink
doc: methods's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mcatta committed Jul 4, 2023
1 parent 9613c4a commit d38bdda
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/commonMain/kotlin/dev/mcatta/polpetta/ReducerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public abstract class ReducerFactory<A : Action, S : State, E : SideEffect>(
private val _sideEffectFactory = sideEffectFactory

/**
* Return the [Reducer] bond to the action [action]
* Return the [Reducer] bond to the action [action] and [FromState]
*
* @param action
* @throws IllegalStateException in case that action doesn't have any Reducers
* @param fromState
* @return Reducer if matches the pair
*/
internal fun <FromState : S> getReducer(
action: A,
Expand All @@ -35,6 +37,7 @@ public abstract class ReducerFactory<A : Action, S : State, E : SideEffect>(

/**
* Define a [Reducer]'s body for the defined action [A]
*
* @param block
*/
public inline fun <reified RA : A, reified FromState : S> on(
Expand All @@ -45,6 +48,7 @@ public abstract class ReducerFactory<A : Action, S : State, E : SideEffect>(

/**
* Define a [Reducer]'s body for the defined action with class [KClass]
*
* @param kClassAction
* @param kClassFromState
* @param block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public class SideEffectFactory<E : SideEffect> {
private val _sideEffectFlow: MutableStateFlow<E?> = MutableStateFlow(null)
public val sideEffectFlow: StateFlow<E?> = _sideEffectFlow.asStateFlow()

/**
* Dispatch a [sideEffect]
*
* @param sideEffect
*/
public suspend fun sideEffect(sideEffect: E) {
_sideEffectFlow.emit(sideEffect)
}
Expand Down
23 changes: 18 additions & 5 deletions src/commonMain/kotlin/dev/mcatta/polpetta/StateStore.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.mcatta.polpetta

import dev.mcatta.polpetta.core.Reducer
import dev.mcatta.polpetta.operators.Action
import dev.mcatta.polpetta.operators.SideEffect
import dev.mcatta.polpetta.operators.State
Expand Down Expand Up @@ -38,18 +37,32 @@ public abstract class StateStore<A : Action, S : State, E : SideEffect>(
.map { action ->
val currentState = _stateFlow.value

_reducerFactory.getReducer(
action = action,
fromState = currentState
)?.reduce(currentState = StateModifier.of(currentState))
reduceState(action, currentState)
}
.filterNotNull()
.onEach { newState -> _stateFlow.value = newState }
.launchIn(coroutineScope)
}

/**
* Reduce the [currentState] into a new one based on the [action] and the [currentState] itself.
* In case this pair is not defined into the [ReducerFactory] class this will return null
*
* @param action
* @param currentState
* @return new state or null
*/
private suspend fun reduceState(
action: A,
currentState: S
): S? = _reducerFactory.getReducer(
action = action,
fromState = currentState
)?.reduce(currentState = StateModifier.of(currentState))

/**
* Dispatch an action that trigger a Reducer
*
* @return true if the Action si defined
*/
public suspend fun dispatchAction(action: A): Unit = _reducerQueue.send(action)
Expand Down
7 changes: 6 additions & 1 deletion src/commonMain/kotlin/dev/mcatta/polpetta/core/Reducer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ internal fun interface Reducer<S : State> {

/**
* This method allows to create an [Reducer]
*
* @param block function's body
* @return Reducer function
*/
internal fun <S : State> reducer(block: suspend (StateModifier<S>) -> S) = Reducer(block)
internal fun <S : State> reducer(
block: suspend (StateModifier<S>) -> S
) = Reducer(block)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlin.reflect.KClass

/**
* Reducer Factory Builder used to wrap the Reduce function based on an action
*
* @param kClassAction action type
* @param handler reducer
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package dev.mcatta.polpetta.operators

/**
* Side Effect definition
*/
public interface SideEffect
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class StateModifier<FromState : State> private constructor(
public companion object {
/**
* Smart constructor for the StateModifier creation
*
* @param state
*/
public fun <FromState : State> of(
Expand All @@ -24,22 +25,28 @@ public class StateModifier<FromState : State> private constructor(

/**
* Change the current state with new properties
*
* @param mutator mutator callback
* @return the mutated [FromState]
*/
public suspend fun mutate(
mutator: suspend FromState.() -> FromState
): FromState = mutator(getAndCheck())

/**
* Change the current state into a new one
*
* @param transformer transformer callback
* @return a new state that inherited from [State]
*/
public suspend fun <ToState : State> transform(
transformer: suspend FromState.() -> ToState
): ToState = transformer(getAndCheck())

/**
* This function get the current state and validate is value
*
* @return the current state or throws an exception
*/
@Suppress("UNCHECKED_CAST")
private fun <CS : FromState> getAndCheck(): CS {
Expand Down

0 comments on commit d38bdda

Please sign in to comment.