a zip code crypto-currency system good for red ONLY

takeWhile.js 3.4KB

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