Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aleweichandt
Created August 16, 2021 18:55
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 aleweichandt/aaf0d82dfdd4f8ca6a5f878760d32640 to your computer and use it in GitHub Desktop.
Save aleweichandt/aaf0d82dfdd4f8ca6a5f878760d32640 to your computer and use it in GitHub Desktop.
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