Skip to content

Instantly share code, notes, and snippets.

@fvilarino
Last active September 18, 2023 20:49
Show Gist options
  • Save fvilarino/0dc2d480c79ebc8c7e7026b37d13999c to your computer and use it in GitHub Desktop.
Save fvilarino/0dc2d480c79ebc8c7e7026b37d13999c to your computer and use it in GitHub Desktop.
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