count.js
1.22 KB
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
42
43
44
45
46
47
48
import { Subscriber } from '../Subscriber';
export function count(predicate) {
return (source) => source.lift(new CountOperator(predicate, source));
}
class CountOperator {
constructor(predicate, source) {
this.predicate = predicate;
this.source = source;
}
call(subscriber, source) {
return source.subscribe(new CountSubscriber(subscriber, this.predicate, this.source));
}
}
class CountSubscriber extends Subscriber {
constructor(destination, predicate, source) {
super(destination);
this.predicate = predicate;
this.source = source;
this.count = 0;
this.index = 0;
}
_next(value) {
if (this.predicate) {
this._tryPredicate(value);
}
else {
this.count++;
}
}
_tryPredicate(value) {
let result;
try {
result = this.predicate(value, this.index++, this.source);
}
catch (err) {
this.destination.error(err);
return;
}
if (result) {
this.count++;
}
}
_complete() {
this.destination.next(this.count);
this.destination.complete();
}
}
//# sourceMappingURL=count.js.map