Skip to content

Instantly share code, notes, and snippets.

@fvilarino
Last active September 18, 2023 20:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
MVI Framework - Counter Middleware
// 1
class CounterMiddleware : Middleware<CounterState, CounterAction>() {
// 2
override suspend fun process(state: CounterState, action: CounterAction) {
// 3
when (action) {
CounterAction.GenerateRandom -> generateRandom()
else -> { /* no-op */ }
}
}
private suspend fun generateRandom() {
// 4
dispatch(CounterAction.Loading)
// 5
delay(500L + Random.nextLong(2000L))
val counterValue = Random.nextInt(100)
// 6
dispatch(CounterAction.CounterUpdated(counterValue))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment