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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { Observable } from '../Observable';
  4. import { empty } from '../observable/empty';
  5. import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
  6. /**
  7. * Returns an Observable that will resubscribe to the source stream when the source stream completes, at most count times.
  8. *
  9. * <span class="informal">Repeats all values emitted on the source. It's like {@link retry}, but for non error cases.</span>
  10. *
  11. * ![](repeat.png)
  12. *
  13. * Similar to {@link retry}, this operator repeats the stream of items emitted by the source for non error cases.
  14. * Repeat can be useful for creating observables that are meant to have some repeated pattern or rhythm.
  15. *
  16. * Note: `repeat(0)` returns an empty observable and `repeat()` will repeat forever
  17. *
  18. * ## Example
  19. * Repeat a message stream
  20. * ```ts
  21. * import { of } from 'rxjs';
  22. * import { repeat, delay } from 'rxjs/operators';
  23. *
  24. * const source = of('Repeat message');
  25. * const example = source.pipe(repeat(3));
  26. * example.subscribe(x => console.log(x));
  27. *
  28. * // Results
  29. * // Repeat message
  30. * // Repeat message
  31. * // Repeat message
  32. * ```
  33. *
  34. * Repeat 3 values, 2 times
  35. * ```ts
  36. * import { interval } from 'rxjs';
  37. * import { repeat, take } from 'rxjs/operators';
  38. *
  39. * const source = interval(1000);
  40. * const example = source.pipe(take(3), repeat(2));
  41. * example.subscribe(x => console.log(x));
  42. *
  43. * // Results every second
  44. * // 0
  45. * // 1
  46. * // 2
  47. * // 0
  48. * // 1
  49. * // 2
  50. * ```
  51. *
  52. * @see {@link repeatWhen}
  53. * @see {@link retry}
  54. *
  55. * @param {number} [count] The number of times the source Observable items are repeated, a count of 0 will yield
  56. * an empty Observable.
  57. * @return {Observable} An Observable that will resubscribe to the source stream when the source stream completes
  58. * , at most count times.
  59. * @method repeat
  60. * @owner Observable
  61. */
  62. export function repeat<T>(count: number = -1): MonoTypeOperatorFunction<T> {
  63. return (source: Observable<T>) => {
  64. if (count === 0) {
  65. return empty();
  66. } else if (count < 0) {
  67. return source.lift(new RepeatOperator(-1, source));
  68. } else {
  69. return source.lift(new RepeatOperator(count - 1, source));
  70. }
  71. };
  72. }
  73. class RepeatOperator<T> implements Operator<T, T> {
  74. constructor(private count: number,
  75. private source: Observable<T>) {
  76. }
  77. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  78. return source.subscribe(new RepeatSubscriber(subscriber, this.count, this.source));
  79. }
  80. }
  81. /**
  82. * We need this JSDoc comment for affecting ESDoc.
  83. * @ignore
  84. * @extends {Ignored}
  85. */
  86. class RepeatSubscriber<T> extends Subscriber<T> {
  87. constructor(destination: Subscriber<any>,
  88. private count: number,
  89. private source: Observable<T>) {
  90. super(destination);
  91. }
  92. complete() {
  93. if (!this.isStopped) {
  94. const { source, count } = this;
  95. if (count === 0) {
  96. return super.complete();
  97. } else if (count > -1) {
  98. this.count = count - 1;
  99. }
  100. source.subscribe(this._unsubscribeAndRecycle());
  101. }
  102. }
  103. }