-1

Observables only emit one object, so doOnNext() is always called with an Action1. How can I use Action2 in a similar fashion?

Can I combine 2 observables call an Action2?

EDIT: Why would I want to do this? I am working on a checkout app. I have a view that in order to display correctly, it needs two pieces of data (1: tip% and 2: total cost). So if this view could react to an observable sequence as an Action2, I would be happy.

EDIT2: Here's a method on the view mentioned previously. If this were an Action1, I could easily call it like I do with .doOnNext(). Are there operators that can operate similarly to .doOnNext() but take in an Action2 as a parameter? Maybe something like withLatestFrom() that takes in an Action2 instead of a Func2?

public Action2<Money, List<Integer>> displayGratuityOptions() {
        return (subtotal, gratuityPercents) -> {
            removeAllTabs();
            for (final Integer percent : gratuityPercents) {
                addTab(createTab(subtotal, percent));
            }
            addTab(createCustomGratuityTab());       
        };
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107
  • Why would you want to do that? What is your use case? – akarnokd Mar 15 '16 at 00:16
  • You need to explain a little more clearly what you're asking. – Enigmativity Mar 15 '16 at 03:17
  • You say "Why would I want to do this?" but I don't know what you mean by "this". Can you please explain what you mean by `Action1` and `Action2`? What are their types? What do you mean that an observable can call them? etc, etc, etc... – Enigmativity Mar 15 '16 at 04:18
  • @Enigmativity I guess I want to simulate `.withLatestFrom()` but as an `Action2` instead of a `Func2`. I guess this isn't possible? – ZakTaccardi Mar 15 '16 at 04:43
  • @ZakTaccardi - I still don't know what you mean. Can you **explain** what you're trying to do? – Enigmativity Mar 15 '16 at 04:51

1 Answers1

0

Operators like withLatestFrom are there to combine multiple sequences into a single sequence. I think you will be better placed to work with Rx if you can adopt this way of thinking i.e. single data type sequences.

To this end I would suggest creating a type that has both the data types you want. You can then merge your data and then pass the single value to your doOnNext handler*.

This is simple stuff in most languages that Rx is used in (C#, F#, JS, Scala) but in Java, you may have to actually declare/define a type to do this. See - A Java collection of value pairs? (tuples?)

*Please try to avoid using the Do side-effect operators. There is almost always a better way. In you example it looks like you are doing some significant work in that handler (Creating, Adding and Removing tabs which I assume are Visual Controls)

Community
  • 1
  • 1
Lee Campbell
  • 10,631
  • 1
  • 34
  • 29