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';
8 * Returns an Observable that will resubscribe to the source stream when the source stream completes, at most count times.
10 * <span class="informal">Repeats all values emitted on the source. It's like {@link retry}, but for non error cases.</span>
14 * Similar to {@link retry}, this operator repeats the stream of items emitted by the source for non error cases.
15 * Repeat can be useful for creating observables that are meant to have some repeated pattern or rhythm.
17 * Note: `repeat(0)` returns an empty observable and `repeat()` will repeat forever
20 * Repeat a message stream
22 * import { of } from 'rxjs';
23 * import { repeat, delay } from 'rxjs/operators';
25 * const source = of('Repeat message');
26 * const example = source.pipe(repeat(3));
27 * example.subscribe(x => console.log(x));
35 * Repeat 3 values, 2 times
37 * import { interval } from 'rxjs';
38 * import { repeat, take } from 'rxjs/operators';
40 * const source = interval(1000);
41 * const example = source.pipe(take(3), repeat(2));
42 * example.subscribe(x => console.log(x));
44 * // Results every second
53 * @see {@link repeatWhen}
56 * @param {number} [count] The number of times the source Observable items are repeated, a count of 0 will yield
57 * an empty Observable.
58 * @return {Observable} An Observable that will resubscribe to the source stream when the source stream completes
59 * , at most count times.
63 export function repeat<T>(count: number = -1): MonoTypeOperatorFunction<T> {
64 return (source: Observable<T>) => {
67 } else if (count < 0) {
68 return source.lift(new RepeatOperator(-1, source));
70 return source.lift(new RepeatOperator(count - 1, source));
75 class RepeatOperator<T> implements Operator<T, T> {
76 constructor(private count: number,
77 private source: Observable<T>) {
79 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
80 return source.subscribe(new RepeatSubscriber(subscriber, this.count, this.source));
85 * We need this JSDoc comment for affecting ESDoc.
89 class RepeatSubscriber<T> extends Subscriber<T> {
90 constructor(destination: Subscriber<any>,
91 private count: number,
92 private source: Observable<T>) {
96 if (!this.isStopped) {
97 const { source, count } = this;
99 return super.complete();
100 } else if (count > -1) {
101 this.count = count - 1;
103 source.subscribe(this._unsubscribeAndRecycle());