uber / AutoDispose

Automatic binding+disposal of RxJava streams.
https://uber.github.io/AutoDispose/
Apache License 2.0
3.37k stars 226 forks source link

Context receiver APIs #370

Open ZacSweers opened 5 years ago

ZacSweers commented 5 years ago

If compound extensions do make their way into Kotlin, we could leverage them nicely in AutoDispose to do something like this:

fun <T> ScopeProvider.Observable<T>.autoDispose() {
  // ...
}

fun test(scope: ScopeProvider) = scope.apply {
  myObservable
      .autoDisposable()
      .subscribe()
}

// or

class Worker : ScopeProvider {
  fun run() {
    workObservable
        .autoDisposable()
        .subscribe()
  }
}
ZacSweers commented 5 years ago

Note: this would supersede the need for #375

ZacSweers commented 4 years ago

Closing this as it seems like it's not moving anywhere

ZacSweers commented 2 years ago

context receivers are happening

psteiger commented 1 year ago

Should be a very simple function:

// Original function
@CheckReturnValue
inline fun <T> Observable<T>.autoDispose(provider: ScopeProvider): ObservableSubscribeProxy<T> =
    this.`as`(AutoDispose.autoDisposable(provider))

// Context receivers function
context(ScopeProvider)
inline fun <T> Observable<T>.autoDispose(): ObservableSubscribeProxy<T> = autoDispose(this@ScopeProvider)