Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

count.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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