| 1 |
import { EMPTY } from '../observable/empty'; |
| 2 |
import { MonoTypeOperatorFunction } from '../types'; |
| 3 |
import { operate } from '../util/lift'; |
| 4 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Waits for the source to complete, then emits the last N values from the source, |
| 8 |
* as specified by the `count` argument. |
| 9 |
* |
| 10 |
*  |
| 11 |
* |
| 12 |
* `takeLast` results in an observable that will hold values up to `count` values in memory, |
| 13 |
* until the source completes. It then pushes all values in memory to the consumer, in the |
| 14 |
* order they were received from the source, then notifies the consumer that it is |
| 15 |
* complete. |
| 16 |
* |
| 17 |
* If for some reason the source completes before the `count` supplied to `takeLast` is reached, |
| 18 |
* all values received until that point are emitted, and then completion is notified. |
| 19 |
* |
| 20 |
* **Warning**: Using `takeLast` with an observable that never completes will result |
| 21 |
* in an observable that never emits a value. |
| 22 |
* |
| 23 |
* ## Example |
| 24 |
* |
| 25 |
* Take the last 3 values of an Observable with many values |
| 26 |
* |
| 27 |
* ```ts |
| 28 |
* import { range, takeLast } from 'rxjs'; |
| 29 |
* |
| 30 |
* const many = range(1, 100); |
| 31 |
* const lastThree = many.pipe(takeLast(3)); |
| 32 |
* lastThree.subscribe(x => console.log(x)); |
| 33 |
* ``` |
| 34 |
* |
| 35 |
* @see {@link take} |
| 36 |
* @see {@link takeUntil} |
| 37 |
* @see {@link takeWhile} |
| 38 |
* @see {@link skip} |
| 39 |
* |
| 40 |
* @param count The maximum number of values to emit from the end of |
| 41 |
* the sequence of values emitted by the source Observable. |
| 42 |
* @return A function that returns an Observable that emits at most the last |
| 43 |
* `count` values emitted by the source Observable. |
| 44 |
*/ |
| 45 |
export function takeLast<T>(count: number): MonoTypeOperatorFunction<T> { |
| 46 |
return count <= 0 |
| 47 |
? () => EMPTY |
| 48 |
: operate((source, subscriber) => { |
| 49 |
// This buffer will hold the values we are going to emit |
| 50 |
// when the source completes. Since we only want to take the |
| 51 |
// last N values, we can't emit until we're sure we're not getting |
| 52 |
// any more values. |
| 53 |
let buffer: T[] = []; |
| 54 |
source.subscribe( |
| 55 |
createOperatorSubscriber( |
| 56 |
subscriber, |
| 57 |
(value) => { |
| 58 |
// Add the most recent value onto the end of our buffer. |
| 59 |
buffer.push(value); |
| 60 |
// If our buffer is now larger than the number of values we |
| 61 |
// want to take, we remove the oldest value from the buffer. |
| 62 |
count < buffer.length && buffer.shift(); |
| 63 |
}, |
| 64 |
() => { |
| 65 |
// The source completed, we now know what are last values |
| 66 |
// are, emit them in the order they were received. |
| 67 |
for (const value of buffer) { |
| 68 |
subscriber.next(value); |
| 69 |
} |
| 70 |
subscriber.complete(); |
| 71 |
}, |
| 72 |
// Errors are passed through to the consumer |
| 73 |
undefined, |
| 74 |
() => { |
| 75 |
// During finalization release the values in our buffer. |
| 76 |
buffer = null!; |
| 77 |
} |
| 78 |
) |
| 79 |
); |
| 80 |
}); |
| 81 |
} |