count.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /** PURE_IMPORTS_START tslib,_Subscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. export function count(predicate) {
  5. return function (source) { return source.lift(new CountOperator(predicate, source)); };
  6. }
  7. var CountOperator = /*@__PURE__*/ (function () {
  8. function CountOperator(predicate, source) {
  9. this.predicate = predicate;
  10. this.source = source;
  11. }
  12. CountOperator.prototype.call = function (subscriber, source) {
  13. return source.subscribe(new CountSubscriber(subscriber, this.predicate, this.source));
  14. };
  15. return CountOperator;
  16. }());
  17. var CountSubscriber = /*@__PURE__*/ (function (_super) {
  18. tslib_1.__extends(CountSubscriber, _super);
  19. function CountSubscriber(destination, predicate, source) {
  20. var _this = _super.call(this, destination) || this;
  21. _this.predicate = predicate;
  22. _this.source = source;
  23. _this.count = 0;
  24. _this.index = 0;
  25. return _this;
  26. }
  27. CountSubscriber.prototype._next = function (value) {
  28. if (this.predicate) {
  29. this._tryPredicate(value);
  30. }
  31. else {
  32. this.count++;
  33. }
  34. };
  35. CountSubscriber.prototype._tryPredicate = function (value) {
  36. var result;
  37. try {
  38. result = this.predicate(value, this.index++, this.source);
  39. }
  40. catch (err) {
  41. this.destination.error(err);
  42. return;
  43. }
  44. if (result) {
  45. this.count++;
  46. }
  47. };
  48. CountSubscriber.prototype._complete = function () {
  49. this.destination.next(this.count);
  50. this.destination.complete();
  51. };
  52. return CountSubscriber;
  53. }(Subscriber));
  54. //# sourceMappingURL=count.js.map