-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
ToAsync implemented: Issue #95 #505
Conversation
RxJava-pull-requests #429 SUCCESS |
Can you help me understand the purpose of toAsync(Action)? Is it just that On Wed, Nov 20, 2013 at 1:10 PM, CloudBees pull request builder plugin <
David M. Gross |
@DavidMGross As I understand Rx.NET, it is something like this: You have an action A, but you don't want to execute it now, so you wrap it into an Observable that runs on a background thread. Once some observer subscribes, the action gets executed on the background thread and a onNext(null) + onComplete() is issued to it. However, it uses an AsyncSubject internally, so the action A is executed only once, and its "result" retrievered instantly to late observers. The value comes when you want to execute an action with certain parameter values. Given an Action1, that takes some parameter, you would make its execution asynchronous and you create a function for executing it with a particular parameter: Action1<Integer> a1 = v -> lengthlyOperation(v);
Func1<Integer, Observable<Void>> async = Observable.toAsync(a1);
Observable<Void> o = async.call(1);
o.subscribe(() -> doSomethingWhenCompleted);
async.call(2).subscribe(() -> doSomethingForParam2());
// later
o.subscribe(() -> doSomethingWhenCompletedAgain()); In this example, you'll trigger an execution of a1 with value 1, and in parallel, a1 with value 2. Later, if you subscribe to "o" again, you would get an "instant" completion. There are overloads for functions so you can get some result back as well. This seems to be similar to making a Future into an Observable, but there are a bunch of overloads for multi-parameter actions and functions, not just a simple Callable and Runnable case. |
Thanks! Just what I needed. On Wed, Nov 20, 2013 at 2:33 PM, akarnokd notifications@github.com wrote:
David M. Gross |
@headinthebox I'm wondering if these are better suited on a different class than |
The other problem is that many of these method overloads will not work for dynamic languages like Groovy and Clojure as the method arity is the same, only the type of For example: We need to think through how to specify all of these in a way that is descriptive and usable and consciously understand impact on our polyglot principles. |
Perhaps: rx.async.AsyncFunctions.fromFunc0(Func0) rx.async.AsyncActions.fromAction0(Action0) I suggest the package rx.async instead of rx.util.functions as I'd rather avoid package cycles, unless we expect the "async" keyword in some future Java. |
Why not all call them Async(....) and use static import so you can write Async((Integer x)->x). Looks really nice. |
@headinthebox I think because of Groovy (and others) are unable to differentiate based on arity; I had to drop a few overloads of toMap and toMultimap because of this. |
Yes, we can't have two overloads like this and still support non-strongly-typed languages:
Strongly typed languages can differentiate based on the number of args |
We should try Java 8 Nashorn to determine how Javascript will behave with functional interfaces as that's going to be a major player on the JVM in the coming years. |
What about having both @headinthebox' and @akarnokd's solutions at the same time? |
@headinthebox and I have reviewed this and feel the following is needed:
|
Closing. See #533 for updated version. |
Added 44 overloads of toAsync.