Debian Codegraph @

node/rxjs/src/internal/operators/skipLast.ts

raw
1 import { MonoTypeOperatorFunction } from '../types';
2 import { identity } from '../util/identity';
3 import { operate } from '../util/lift';
4 import { createOperatorSubscriber } from './OperatorSubscriber';
5
6 /**
7 * Skip a specified number of values before the completion of an observable.
8 *
9 * ![](skipLast.png)
10 *
11 * Returns an observable that will emit values as soon as it can, given a number of
12 * skipped values. For example, if you `skipLast(3)` on a source, when the source
13 * emits its fourth value, the first value the source emitted will finally be emitted
14 * from the returned observable, as it is no longer part of what needs to be skipped.
15 *
16 * All values emitted by the result of `skipLast(N)` will be delayed by `N` emissions,
17 * as each value is held in a buffer until enough values have been emitted that that
18 * the buffered value may finally be sent to the consumer.
19 *
20 * After subscribing, unsubscribing will not result in the emission of the buffered
21 * skipped values.
22 *
23 * ## Example
24 *
25 * Skip the last 2 values of an observable with many values
26 *
27 * ```ts
28 * import { of, skipLast } from 'rxjs';
29 *
30 * const numbers = of(1, 2, 3, 4, 5);
31 * const skipLastTwo = numbers.pipe(skipLast(2));
32 * skipLastTwo.subscribe(x => console.log(x));
33 *
34 * // Results in:
35 * // 1 2 3
36 * // (4 and 5 are skipped)
37 * ```
38 *
39 * @see {@link skip}
40 * @see {@link skipUntil}
41 * @see {@link skipWhile}
42 * @see {@link take}
43 *
44 * @param skipCount Number of elements to skip from the end of the source Observable.
45 * @return A function that returns an Observable that skips the last `count`
46 * values emitted by the source Observable.
47 */
48 export function skipLast<T>(skipCount: number): MonoTypeOperatorFunction<T> {
49 return skipCount <= 0
50 ? // For skipCounts less than or equal to zero, we are just mirroring the source.
51 identity
52 : operate((source, subscriber) => {
53 // A ring buffer to hold the values while we wait to see
54 // if we can emit it or it's part of the "skipped" last values.
55 // Note that it is the _same size_ as the skip count.
56 let ring: T[] = new Array(skipCount);
57 // The number of values seen so far. This is used to get
58 // the index of the current value when it arrives.
59 let seen = 0;
60 source.subscribe(
61 createOperatorSubscriber(subscriber, (value) => {
62 // Get the index of the value we have right now
63 // relative to all other values we've seen, then
64 // increment `seen`. This ensures we've moved to
65 // the next slot in our ring buffer.
66 const valueIndex = seen++;
67 if (valueIndex < skipCount) {
68 // If we haven't seen enough values to fill our buffer yet,
69 // Then we aren't to a number of seen values where we can
70 // emit anything, so let's just start by filling the ring buffer.
71 ring[valueIndex] = value;
72 } else {
73 // We are traversing over the ring array in such
74 // a way that when we get to the end, we loop back
75 // and go to the start.
76 const index = valueIndex % skipCount;
77 // Pull the oldest value out so we can emit it,
78 // and stuff the new value in it's place.
79 const oldValue = ring[index];
80 ring[index] = value;
81 // Emit the old value. It is important that this happens
82 // after we swap the value in the buffer, if it happens
83 // before we swap the value in the buffer, then a synchronous
84 // source can get the buffer out of whack.
85 subscriber.next(oldValue);
86 }
87 })
88 );
89
90 return () => {
91 // Release our values in memory
92 ring = null!;
93 };
94 });
95 }