Skip to content

Instantly share code, notes, and snippets.

@shakil807g
Created January 31, 2018 10:23
Show Gist options
  • Save shakil807g/6938bb2d89bcc7aa9ad8833110ab93e4 to your computer and use it in GitHub Desktop.
Save shakil807g/6938bb2d89bcc7aa9ad8833110ab93e4 to your computer and use it in GitHub Desktop.
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.OnLifecycleEvent
import android.util.Log
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.Deferred
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.newFixedThreadPoolContext
// Quick & dirty logcat extensions
inline fun <reified T> T.logd(message: () -> String) = Log.d(T::class.simpleName, message())
inline fun <reified T> T.loge(error: Throwable, message: () -> String) = Log.d(T::class.simpleName, message(), error)
internal class CoroutineLifecycleListener(private val deferred: Deferred<*>) : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun cancelCoroutine() {
if (!deferred.isCancelled) {
deferred.cancel()
}
}
}
// CoroutineContext running on background threads.
internal val Background = newFixedThreadPoolContext(Runtime.getRuntime().availableProcessors() * 2, "Loader")
/**
* Creates a lazily started coroutine that runs <code>loader()</code>.
* The coroutine is automatically cancelled using the CoroutineLifecycleListener.
*/
fun <T> LifecycleOwner.load(loader: suspend () -> T): Deferred<T> {
val deferred = async(context = Background, start = CoroutineStart.LAZY) {
loader()
}
lifecycle.addObserver(CoroutineLifecycleListener(deferred))
return deferred
}
/**
* Extension function on <code>Deferred<T><code> that creates a launches a coroutine which
* will call <code>await()</code> and pass the returned value to <code>block()</code>.
*/
infix fun <T> Deferred<T>.then(block: suspend (T) -> Unit): Job {
return launch(context = UI) {
try {
block([email protected]())
} catch (e: Exception) {
// Just log the exception to confirm when we get cancelled (Expect JobCancellationException)
loge(e) { "Exception in then()!" }
throw e
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment