takeWhile.js 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * Emits values emitted by the source Observable so long as each value satisfies
  12. * the given `predicate`, and then completes as soon as this `predicate` is not
  13. * satisfied.
  14. *
  15. * <span class="informal">Takes values from the source only while they pass the
  16. * condition given. When the first value does not satisfy, it completes.</span>
  17. *
  18. * <img src="./img/takeWhile.png" width="100%">
  19. *
  20. * `takeWhile` subscribes and begins mirroring the source Observable. Each value
  21. * emitted on the source is given to the `predicate` function which returns a
  22. * boolean, representing a condition to be satisfied by the source values. The
  23. * output Observable emits the source values until such time as the `predicate`
  24. * returns false, at which point `takeWhile` stops mirroring the source
  25. * Observable and completes the output Observable.
  26. *
  27. * @example <caption>Emit click events only while the clientX property is greater than 200</caption>
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var result = clicks.takeWhile(ev => ev.clientX > 200);
  30. * result.subscribe(x => console.log(x));
  31. *
  32. * @see {@link take}
  33. * @see {@link takeLast}
  34. * @see {@link takeUntil}
  35. * @see {@link skip}
  36. *
  37. * @param {function(value: T, index: number): boolean} predicate A function that
  38. * evaluates a value emitted by the source Observable and returns a boolean.
  39. * Also takes the (zero-based) index as the second argument.
  40. * @return {Observable<T>} An Observable that emits the values from the source
  41. * Observable so long as each value satisfies the condition defined by the
  42. * `predicate`, then completes.
  43. * @method takeWhile
  44. * @owner Observable
  45. */
  46. export function takeWhile(predicate) {
  47. return function (source) { return source.lift(new TakeWhileOperator(predicate)); };
  48. }
  49. var TakeWhileOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  50. function TakeWhileOperator(predicate) {
  51. this.predicate = predicate;
  52. }
  53. TakeWhileOperator.prototype.call = function (subscriber, source) {
  54. return source.subscribe(new TakeWhileSubscriber(subscriber, this.predicate));
  55. };
  56. return TakeWhileOperator;
  57. }());
  58. /**
  59. * We need this JSDoc comment for affecting ESDoc.
  60. * @ignore
  61. * @extends {Ignored}
  62. */
  63. var TakeWhileSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  64. __extends(TakeWhileSubscriber, _super);
  65. function TakeWhileSubscriber(destination, predicate) {
  66. _super.call(this, destination);
  67. this.predicate = predicate;
  68. this.index = 0;
  69. }
  70. TakeWhileSubscriber.prototype._next = function (value) {
  71. var destination = this.destination;
  72. var result;
  73. try {
  74. result = this.predicate(value, this.index++);
  75. }
  76. catch (err) {
  77. destination.error(err);
  78. return;
  79. }
  80. this.nextOrComplete(value, result);
  81. };
  82. TakeWhileSubscriber.prototype.nextOrComplete = function (value, predicateResult) {
  83. var destination = this.destination;
  84. if (Boolean(predicateResult)) {
  85. destination.next(value);
  86. }
  87. else {
  88. destination.complete();
  89. }
  90. };
  91. return TakeWhileSubscriber;
  92. }(Subscriber));
  93. //# sourceMappingURL=takeWhile.js.map