Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Last active October 7, 2019 23:00
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 yongjhih/a0a6c3a2a669b92b89acf120e6c1c7c6 to your computer and use it in GitHub Desktop.
Save yongjhih/a0a6c3a2a669b92b89acf120e6c1c7c6 to your computer and use it in GitHub Desktop.
fun <T> MethodCall.argumentOrNull(key: String): T? = try { argument(key) } catch (e: Throwable) { null }
fun <T> MethodCall.argumentsOrNull(): T? = arguments() as? T?
//fun <T> MethodCall.argument(key: String): T? = try { argument(key) } catch (e: Throwable) { null }
//fun <T> MethodCall.arguments(): T? = arguments() as? T?
//@JvmOverloads
//fun Result.success(result: Any? = null): Unit = success(result)
fun Result.success(): Unit = success(null) // avoid shadow
fun Result.errors(code: String, message: String? = null, details: Any? = null): Unit = error(code, message, details)
fun Result.error(e: Throwable): Unit = errors(e.cause.toString(), e.message, e.stackTrace)
fun Result.complete(onRunnable: () -> Unit) {
try {
onRunnable()
success()
} catch (e: Throwable) {
error(e)
}
}
fun <R> Result.call(onConsumer: () -> R) {
try {
success(onConsumer())
} catch (e: Throwable) {
error(e)
}
}
fun <T, R> Result.call(arg: T?, onSuccess: (T) -> R) {
try {
success(onSuccess(arg!!))
} catch (e: Throwable) {
error(e)
}
}
fun <T> Result.complete(arg: T?, onComplete: (T) -> Unit) {
try {
onComplete(arg!!)
success()
} catch (e: Throwable) {
error(e)
}
}
fun <T, T2> Result.complete(arg: T?, arg2: T2?, onComplete: (T, T2) -> Unit) {
try {
onComplete(arg!!, arg2!!)
success()
} catch (e: Throwable) {
error(e)
}
}
fun <T, R> Result.callTask(task: Task<T>, onSuccess: (T) -> R) {
try {
if (task.isSuccessful) {
success(onSuccess(task.result!!))
} else {
error(task.exception!!)
}
} catch (e: Throwable) {
error(e)
}
}
fun <T> Result.completeTask(task: Task<T>, onComplete: (T) -> Unit) {
try {
if (task.isSuccessful) {
onComplete(task.result!!)
success()
} else {
error(task.exception!!)
}
} catch (e: Throwable) {
error(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment