dispatchFunction
This function only returns given dispatch. It has a few benefits over simple DispatchFunction lambda that are illustrated with this example:
fun counterMiddleware1(): Middleware<AppState> = {
var i = restorePreviousValue() // semicolon is required here to compile
{ action -> // label must be defined manually here
if (action is ResetCounter) {
i = 0
return next(action) // early exit requires a label to lambda
}
i++
next(action)
}
}
fun counterMiddleware2(): Middleware<AppState> = {
var i = restorePreviousValue() // semicolon is NOT required here to compile
dispatchFunction { action ->
if (action is ResetCounter) {
i = 0
return@dispatchFunction next(action) // default label can be referred here
}
i++
next(action)
}
}
Content copied to clipboard