-
Notifications
You must be signed in to change notification settings - Fork 2
/
repeat.ts
41 lines (36 loc) · 1004 Bytes
/
repeat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Operator, Observer, Unsubscribe } from '../index';
import { createSource } from '../sources';
import subscribe from '../utils/subscribe';
/**
* Returns a source that will resubscribe to the initial source when it completes, at most count times
*
* @param count - number of times the source items are repeated
* @return callbag operator
*
* @public
*/
function repeat<I>(count: number): Operator<I, I> {
return source => {
return createSource((next, complete, error) => {
let remaining = count;
let unsubscribe: Unsubscribe = () => {};
const observer: Observer<I> = {
next,
error,
complete: () => {
remaining -= 1;
if (remaining > 0) {
unsubscribe = subscribe(source)(observer);
} else {
complete();
}
},
};
if (remaining > 0) {
unsubscribe = subscribe(source)(observer);
}
return unsubscribe;
});
};
}
export default repeat;