Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

skipLast.ts 2.8KB

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