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

takeLast.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  4. import { empty } from '../observable/empty';
  5. import { Observable } from '../Observable';
  6. import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
  7. /**
  8. * Emits only the last `count` values emitted by the source Observable.
  9. *
  10. * <span class="informal">Remembers the latest `count` values, then emits those
  11. * only when the source completes.</span>
  12. *
  13. * ![](takeLast.png)
  14. *
  15. * `takeLast` returns an Observable that emits at most the last `count` values
  16. * emitted by the source Observable. If the source emits fewer than `count`
  17. * values then all of its values are emitted. This operator must wait until the
  18. * `complete` notification emission from the source in order to emit the `next`
  19. * values on the output Observable, because otherwise it is impossible to know
  20. * whether or not more values will be emitted on the source. For this reason,
  21. * all values are emitted synchronously, followed by the complete notification.
  22. *
  23. * ## Example
  24. * Take the last 3 values of an Observable with many values
  25. * ```ts
  26. * import { range } from 'rxjs';
  27. * import { takeLast } from 'rxjs/operators';
  28. *
  29. * const many = range(1, 100);
  30. * const lastThree = many.pipe(takeLast(3));
  31. * lastThree.subscribe(x => console.log(x));
  32. * ```
  33. *
  34. * @see {@link take}
  35. * @see {@link takeUntil}
  36. * @see {@link takeWhile}
  37. * @see {@link skip}
  38. *
  39. * @throws {ArgumentOutOfRangeError} When using `takeLast(i)`, it delivers an
  40. * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
  41. *
  42. * @param {number} count The maximum number of values to emit from the end of
  43. * the sequence of values emitted by the source Observable.
  44. * @return {Observable<T>} An Observable that emits at most the last count
  45. * values emitted by the source Observable.
  46. * @method takeLast
  47. * @owner Observable
  48. */
  49. export function takeLast<T>(count: number): MonoTypeOperatorFunction<T> {
  50. return function takeLastOperatorFunction(source: Observable<T>): Observable<T> {
  51. if (count === 0) {
  52. return empty();
  53. } else {
  54. return source.lift(new TakeLastOperator(count));
  55. }
  56. };
  57. }
  58. class TakeLastOperator<T> implements Operator<T, T> {
  59. constructor(private total: number) {
  60. if (this.total < 0) {
  61. throw new ArgumentOutOfRangeError;
  62. }
  63. }
  64. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  65. return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
  66. }
  67. }
  68. /**
  69. * We need this JSDoc comment for affecting ESDoc.
  70. * @ignore
  71. * @extends {Ignored}
  72. */
  73. class TakeLastSubscriber<T> extends Subscriber<T> {
  74. private ring: Array<T> = new Array();
  75. private count: number = 0;
  76. constructor(destination: Subscriber<T>, private total: number) {
  77. super(destination);
  78. }
  79. protected _next(value: T): void {
  80. const ring = this.ring;
  81. const total = this.total;
  82. const count = this.count++;
  83. if (ring.length < total) {
  84. ring.push(value);
  85. } else {
  86. const index = count % total;
  87. ring[index] = value;
  88. }
  89. }
  90. protected _complete(): void {
  91. const destination = this.destination;
  92. let count = this.count;
  93. if (count > 0) {
  94. const total = this.count >= this.total ? this.total : this.count;
  95. const ring = this.ring;
  96. for (let i = 0; i < total; i++) {
  97. const idx = (count++) % total;
  98. destination.next(ring[idx]);
  99. }
  100. }
  101. destination.complete();
  102. }
  103. }