Search code examples
angularrxjspipeobservableconcatenation

RXJS concat: Does it execute already completed observables when another observable in the pipe executes again?


I have 2 observable: initialLessons$ and searchLessons$. The initialLessons$ displays a completed http fetch request of courses initially. The searchLessons$ request is an observable fromEvent that listens on button click keyup event and executes after a button click on the search filter button (with a filter parameter).

Now, if I have this.lessons$ = concat(initialLessons$, searchlessons$), normally, the operator concat from rxjs first executes the first observable, then waits for it to complete, before executing the second observable.

If I have already completed the first observable on initial request, does it "skip" the completed observable and execute the second observable only, e.g if I launch a search request on button click? Or will it again execute the first observable in the pipe and then the others sequentially? Because that would be a waste of ressources...

And same, if I only e.g execute the first observable, and the second has already completed (or is unnecessary to execute), will it still execute the second (and all others in the parameter list)?


Solution

  • If I have already completed the first observable on initial request, does it "skip" the completed observable and execute the second observable only, e.g if I launch a search request on button click? Or will it again execute the first observable in the pipe and then the others sequentially? Because that would be a waste of ressources...

    Yes, it does "skip" the completed observable(s). Concat works like you mentioned, concat will subscribe to the-i if and only if i-1 observable is completed. So now every time click a button, lesson$ will emit the event.

    And same, if I only e.g execute the first observable, and the second has already completed (or is unnecessary to execute), will it still execute the second (and all others in the parameter list)?

    This question does not make sense (not in an offending way by the way), because if the i observable never completed, how the i+1 already completed? The keyword here is concat WILL SUBSCRIBE to every parameters.