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