Skip to content

Instantly share code, notes, and snippets.

public suspend inline fun <T : Closeable?, R> T.useCancellably(
crossinline block: (T) -> R
): R = suspendCancellableCoroutine { cont ->
cont.invokeOnCancellation { this?.close() }
cont.resume(use(block))
}
@Keep
internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler),
CoroutineExceptionHandler {
override fun handleException(context: CoroutineContext, exception: Throwable) {
Crashlytics.logException(exception)
// Since we don't want to crash and Coroutines will call the current thread's handler, we
// install a noop handler and then reinstall the existing one once coroutines calls the new
// handler.
Thread.currentThread().apply {
class StateHolder<T : Any>(state: T) {
private val _liveData = MutableLiveData(state)
val liveData: LiveData<T> get() = _liveData
private var _value: T = state
val value: T get() = _value
fun update(notify: Boolean = true, block: T.() -> T) {
synchronized(LOCK) {
_value = block(value)