Middleware

fun interface Middleware<State, Action : Any>

A middleware that can wrap the Store.dispatch method.

An example of a middleware that logs actions and states:

class LoggerMiddleware<State, Action : Any> : Middleware<State, Action> {
override fun MiddlewareScope<State, Action>.process(action: Action): State {
println("action: $action")
println("current state: $state")
val nextState = next(action)
println("next state: $nextState")
return nextState
}
}

Functions

Link copied to clipboard
abstract fun MiddlewareScope<State, Action>.process(action: Action): State

Processes an action and returns the next state. This function is given the next function that is the next middleware function in the chain, and is expected to call next(action) with different arguments, or at different times, or maybe not call it at all. The last middleware in the chain will receive the real Store.dispatch function as the next, thus ending the chain.