source: trip-planner-front/node_modules/rxjs/src/internal/operators/skipLast.ts@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1import { Operator } from '../Operator';
2import { Subscriber } from '../Subscriber';
3import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
4import { Observable } from '../Observable';
5import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
6
7/**
8 * Skip the last `count` values emitted by the source Observable.
9 *
10 * ![](skipLast.png)
11 *
12 * `skipLast` returns an Observable that accumulates a queue with a length
13 * enough to store the first `count` values. As more values are received,
14 * values are taken from the front of the queue and produced on the result
15 * sequence. This causes values to be delayed.
16 *
17 * ## Example
18 * Skip the last 2 values of an Observable with many values
19 * ```ts
20 * import { range } from 'rxjs';
21 * import { skipLast } from 'rxjs/operators';
22 *
23 * const many = range(1, 5);
24 * const skipLastTwo = many.pipe(skipLast(2));
25 * skipLastTwo.subscribe(x => console.log(x));
26 *
27 * // Results in:
28 * // 1 2 3
29 * ```
30 *
31 * @see {@link skip}
32 * @see {@link skipUntil}
33 * @see {@link skipWhile}
34 * @see {@link take}
35 *
36 * @throws {ArgumentOutOfRangeError} When using `skipLast(i)`, it throws
37 * ArgumentOutOrRangeError if `i < 0`.
38 *
39 * @param {number} count Number of elements to skip from the end of the source Observable.
40 * @returns {Observable<T>} An Observable that skips the last count values
41 * emitted by the source Observable.
42 * @method skipLast
43 * @owner Observable
44 */
45export function skipLast<T>(count: number): MonoTypeOperatorFunction<T> {
46 return (source: Observable<T>) => source.lift(new SkipLastOperator(count));
47}
48
49class SkipLastOperator<T> implements Operator<T, T> {
50 constructor(private _skipCount: number) {
51 if (this._skipCount < 0) {
52 throw new ArgumentOutOfRangeError;
53 }
54 }
55
56 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
57 if (this._skipCount === 0) {
58 // If we don't want to skip any values then just subscribe
59 // to Subscriber without any further logic.
60 return source.subscribe(new Subscriber(subscriber));
61 } else {
62 return source.subscribe(new SkipLastSubscriber(subscriber, this._skipCount));
63 }
64 }
65}
66
67/**
68 * We need this JSDoc comment for affecting ESDoc.
69 * @ignore
70 * @extends {Ignored}
71 */
72class SkipLastSubscriber<T> extends Subscriber<T> {
73 private _ring: T[];
74 private _count: number = 0;
75
76 constructor(destination: Subscriber<T>, private _skipCount: number) {
77 super(destination);
78 this._ring = new Array<T>(_skipCount);
79 }
80
81 protected _next(value: T): void {
82 const skipCount = this._skipCount;
83 const count = this._count++;
84
85 if (count < skipCount) {
86 this._ring[count] = value;
87 } else {
88 const currentIndex = count % skipCount;
89 const ring = this._ring;
90 const oldValue = ring[currentIndex];
91
92 ring[currentIndex] = value;
93 this.destination.next(oldValue);
94 }
95 }
96}
Note: See TracBrowser for help on using the repository browser.