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

take.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 first `count` values emitted by the source Observable.
  9. *
  10. * <span class="informal">Takes the first `count` values from the source, then
  11. * completes.</span>
  12. *
  13. * ![](take.png)
  14. *
  15. * `take` returns an Observable that emits only the first `count` values emitted
  16. * by the source Observable. If the source emits fewer than `count` values then
  17. * all of its values are emitted. After that, it completes, regardless if the
  18. * source completes.
  19. *
  20. * ## Example
  21. * Take the first 5 seconds of an infinite 1-second interval Observable
  22. * ```ts
  23. * import { interval } from 'rxjs';
  24. * import { take } from 'rxjs/operators';
  25. *
  26. * const intervalCount = interval(1000);
  27. * const takeFive = intervalCount.pipe(take(5));
  28. * takeFive.subscribe(x => console.log(x));
  29. *
  30. * // Logs:
  31. * // 0
  32. * // 1
  33. * // 2
  34. * // 3
  35. * // 4
  36. * ```
  37. *
  38. * @see {@link takeLast}
  39. * @see {@link takeUntil}
  40. * @see {@link takeWhile}
  41. * @see {@link skip}
  42. *
  43. * @throws {ArgumentOutOfRangeError} When using `take(i)`, it delivers an
  44. * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
  45. *
  46. * @param {number} count The maximum number of `next` values to emit.
  47. * @return {Observable<T>} An Observable that emits only the first `count`
  48. * values emitted by the source Observable, or all of the values from the source
  49. * if the source emits fewer than `count` values.
  50. * @method take
  51. * @owner Observable
  52. */
  53. export function take<T>(count: number): MonoTypeOperatorFunction<T> {
  54. return (source: Observable<T>) => {
  55. if (count === 0) {
  56. return empty();
  57. } else {
  58. return source.lift(new TakeOperator(count));
  59. }
  60. };
  61. }
  62. class TakeOperator<T> implements Operator<T, T> {
  63. constructor(private total: number) {
  64. if (this.total < 0) {
  65. throw new ArgumentOutOfRangeError;
  66. }
  67. }
  68. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  69. return source.subscribe(new TakeSubscriber(subscriber, this.total));
  70. }
  71. }
  72. /**
  73. * We need this JSDoc comment for affecting ESDoc.
  74. * @ignore
  75. * @extends {Ignored}
  76. */
  77. class TakeSubscriber<T> extends Subscriber<T> {
  78. private count: number = 0;
  79. constructor(destination: Subscriber<T>, private total: number) {
  80. super(destination);
  81. }
  82. protected _next(value: T): void {
  83. const total = this.total;
  84. const count = ++this.count;
  85. if (count <= total) {
  86. this.destination.next(value);
  87. if (count === total) {
  88. this.destination.complete();
  89. this.unsubscribe();
  90. }
  91. }
  92. }
  93. }