Created
April 24, 2019 04:08
-
-
Save uddyami/bdf64d7ef3f4d7016a45c99d02e45149 to your computer and use it in GitHub Desktop.
Revisions
-
uddyami created this gist
Apr 24, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ 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) } } }