ModuleWithLogic

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) }
}

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)

Properties

Link copied to clipboard
abstract override val createLogic: (StoreAccessor) -> L
Link copied to clipboard
open val createMiddleware: () -> Middleware?

Optional factory for creating a middleware provided by this module.

Link copied to clipboard
abstract val initialState: S
Link copied to clipboard
abstract val reducer: (S, A) -> S

Functions

Link copied to clipboard
open suspend fun selectLogic(store: StoreAccessor): ModuleLogic
Link copied to clipboard
open suspend fun selectLogicTyped(store: StoreAccessor): L

Select the typed logic instance from the store.

Link copied to clipboard
open fun selectStateFlowNonSuspend(store: StoreAccessor): StateFlow<S>