-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RxLifecycle interop module #82
Comments
What will be the use case for such a module? |
So when I say |
One solution can be to have static util methods to provide the different type of scopers for For public static <T> Function<Observable<?>, ObservableSubscribeProxy<Object>> lifeCycleProvider(final LifecycleProvider<T> lifecycleProvider) {
Maybe<T> maybe = Maybe.create(new MaybeOnSubscribe<T>() {
@Override public void subscribe(final MaybeEmitter<T> e) throws Exception {
lifecycleProvider.lifecycle().subscribe(new Consumer<T>() {
@Override public void accept(T t) throws Exception {
e.onSuccess(t);
}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {
// It shouldn't be thrown ideally. Throwing so that AutoDisposingObserverImpl dispose in the error callback.
e.onError(throwable);
}
});
}
});
return AutoDispose.with(maybe).forObservable();
} For Lifecyclescopeprovider the consumer can provide the public static <T>LifecycleScopeProvider<T> lifecycleScopeProvider(LifecycleProvider<T> lifecycleProvider, final Function<T, T> correspondingEvents) {
final BehaviorSubject<T> behaviorSubject = BehaviorSubject.create();
lifecycleProvider.lifecycle().subscribe(new Consumer<T>() {
@Override public void accept(T t) throws Exception {
}
});
return new LifecycleScopeProvider<T>() {
@Override public Observable<T> lifecycle() {
return behaviorSubject.hide();
}
@Override public Function<T, T> correspondingEvents() {
return correspondingEvents;
}
@Nullable @Override public T peekLifecycle() {
return behaviorSubject.getValue();
}
};
}
public static <T>LifecycleScopeProvider<T> activityLifecycleScopeProvider(LifecycleProvider<T> lifecycleProvider) {
return lifecycleScopeProvider(lifecycleProvider, RxLifecycleAndroid.ACTIVITY_LIFECYCLE);
} Not sure if this is the best approach as it still needs some plumbing. Also, since Rx lifecycle has multiple modules so it may require multiple interop modules. For |
The API should just return scope providers for autodispose to use, not act as an alternative to autodispose itself. Also FYI - The API has changed, Scopers are deprecated. So my thoughts on this are basically:
An implementation could look like this: static ScopeProvider bindLifecycle(LifecycleProvider<E> provider) {
return new ScopeProvider() {
@Override
Maybe<?> requestScope() {
return provider.lifecycle()
.compose(provider.bindToLifecycle()) // Interop layer. This stream will now complete upon lifecycle end
.ignoreElements() // Listen for just completions
.toMaybe() // Back to Maybe<?>
.defaultIfEmpty(SOME_STATIC_THROWAWAY_OBJECT) // Because RxLifecycle just completes when it's done, we want to coerce that to a faked "onSuccess" here
}
}
} There will be caveats, but that's ok. This will not really be a One question could be if we want to coerce RxLifecycle's |
Got it. Static utility method for error is much better than either forcing them or breaking their existing chain. I think the best part of the approach is that we doesn't need separate methods for RX Activity as the caller takes care of provider.bindToLifeCycle() |
Cool. Let's make the default to map to AutoDispose's exception, since I think that will be the common case. That way it'll be picked up by the autodispose plugin hook for lifecycle exceptions too |
Looking at |
Oh right, in that case we don't need to add the error switch at all then |
Should we add one? The gist would be that it could understand RxLifecycle's
LifecycleProvider
mechanics, which in turn should also lend support for its various components (RxActivity
, etc)The text was updated successfully, but these errors were encountered: