In this lecture, we are going to explore the take operator for limiting the values pushed by an observable.
The Tic operator accepts the number of values that can be pushed from the observable.
Internally, it'll keep track of the values pushed from the previous observable.
Once the limit is reached, the observable completes.
It's an operator that can force and observable to complete.
For example, if we limit the number of emitted values to five, the observable will complete after
five emitted values.
If the observable attempts to emit additional values, they will be ignored.
The take operator will continue to push the values onto the next observable until the threshold has
been met.
Let's look at an example at the moment, we are pushing five values to the Reduce operator.
However, there's one possible issue with this operator it does not push the value until the observable
has been completed.
Therefore, we will never receive the accumulated value if an observable is ongoing.
Let's implement the take operator as a solution to this problem.
First, we should change our observable.
Let's replace the of operator with the interval.
Operator We will grab the interval operator from the SJS package.
Next, we will replace the of operator with the interval operator.
The duration will be five hundred milliseconds.
By default, the interval operator will limit numbers.
The console is not logging new values.
It's because the interval operator will run forever.
We will import the take operator to force completion.
The Take operator will be added at the top of the pipeline.
The order does matter in this scenario.
By default, the take operator will pass on the value to the next operator in the chain before it does.
It'll check if the limit has been surpassed.
If it has, the observable will be completed.
On the other hand, the Reduce operator does not pass on the value.
If we added the Take operator after the Reduce operator, it would never be able to limit the values
emitted from the observable.
Checking if the threshold has been met should occur first before moving on to the Reduce operator.
Let's set the limit to five.
After a few seconds, the console should log the total from the interval.
The value is 10.
We aren't getting 15 because the interval operator emits zero as its first value by combining the power
of the take and reduce operators.
We have complete control over how values are accumulated over time from an observable.
They work really well together.
In some cases, you may want to view the accumulated value as it is updated in real time.
There is an alternative operator that behaves similarly to the Reduce operator at the top of the file.
We are going to import an operator called Scan.
The scan operator performs the exact same operation as the reduced operator, a reducer is applied to
the emitted value as they come in.
So what's the difference?
The accumulated value will be pushed on to the next operator or observer.
It doesn't wait for the observable to complete.
This can be useful for viewing the accumulated value over time.
Let's replace the reduced operator with the scan operator.
The arguments for the scan operator are the same as the Reduce operator.
We don't need to modify our existing code base besides swapping the operator.
If we look in the console, we can see the accumulated values.
We have two operators at our disposal for performing similar tasks.
Choosing an operator will depend on when you need the value.