Comments (4)
In the face of it, I don't think so. It has an intern observable but disposal of intermediary streams is not something we want to encourage. Disposal only travels up, so everything below it would be unbound (this caused a lot of issues when using something via compose()).
What would the API look like?
from autodispose.
I get your consideration.
What would the API look like?
I expect the API of ObservableSubscribeProxy
interface to be
public interface ObservableSubscribeProxy<T> {
...
Observable<? extends T> getSource();
}
then I can do
fun <T : Any> ObservableSubscribeProxy<T>.mySubscribe(f: () -> Unit): Disposable {
return getSource().doOnSubscribe { f() }
.subscribe(Consumer {}, Consumer {})
}
Any alternative solutions?
Cheers.
from autodispose.
hmm. I see the value from a flexibility, but your example actually ends up not using AutoDispose's functionality. Under the hood, this works because we decorate the received observer or lambda observers with a custom autodisposing one. In your example, you're just hitting RxJava's standard subscribe()
method and forfeiting AutoDispose's.
You could adjust it to use an autodispose one, but now you no longer have access to the scope, so we'd need to then provide that somewhere too, so now we're talking about two extra methods (source()
and scope()
) just to be functional.
Two ideas that come to mind:
- Depending on what you want to do, a plugin similar to RxJava's onSubscribe hooks might give you the flexibility you're looking for. It would be per-runtime though, and not as flexible as extension functions in terms of visibility/scoping/etc.
- We expose a method on
SubscribeProxy
that outlets to another interface that exposes the above two elements and lets you handle it how you want (and hopefully remember to still call a real autodispose subscribe under the hood). In Kotlin, you could then tack on whatever extension methods you want, but it would be largely useless in Java and annoying to show in autocomplete. If we could somehow hide it from java but available in kotlin, I'd be pretty interested.
interface ObservableSubscribeProxy<T> {
// Currently existing methods...
// New method
ObservableExtensionsHook<T> extensions()
}
interface ObservableExtensionsHook<T> {
Observable<T> source()
Maybe<?> scope()
}
// Your example
fun <T : Any> ObservableExtensionsHook<T>.mySubscribe(f: () -> Unit): Disposable {
return source().doOnSubscribe { f() }
.autoDisposeWith(scope())
.subscribe(Consumer {}, Consumer {})
}
// In use
Observable.just(1)
.autoDisposeWith(this)
.extensions()
.mySubscribe(...)
Basically, I like this, but ObservableExtensionsHook<T> extensions()
is practically useless to java consumers and I wouldn't want to pollute the API.
from autodispose.
I manage to solve my question.
interface MyProxy<T> {
fun sourceObservable(): Observable<T>
fun lifecycleOwner(): LifecycleOwner
}
class MyProxyImpl<T>(val source: Observable<T>, val lifecycleOwner: LifecycleOwner): MyProxy<T> {
override fun sourceObservable(): Observable<T> = source
override fun lifecycleOwner(): LifecycleOwner = lifecycleOwner
}
fun <T> Observable<T>.autoDispose(lifecycleOwner: LifecycleOwner): MyProxy<T> {
return MyProxyImpl(this, lifecycleOwner)
}
fun <T : Any> MyProxy<T>.mySubscribe(f: () -> Unit): Disposable {
return sourceObservable().doOnSubscribe { f() }
.autoDisposeWith(lifecycleOwner())
.subscribe(Consumer {}, Consumer {})
}
from autodispose.
Related Issues (20)
- crash by fragment in removing state HOT 1
- ViewScopeProvider throws when Activity restarts. HOT 5
- Question: Incompatible with RxJava 2? HOT 4
- Unsubscribe unexpectedly when used in item of 'RecyclerView' inside 'ViewPager2' after page changed. HOT 1
- how to remove warning Missing Disposable handling: Apply AutoDispose or cache the Disposable instance manually and enable lenient mode. HOT 1
- I need AutoDispose proguard
- Help ,Why the main thread is required ? HOT 3
- Integrate kotlin ABI validator
- Please ignore
- Please ignore
- Obseravable called twice when completable scope. #416 HOT 3
- Unusable with lint 7.0.0-alpha14 (Jetpack Compose project) HOT 4
- How to use it in MVVM? HOT 2
- gradle 7.1 and AGP 7.0.0-beta05 HOT 3
- Snapshots and Building from source HOT 1
- Leak - DetachEventCompletable isn't removed when View is removed from ViewTree HOT 1
- lint lenient mode do not support kotlin lambda/when/if expressions HOT 1
- Question: how to implement a custom LifecycleScopeProvider HOT 1
- ERROR: R8: Missing class com.google.errorprone.annotations.DoNotMock (referenced from: autodispose2.ScopeProvider and 1 other context) HOT 3
- Service loader file missing in error prone plugin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autodispose.