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
Method for converting an Observable to a different type, on Observable #2793
Comments
Or Ratpack could have a method method which takes an RxJava Observable and returns the appropriate type: import static RxRatpack.*;
asPromise(source)...; We do this for pattern for lots of things: iterables, futures, blocking observables. And besides, your proposed method loses the type T and you'd get Promise by default. |
It does have that method. The problem is that it breaks the flow if it can't be used fluently.
That's what I'm after. The type of the observable isn't lost if the return of the converter function is defined in terms of it. |
You are right with the return type, just checked. Still, I'm not convinced whether or not we'd need such operator. |
I'm not sure what more I can add to this, unless you feel that you don't see how I would use it. |
Hi @alkemist, Would it be correct to say that this is about "escaping the monad" in a non-blocking way? Similar to how Effectively the only difference between This seems reasonable and like a simple overload of |
Not quite… if I understand you. It's actually the observable that gets transformed not its items. I have this…
It's common in Ratpack to do processing with Rx, but need to give a promise to Ratpack to render the value or do… something… with it. Ratpack's core doesn't depend on Rx, so at times it's convenient to adapt. So the static method thing works and is fine, it's just not lambda friendly and breaks the chaining flow.
vs.
It's exactly the same thing when adapting |
I've posted a PR for this #2971. |
Would it be acceptable for the |
If "I" gave it out, there would be no safeguards, no plugin hook calls and no start calls. |
@akarnokd You bring up a good point about the plugin hooks not getting called but this seems like something that could be solved by a variety of engineering solutions (such as an abstract class or possibly a default method on an interface). With respects the onStart call, this isn't required for simple cases. The default behavior of a subscriber would make requests when With respects to safeguards, I would appreciate your feedback. Let me pose a use case. Lets say that a developer wants to convert their observable to a HashMap using the |
When you're designing the function that leaves the public static <T> Func1<Observable<T>, HashMap<String, T>> toHashMap() {
return o -> {
HashMap<String, T> result = new HashMap<>();
try {
o.toBlocking().forEach(e -> result.put(e.toString(), e));
} catch (RuntimeException ex) {
// ignore
}
return result;
};
} Maybe you swallow the exception, maybe you return some default value or have the return type as
It is one of the safeguards with the purpose to call unsubscribe on terminal events and clean up the chain. The others are protecting against |
We ended up with a new operator |
Interesting choice of method name. I've integrated this for my original purpose: ratpack/ratpack@d65ec33 Thanks. |
Ratpack is a web framework that integrates with RxJava. It has its own
Promise
type used throughout its API. Part of the Rx integration is the ability to move back and forth between Ratpack and Rx types. A method like…On
Observable
would help here. Ratpack has something similar on it'sPromise
type. Having this method onObservable
would allow code likeObservable.just(1).to(RxRatpack::asPromise)
. The same could be used for the RxReactiveStreams integration…Observable.just(1).to(RxReactiveStreams::toPublisher)
.A bit niche I know, but I thought it's worth asking for as it would make using Rx from Ratpack nicer.
The text was updated successfully, but these errors were encountered: