Skip to content

Instantly share code, notes, and snippets.

@Syex
Created June 12, 2019 09:17
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 Syex/4bc10c31de0003c4bd3244d65e5990e0 to your computer and use it in GitHub Desktop.
Save Syex/4bc10c31de0003c4bd3244d65e5990e0 to your computer and use it in GitHub Desktop.
abstract class UseCase<out Type, in Params> where Type : Any {
abstract suspend fun run(params: Params): Either<Exception, Type>
suspend operator fun invoke(params: Params, onSuccess: (Type) -> Unit, onFailure: (Exception) -> Unit) {
val result = run(params)
coroutineScope {
launch(uiDispatcher) {
result.fold(
failed = { onFailure(it) },
succeeded = { onSuccess(it) }
)
}
}
}
object None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment