Created
April 24, 2019 04:08
-
-
Save uddyami/bdf64d7ef3f4d7016a45c99d02e45149 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun <T> T.doAsync( | |
| exceptionHandler: ((Throwable) -> Unit)? = crashLogger, | |
| task: AnkoAsyncContext<T>.() -> Unit | |
| ): Future<Unit> { | |
| val context = AnkoAsyncContext(WeakReference(this)) | |
| return BackgroundExecutor.submit { | |
| return@submit try { | |
| context.task() | |
| } catch (thr: Throwable) { | |
| val result = exceptionHandler?.invoke(thr) | |
| if (result != null) { | |
| result | |
| } else { | |
| Unit | |
| } | |
| } | |
| } | |
| } | |
| fun <T> T.doAsync( | |
| exceptionHandler: ((Throwable) -> Unit)? = crashLogger, | |
| executorService: ExecutorService, | |
| task: AnkoAsyncContext<T>.() -> Unit | |
| ): Future<Unit> { | |
| val context = AnkoAsyncContext(WeakReference(this)) | |
| return executorService.submit<Unit> { | |
| try { | |
| context.task() | |
| } catch (thr: Throwable) { | |
| exceptionHandler?.invoke(thr) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment