ModuleWithLogic
interface ModuleWithLogic<S : ModuleState, A : ModuleAction, L : ModuleLogic> : Module<S, A> (source)
The recommended interface for defining modules with type-safe logic access.
ModuleWithLogic extends Module with typed logic, allowing direct access to logic methods without type casting. This is the preferred pattern for new modules.
Example:
@Serializable
data class CounterState(val count: Int = 0) : ModuleState
sealed class CounterAction : ModuleAction(CounterModule::class) {
data object Increment : CounterAction()
data class SetCount(val value: Int) : CounterAction()
}
class CounterLogic(private val storeAccessor: StoreAccessor) : ModuleLogic() {
suspend fun incrementAsync() {
delay(1000)
storeAccessor.dispatch(CounterAction.Increment)
}
}
object CounterModule : ModuleWithLogic<CounterState, CounterAction, CounterLogic> {
override val initialState = CounterState()
override val reducer: (CounterState, CounterAction) -> CounterState = { state, action ->
when (action) {
is CounterAction.Increment -> state.copy(count = state.count + 1)
is CounterAction.SetCount -> state.copy(count = action.value)
}
}
override val createLogic: (StoreAccessor) -> CounterLogic = { CounterLogic(it) }
}Content copied to clipboard
Parameters
S
The state type for this module (must implement ModuleState)
A
The action type for this module (must extend ModuleAction)
L
The logic type for this module (must extend ModuleLogic)