Front end of the Slack clone application.

count.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /** PURE_IMPORTS_START .._Subscriber PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subscriber } from '../Subscriber';
  10. /**
  11. * Counts the number of emissions on the source and emits that number when the
  12. * source completes.
  13. *
  14. * <span class="informal">Tells how many values were emitted, when the source
  15. * completes.</span>
  16. *
  17. * <img src="./img/count.png" width="100%">
  18. *
  19. * `count` transforms an Observable that emits values into an Observable that
  20. * emits a single value that represents the number of values emitted by the
  21. * source Observable. If the source Observable terminates with an error, `count`
  22. * will pass this error notification along without emitting a value first. If
  23. * the source Observable does not terminate at all, `count` will neither emit
  24. * a value nor terminate. This operator takes an optional `predicate` function
  25. * as argument, in which case the output emission will represent the number of
  26. * source values that matched `true` with the `predicate`.
  27. *
  28. * @example <caption>Counts how many seconds have passed before the first click happened</caption>
  29. * var seconds = Rx.Observable.interval(1000);
  30. * var clicks = Rx.Observable.fromEvent(document, 'click');
  31. * var secondsBeforeClick = seconds.takeUntil(clicks);
  32. * var result = secondsBeforeClick.count();
  33. * result.subscribe(x => console.log(x));
  34. *
  35. * @example <caption>Counts how many odd numbers are there between 1 and 7</caption>
  36. * var numbers = Rx.Observable.range(1, 7);
  37. * var result = numbers.count(i => i % 2 === 1);
  38. * result.subscribe(x => console.log(x));
  39. *
  40. * // Results in:
  41. * // 4
  42. *
  43. * @see {@link max}
  44. * @see {@link min}
  45. * @see {@link reduce}
  46. *
  47. * @param {function(value: T, i: number, source: Observable<T>): boolean} [predicate] A
  48. * boolean function to select what values are to be counted. It is provided with
  49. * arguments of:
  50. * - `value`: the value from the source Observable.
  51. * - `index`: the (zero-based) "index" of the value from the source Observable.
  52. * - `source`: the source Observable instance itself.
  53. * @return {Observable} An Observable of one number that represents the count as
  54. * described above.
  55. * @method count
  56. * @owner Observable
  57. */
  58. export function count(predicate) {
  59. return function (source) { return source.lift(new CountOperator(predicate, source)); };
  60. }
  61. var CountOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  62. function CountOperator(predicate, source) {
  63. this.predicate = predicate;
  64. this.source = source;
  65. }
  66. CountOperator.prototype.call = function (subscriber, source) {
  67. return source.subscribe(new CountSubscriber(subscriber, this.predicate, this.source));
  68. };
  69. return CountOperator;
  70. }());
  71. /**
  72. * We need this JSDoc comment for affecting ESDoc.
  73. * @ignore
  74. * @extends {Ignored}
  75. */
  76. var CountSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  77. __extends(CountSubscriber, _super);
  78. function CountSubscriber(destination, predicate, source) {
  79. _super.call(this, destination);
  80. this.predicate = predicate;
  81. this.source = source;
  82. this.count = 0;
  83. this.index = 0;
  84. }
  85. CountSubscriber.prototype._next = function (value) {
  86. if (this.predicate) {
  87. this._tryPredicate(value);
  88. }
  89. else {
  90. this.count++;
  91. }
  92. };
  93. CountSubscriber.prototype._tryPredicate = function (value) {
  94. var result;
  95. try {
  96. result = this.predicate(value, this.index++, this.source);
  97. }
  98. catch (err) {
  99. this.destination.error(err);
  100. return;
  101. }
  102. if (result) {
  103. this.count++;
  104. }
  105. };
  106. CountSubscriber.prototype._complete = function () {
  107. this.destination.next(this.count);
  108. this.destination.complete();
  109. };
  110. return CountSubscriber;
  111. }(Subscriber));
  112. //# sourceMappingURL=count.js.map