Skip to content

Instantly share code, notes, and snippets.

@galcyurio
Created January 7, 2021 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save galcyurio/93a4140271fb73463fc0b5b9e0874379 to your computer and use it in GitHub Desktop.
Save galcyurio/93a4140271fb73463fc0b5b9e0874379 to your computer and use it in GitHub Desktop.
auto disposable extension for ViewModel
package androidx.lifecycle
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import java.io.Closeable
private const val JOB_KEY = "io.reactivex.disposables.Disposable.JOB_KEY"
val ViewModel.disposable: CompositeDisposable
get() {
val disposable: Disposable? = getTag(JOB_KEY)
if (disposable is CompositeDisposable) {
return disposable
}
return setTagIfAbsent(JOB_KEY, CloseableDisposable()).disposable
}
private class CloseableDisposable(
val disposable: CompositeDisposable = CompositeDisposable()
) : Closeable, Disposable by disposable {
override fun close() {
disposable.clear()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment