Skip to content

Instantly share code, notes, and snippets.

@uteke
Last active September 2, 2023 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uteke/b9d06c02c9fa5f2221e7ec56b560b0cb to your computer and use it in GitHub Desktop.
Save uteke/b9d06c02c9fa5f2221e7ec56b560b0cb to your computer and use it in GitHub Desktop.
Default action processor implementation
class DefaultActionProcessor(
private val getUserListRepository: GetUserListRepository,
private val connectivityMonitor: ConnectivityMonitor,
private val logger: Logger,
private val tracker: Tracker,
) : ActionProcessor<Action, Mutation, Event> {
override fun invoke(action: Action): Flow<Pair<Mutation?, Event?>> =
flow {
when (action) {
is Action.Load -> load()
...
}
}
private suspend fun FlowCollector<Pair<Mutation?, Event?>>.load() {
emit(Mutation.ShowLoader to null)
when (val result = getUserListRepository()) {
is Success -> {
tracker.trackEvent(CatalogFetched)
emit(Mutation.ShowContent(users = result.users) to null)
}
is Empty -> {
tracker.trackEvent(EmptyCatalogFetched)
logger.error("empty catalog")
emit(Mutation.ShowContent(users = result.users) to null)
}
is Failure -> {
tracker.trackEvent(CatalogFetchFailed)
logger.error("failed catalog ${result.errorType}")
emit(Mutation.ShowError(result.errorType)) to null)
}
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment