Middleware

typealias Middleware = suspend (action: ModuleAction, getAllStates: suspend () -> Map<String, ModuleState>, storeAccessor: StoreAccessor, updatedState: suspend (ModuleAction) -> ModuleState) -> Unit(source)

Type alias for middleware functions.

Middleware allows you to intercept actions and perform additional operations like logging, analytics, or side effects before and after the action is processed.

Example:

val loggingMiddleware: Middleware = { action, getAllStates, storeAccessor, updatedState ->
println("Before: $action")
val newState = updatedState(action)
println("After: $newState")
}

val store = createStore {
module(MyModule)
middlewares(loggingMiddleware)
}

Parameters

action

The action being dispatched

getAllStates

Function to get all current module states

storeAccessor

Access to dispatch, state selection, and logic selection

updatedState

Function to continue processing the action and get the resulting state