Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | import { Subscriber } from '../Subscriber';
|
---|
2 | export function count(predicate) {
|
---|
3 | return (source) => source.lift(new CountOperator(predicate, source));
|
---|
4 | }
|
---|
5 | class CountOperator {
|
---|
6 | constructor(predicate, source) {
|
---|
7 | this.predicate = predicate;
|
---|
8 | this.source = source;
|
---|
9 | }
|
---|
10 | call(subscriber, source) {
|
---|
11 | return source.subscribe(new CountSubscriber(subscriber, this.predicate, this.source));
|
---|
12 | }
|
---|
13 | }
|
---|
14 | class CountSubscriber extends Subscriber {
|
---|
15 | constructor(destination, predicate, source) {
|
---|
16 | super(destination);
|
---|
17 | this.predicate = predicate;
|
---|
18 | this.source = source;
|
---|
19 | this.count = 0;
|
---|
20 | this.index = 0;
|
---|
21 | }
|
---|
22 | _next(value) {
|
---|
23 | if (this.predicate) {
|
---|
24 | this._tryPredicate(value);
|
---|
25 | }
|
---|
26 | else {
|
---|
27 | this.count++;
|
---|
28 | }
|
---|
29 | }
|
---|
30 | _tryPredicate(value) {
|
---|
31 | let result;
|
---|
32 | try {
|
---|
33 | result = this.predicate(value, this.index++, this.source);
|
---|
34 | }
|
---|
35 | catch (err) {
|
---|
36 | this.destination.error(err);
|
---|
37 | return;
|
---|
38 | }
|
---|
39 | if (result) {
|
---|
40 | this.count++;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | _complete() {
|
---|
44 | this.destination.next(this.count);
|
---|
45 | this.destination.complete();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | //# sourceMappingURL=count.js.map |
---|
Note:
See
TracBrowser
for help on using the repository browser.