Skip to content

Instantly share code, notes, and snippets.

@aleweichandt
Created Aug 16, 2021
Embed
What would you like to do?
Flow UseCase abstraction
abstract class FlowUseCase<in TParam, out TResult>(
private val exceptionHandler: IExceptionHandler,
private val dispatcher: CoroutineDispatcher
) {
@ExperimentalCoroutinesApi
@Suppress("TooGenericExceptionCaught")
suspend operator fun invoke(param: TParam) =
performAction(param)
.catch { exception ->
exceptionHandler.handle(exception)
emit(Result.Failure(exception))
}
.flowOn(dispatcher)
protected abstract suspend fun performAction(param: TParam): Flow<Result<TResult>>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment