These are chat archives for ReactiveX/RxJava

12th
Jun 2017
Ivan Schütz
@i-schuetz
Jun 12 2017 14:35
Any idea how I can test an observable / behaviour subject that has already been initialized. I just want the current value:
var b = BehaviorSubject.create<String>()
b.onNext("foo")

val connectableObservable: ConnectableObservable<String> = b.replay()

System.out.println("connectableObservable: " + connectableObservable)

//        val str: String = connectableObservable.blockingFirst()
//        val str: String = connectableObservable.blockingSingle()
val str: String = connectableObservable.blockingLast()

System.out.println("string: " + str)
my unit test "freezes" on connectableObservable.blockingLast()
(or throws an exception if I add a timeout observer...)
Ivan Schütz
@i-schuetz
Jun 12 2017 14:42
Java version:
BehaviorSubject<String> b = BehaviorSubject.create();
b.onNext("foo");

ConnectableObservable<String> connectableObservable = b.replay();

System.out.println("connectableObservable: " + connectableObservable);

String str = connectableObservable.timeout(2, TimeUnit.SECONDS).blockingLast();

System.out.println("string: " + str);
Ivan Schütz
@i-schuetz
Jun 12 2017 14:49
(it's synchronous/blocking because it's for an unit test, and it's also a requirement)
am I doing something stupid...?
Ivan Schütz
@i-schuetz
Jun 12 2017 15:06
my bad! getValue()
I tried that first but was using Observable... etc. etc.
Ivan Schütz
@i-schuetz
Jun 12 2017 15:13
why is it not possible to map BehaviourSubject in another BehaviourSubject? It returns an Observable...
Ivan Schütz
@i-schuetz
Jun 12 2017 20:03
well, most of the operators of Subject are defined in Observable... so they return Observables...
Ivan Schütz
@i-schuetz
Jun 12 2017 20:25
anyone knows the reason? Is it rx logic, or something with Java perhaps...? (Though it should be possible to override the methods to return Subject)?
Rafael Guillen
@rguillens
Jun 12 2017 20:32
That Observable never ends, because you never send onComplete event, so you'll never get the last value. You should use TestSubscriber to unit test Rx, here is a little intro: http://code.hootsuite.com/unit-testing-with-rxandroid-and-rxjava/
Ivan Schütz
@i-schuetz
Jun 12 2017 20:37
ok, I believe to have tried it out, but when I was using Observable... and Observable if I understood correctly doesn't work here because the value to be tested is set before of the subscription...
I will read it and play a bit more with this, for now, thanks!
anyway still would be interested in knowing why the operators of Subject don't override the ones of Observable, just curious
to be able to e.g. map a subject to another subject...
Rafael Guillen
@rguillens
Jun 12 2017 20:42
Subjects are just hybrids, they implements both Observer and Observable...
Ivan Schütz
@i-schuetz
Jun 12 2017 20:43
but there still could be cases where I want to run operators on them and just get another subject... no?
Rafael Guillen
@rguillens
Jun 12 2017 20:45
For example??
Ivan Schütz
@i-schuetz
Jun 12 2017 20:45
particularly because of this being hybrids is that I find it strange that map basically forces Subject to become an Observable
Rafael Guillen
@rguillens
Jun 12 2017 20:46
Indeed, Subject inherits map from Observable
Ivan Schütz
@i-schuetz
Jun 12 2017 20:46
well, I had a case where I was doing this, using observers
isASelectedObservable = generalObservable.map { state ->
    state.currentPage == A
}
then I changed both observables to Subject and it doesn't work anymore because map returns Observable
I had to change it to this
generalSubject.map {  state ->
    isASelectedSubject.onNext(state.currentPage == A)
}
Rafael Guillen
@rguillens
Jun 12 2017 20:49
Why you need two subjects to do that??
Ivan Schütz
@i-schuetz
Jun 12 2017 20:49
why not ?
I have a subject with a general state (includes many things) and in some place of the app I want another subject to react to a change in one specific part of this state
Rafael Guillen
@rguillens
Jun 12 2017 20:51
You can subscribe, why map them??
Ivan Schütz
@i-schuetz
Jun 12 2017 20:53
I can, yes, but why not map? It also makes sense, concept-wise IMO
okay, the reason, then, that Subject doesn't provide operators that work "on itself" is because they shouldn't be used so often...?
(not trying to make a point, genuinely trying to understand)
Rafael Guillen
@rguillens
Jun 12 2017 20:58
Think for a moment, you apply mapping functions to values, not to Observables ...

In this case I think subscribe is a better solution

Disposable d = generalSubject.subscribe(isASelectedSubject::onNext);

This way, isASelectedSubject will re-emit items from generalSubject and you can even dispose the subscription.

Ivan Schütz
@i-schuetz
Jun 12 2017 21:03
that's right, I think it also reflects clearer the intent...
Rafael Guillen
@rguillens
Jun 12 2017 21:06
Subjects are useful, but think for a moment, propagating it's onNextmethod all the way down is very dangerous. Anyone could modify the chain of events at any point... I'm not sure if this is what you meant.
Ivan Schütz
@i-schuetz
Jun 12 2017 21:11
that's what I do with subscribe too, except that there's more control with disposable... or in the block itself
Ivan Schütz
@i-schuetz
Jun 12 2017 21:18
;)