These are chat archives for ReactiveX/RxJava

16th
Sep 2015
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 16 2015 09:39
I have another question. If I map something to something I do map for each signal and each subscription. It's ok in sense of immutability. But what is the propper way of maping and subscribeing signals with statefull objects in RX way?
Dorus
@Dorus
Sep 16 2015 09:39
Scan + Publish?
It does depend on what state you had in mind
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 16 2015 09:44
I map some stateless object to statefull container. Then (in way I do it now) i make 2 subscriptions. 1st subscription updates object (in pair with another observable), 2nd reads state (in pair with completly different observable)
Dorus
@Dorus
Sep 16 2015 09:47
Ok i didn't understand taht
Subscriptions only receive events, they do not update the source.
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 16 2015 09:49
        PublishSubject<Integer> seq = PublishSubject.create();

        Observable<Integer> seq2= seq.map(x ->{
            timesCalled += 1;
            return x * 2;
        });


        seq2.subscribe(e -> System.out.println("Hello 1: " + e));
        seq2.subscribe(e -> System.out.println("Hello 2: " + e));

        seq.onNext(1);

        System.out.println("Times map called: " + timesCalled);
This code shows that map in this case called twice. (Is that a error or not?). And I get 2 different objects in each subscription, expected same.
If it helps to understand my problem
Dorus
@Dorus
Sep 16 2015 09:51
Yes that's expected behavior
I assume timesCalled is declared outside the scope of map, that's where the problem lies.
Unless that was intended
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 16 2015 09:52
No, that's for example
Dorus
@Dorus
Sep 16 2015 09:52
You can use Observable.Create to be able to declare a local variable instead
Or publish seq2 so that side effects are shared between the observables instead.
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 16 2015 10:08
It works. But as far as I understand doc, publish creates ConnectableObservable. Which waits connect before emitting somethig. Sounds like not really aimed at current problem, right? Or I get it wrong? Or I need publish just to create new observable which keeps needed objects.
Until I map it again by the way
Dorus
@Dorus
Sep 16 2015 10:56
You can use refCount, or do both subscriptions and then connect() yes.