Take events only after an Observable from a chain has emitted one

rxjs operators tutorial
rxjava
nested observables rxjava
rxjs flatmap
switchmap
concatmap
rxjava single example
rxjs observable

Here is the chain: observableOne.flatMap(event -> observableTwo).subscribe()

I'd like to take events from observableOne the very first time it emits one, then ignore all other events from this Observable until after observableTwo has either emitted a value, completed or finished with an error. Worth mentioning that ultimately I'm interested in the events from the second Observable.

The context is like, there is a Button clicking on which an event is fired, this is ObservableOne. The fired event triggers ObservableTwo, let's say it's a chain of network operations. So I want to ignore all button clicks while network operations are being executed.

To control the request amounts made by flatMap use a special overload:

observableOne
  .doOnRequest(System.out::println)
  .flatMap(event -> observableTwo, 1)
  .flatMap(event -> observableThree, 1)
.subscribe()

If your sources observableOne, observableTwo, observableThree are synchronous this should not be necessary but for asynchronous sources this should do the job.

Proceedings Of The 28th International Conference On High Energy , In the past two years, these ideas have been applied to observables that do not product expansion, such as event shapes in ete annihilation, jet observables and order gluon emission diagram *** and in the approximation of a single chain of The 1/Q-correction comes only from the two-jet region T – 1, when the gluon  When you are interested in only the first emission, you want to use take.Maybe you want to see what the user first clicked on when they entered the page, or you would want to subscribe to the click event and just take the first emission.

Thinking in nested streams with RxJS, const { interval, from } = Rx; const { take, map, tap } = RxOperators; Promises can be used in long chains or nested deeply within each other, but calling When applied to one observable, it captures all the events emitted by nested When the second stream starts emitting numbers, switchMap only puts  emit only the last item (or the last item that meets some condition) emitted by an Observable If you are only interested in the last item emitted by an Observable, or the last item that meets some criteria, you can filter the Observable with the Last operator.

You could use a Flowable instead of Observable, then you could use the backpressure to achieve the desired effect (drop events until observableTwo terminates) by setting the maxConcurrency of flatMap to 1:

observableOne
    .toFlowable(BackpressureStrategy.DROP)
    .flatMap(event -> observableTwo.toFlowable(BackpressureStrategy.ERROR), 1)
    .subscribe();

I posted a similar question here.

Turns out there's also an ObservableTransformer exactly for this purpose in RxJava2Extensions library by akarnokd. It can be used like this:

observableOne
    .compose(ObservableTransformers.flatMapDrop(event -> observableTwo))
    .subscribe();

What happens when we subscribe to a stream? A look inside RxJava, What actually happens when you subscribe to a stream? call — take operator was the last in chain, so its observable is the outermost one in It establishes a subscription and allows actual flow of events to which we can react. That is, even though we explicitly only subscribe to one observable (the last  ElementAt — emit only item n emitted by an Observable; Filter — emit only those items from an Observable that pass a predicate test; First — emit only the first item, or the first item that meets a condition, from an Observable; IgnoreElements — do not emit any items from an Observable but mirror its termination notification

Unified Field Mechanics: Natural Science Beyond The Veil Of , This is a 'failed inflation' event, where the balance between inflation and Within the chain star are the same processes that occurred after the unmerging event, but It emits only symmetric loops in the form of high energy photons that have of Physics – One Set Always and Everywhere Loop sizes are the only variables  It results in an Observable that emits only those items that are emitted during that initial duration of the source Observable’s lifespan. You set this duration by passing in a length of time and the time units this length is denominated in as parameters to take .

5 Not So Obvious Things About RxJava - Jag Saund, However, map only emits one item, whereas flatMap emits zero or more Unregister callbacks when an Observable is unsubscribed (failing to do Emit events using onNext or onCompleted only while a subscriber is When the consumer is ready to take the next value, it receives the first value emitted by  For example, Angular’s Router has a method events that creates an Observable of the current url. Each time the url changes this Observable emits the new url (this is a simplified example, the

Operators, This allows you to apply these operators one after the other, in a chain. Debounce — only emit an item from an Observable if a particular timespan has passed Do — register an action to take upon a variety of Observable lifecycle events  💡 If you only need a specific number of //when five even numbers have been emitted, complete source observable. Take mouse events on mouse down until mouse

Comments
  • It's usual to ignore previous network operations if the button is clicked multiple times - only taking the results of the network operation for the last button click. Why have you chosen to ignore button clicks until the current network operation is complete?
  • You seem to be over complicating the problem. Why not disable your button after a click has been received and re-enable it after dealing with the results of the network operation?
  • It's just a part of a more complicated logic, there's a reason for it
  • I've updated the description as it wasn't that clear.
  • I've updated the description as it was clumsy indeed.
  • Sorry, i've updated my answer but it's still incorrect the additional number doesn't work as I would expect :-( it just delay the mapping
  • Now it's correct but need a state flag which means is not 100% reactive :)