These are chat archives for ReactiveX/RxJava

18th
Nov 2015
Javier Domingo Cansino
@txomon
Nov 18 2015 00:54
so, how do you organize code? because I am starting to see that I have gotten rid of almost all the logic, but I now have a lot of flows going on together in the setup method...
Dorus
@Dorus
Nov 18 2015 00:55
i'm not too experienced there, but for one thing you can create observables in one place and subscribe in another. You can also create one part of a observable here and the rest of the chain there etc.
Javier Domingo Cansino
@txomon
Nov 18 2015 00:56
yeah, but at the end you have a lot of chains over there.... maybe someone had some idea about that...
Do you know about a way of reuse certain chains?
for example the error treatment chain etc. for me would be usefull to reuse
I had thought about a function that did everything and returned the observable, but I would be breaking the Observable.xxxx.yyyy, and would be xxxx(Observable....).yyyy
Dorus
@Dorus
Nov 18 2015 01:38
You can make functions that take a observable and return one
Or look at transformers
Javier Domingo Cansino
@txomon
Nov 18 2015 01:39
I had seem them all... horrible movies
I don't know why they keep producing them
Dorus
@Dorus
Nov 18 2015 01:39
Not those transformers :)
Javier Domingo Cansino
@txomon
Nov 18 2015 01:39
haha
ok, will do =)
Dorus
@Dorus
Nov 18 2015 01:40
you apply that with source.compose(concatOnLast(...)).subscribe()
Javier Domingo Cansino
@txomon
Nov 18 2015 01:41
I see too many <T,T> ... :(
ohhh ok
that is better I thought it would be something horribler
I will continue tomorrow, 2am here...
Dorus
@Dorus
Nov 18 2015 01:45
Sorry i meant compose

Anyway, a important part of Rx is that you can create a observable, and subscribe later. Every subscription behaves as a fresh one. There is no difference between

Observable o1 = Observable.just(1).map(a -> a + 1);
Subscription s1 = o1.subscribe(), s2 = o1.subscribe();

and

Subscription s1 = Observable.just(1).map(a -> a + 1).subscribe(),
    s2 = Observable.just(1).map(a -> a + 1).subscribe();`.
Hope that makes any sense :)
Anyway i'm sure others here made larger projects with Rx and give more tips, i ussualy keep my Rx stuff small. I'm off now too. Goodnight.
Javier Domingo Cansino
@txomon
Nov 18 2015 13:47
ufff that transformers thing...
Dorus
@Dorus
Nov 18 2015 15:09
Let me explain how to use it.
Dorus
@Dorus
Nov 18 2015 15:28

Imagine you offten apply a function (or want to make your own custom one). You could make a function that adds it to the chain:

source.map(e -> e+1).subscribe;

addOne(source).subscribe();
private static <T extends Number> Observable<T> addOne(Observable<T extends Number> source) {
    return source.map(e -> e + 1);
}

However, this is ugly, imagine

addOne(Observable.just(1,2,3).map(e -> e+5)).map(e -> e*2).subscribe();

So we use a transformer:

Observable.just(1,2,3).map(e -> e+5).compose(addOne()).map(e -> e*2).subscribe();
private static Transformer<Integer, Integer> addOne() {
    return new Observable.Transformer<Integer, Integer>() {
        public Observable<Integer> call(Observable<Integer> t) {
            return t.map(e -> e + 1);
        };
    };
}

More, read this: http://blog.danlew.net/2015/03/02/dont-break-the-chain/

Also i failed to use <T extends Number> in my transformer, i blame Java's limited type system :(
Btw the transformer looks a lot nicer with lambdas:
private static Transformer<Integer, Integer> addOne2() {
    return t -> t.map(e -> e + 1);
}
Javier Domingo Cansino
@txomon
Nov 18 2015 15:30
how you tell hahha lamdas? what's that xD
anyway I got accustomed to the non lambda way
ok, I now understand the compose one
Dorus
@Dorus
Nov 18 2015 15:31
i know, but i got lazy on some of the trivial ones :)
That blog i found got a lot more background, good read.
Javier Domingo Cansino
@txomon
Nov 18 2015 15:31
so it's the cleanest way to insert your stuff in the pipeline
Dorus
@Dorus
Nov 18 2015 15:32
You'll like his first example:
<T> Observable<T> applySchedulers(Observable<T> observable) {  
return observable.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread());
}
And as transformer:
<T> Transformer<T, T> applySchedulers() {  
    return new Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
            }
    };
}
Javier Domingo Cansino
@txomon
Nov 18 2015 15:33
haha yeah, but it depends on my pipeline, I mean, usually the network function is in the middle
what is that first way you did?
I mean, that is just calling your function with the pipeline, no?
David Stemmer
@weefbellington
Nov 18 2015 15:34
I find it helpful to think about a transformer as a way of “decorating” an observable
Javier Domingo Cansino
@txomon
Nov 18 2015 15:34
anyway, it's clear for me now
I will give a shot on them shortly
for now I am experimenting with merge()
@weefbellington context managers if coming from python xD
well, more like generators, but yeah
David Stemmer
@weefbellington
Nov 18 2015 15:36
it’s a transformation from one observable to another. It takes an observable and returns an observable. Kind of like a decorator takes a function and returns a new, wrapped function in python
Javier Domingo Cansino
@txomon
Nov 18 2015 15:36
true, is just the oposite to what I said haha
I was thinking on with xxxx: syntax
but yeah, decorating functions is also good
David Stemmer
@weefbellington
Nov 18 2015 15:37
transformers are helpful, I (over-) use them a lot
Dorus
@Dorus
Nov 18 2015 15:38
It's like the difference between (f o g) and g(f)
Or is that f(g)? i'm never sure
Javier Domingo Cansino
@txomon
Nov 18 2015 15:38
haha yeah, the thing is how to order the code in an android app with Rx... Because I have all the logic in my activity
g(f)
Javier Domingo Cansino
@txomon
Nov 18 2015 15:46
oh, important question, once subscribed, is it possible to loose events?
I mean, miss onNext() items
David Stemmer
@weefbellington
Nov 18 2015 15:47
it’s possible for events to be generated so fast that they can’t be handled
that’s called “backpressure"
if events are getting lost though, that would probably constitute a bug in the RxJava operator
Javier Domingo Cansino
@txomon
Nov 18 2015 15:48
ok, so every Observable item will reach it's observers
David Stemmer
@weefbellington
Nov 18 2015 15:49
well, there are “hot” observables that can emit events before they have subscribers
these include all the Subject types
PublishSubject, BehaviorSubject etc
Dorus
@Dorus
Nov 18 2015 15:49
They can get lost in some operators like sample()
David Stemmer
@weefbellington
Nov 18 2015 15:50
a hot observable can emit items before anything is subscribed. You have to be careful when using them because of potential race conditions
Javier Domingo Cansino
@txomon
Nov 18 2015 15:58
I was just thinking on a normal situation if I would be able to receive all the items, or if I should be prepared to loose some of them
Dorus
@Dorus
Nov 18 2015 15:58
The operators that do lose messages make if very clear and obviouse. In all other cases no messages will be lost.
Javier Domingo Cansino
@txomon
Nov 18 2015 15:59
ok
Dorus
@Dorus
Nov 18 2015 15:59
like, trottle, sample, debounce, buffer+window with a higher skip than count etc.
Javier Domingo Cansino
@txomon
Nov 18 2015 16:00
yeah yeah, but I was just thinking on merge, map etc.
Dorus
@Dorus
Nov 18 2015 16:00
Or if you use zip where one side emit faster than the other. You wont lose messages, but there will be a large delay on one side, eventually filling up your memory.
Javier Domingo Cansino
@txomon
Nov 18 2015 16:00
those have an obvious purpose