These are chat archives for ReactiveX/RxJava

16th
Aug 2018
Yannick Lecaillez
@ylecaillez
Aug 16 2018 14:55
Hi !
Is there an operator which behaves like take(1) but does not cancel upstream once the first item has been emitted ?
I need that because i send multiple request concurrently (flatMap) but i only care about the first response coming back. Still i want the subsequent request to be performed. Any idea ?
Sadly, Flowable.amb() is also cancelling remaining requests
Yannick Lecaillez
@ylecaillez
Aug 16 2018 15:03
Hmm, switchMap() might be what i'm looking for. Sorry for the noise.
David Karnok
@akarnokd
Aug 16 2018 16:05
Beware that switchMap cancels the active inner source if a new upstream item arrives and gets mapped in.
Also not completing and not cancelling may leak resources.
Yannick Lecaillez
@ylecaillez
Aug 16 2018 17:16
Yes this does not work for my use case i ended with something like that:
Using compose:
.compose(f -> {
                           final AtomicBoolean isFirst = new AtomicBoolean();
                           return f.filter(r -> isFirst.compareAndSet(false, true));
                       })
David Stocking
@dmstocking
Aug 16 2018 20:47
I think you could create your own operator doOnFirst() easy if your in kotlin
fun Observable<T>.doOnFirst(action: (T) -> Unit) {
    var first = true;
    return this.doOnNext { item ->
        if (first) {
            first = false
            action(item)
        }
    }
}