Last active
September 18, 2023 20:49
-
-
Save fvilarino/0dc2d480c79ebc8c7e7026b37d13999c to your computer and use it in GitHub Desktop.
MVI Framework - Counter Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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